无法从windows注册表中查询值
can not query values from windows registry
我正在为Windows注册表编写程序,并试图从中查询值,但即使我以管理员的权限运行自己的程序,我也无法读取所有参数,并得到错误代码5-某些值的访问被拒绝。但与此同时,标准regedit可以向我展示这个值。我做错了什么?
我有一个注册表类RegistryClass
RegistryClass.h:
class RegistryClass
{
HKEY hKey;
public:
int GetCountOfKeys();
bool OpenKey(HKEY key, std::string path);
void EnumKeys(char ** result, int * count);
...
}
RegistryClass.cpp
#include "RegistryClass.h"
...
bool RegistryClass::OpenKey(HKEY key, std::string path)
{
if(RegOpenKeyExA(key,path.c_str(),NULL,KEY_ALL_ACCESS,&hKey) == ERROR_SUCCESS)
{
return true;
}
hKey = NULL;
return false;
}
int RegistryClass::GetCountOfKeys()
{
DWORD count = 0;
char keyName [256];
DWORD len = 255;
while(1)
{
if(RegEnumKeyExA(hKey,count,keyName,&len,NULL,NULL,NULL,NULL) != ERROR_SUCCESS)
break;
count++;
len = 255;
}
return count;
}
void RegistryClass::EnumKeys(char ** result, int * count)
{
if(hKey == NULL)
return;
*count = 0;
DWORD dwIndex = 0; // current key
char keyName [255]; // current key name
DWORD lpcchName = 255; // name length
int error;
do
{
error = RegEnumKeyExA(hKey,dwIndex,keyName,&lpcchName,NULL,NULL,NULL,NULL);
if(error == ERROR_SUCCESS)
{
memcpy(result[(*count)],keyName,lpcchName);
(*count)++;
}
dwIndex++;
lpcchName = 255;
}while(error == ERROR_SUCCESS);
}
在主程序中,我试图检索密钥
void MainWindow::InitializeFirstState()
{
item_HKEY_CLASSES_ROOT = new QTreeWidgetItem();
item_HKEY_CLASSES_ROOT->setText(0,"HKEY_CLASSES_ROOT");
// and other keys
...
tree->addTopLevelItem(item_HKEY_CLASSES_ROOT);
...
AddInternalKeys(item_HKEY_CLASSES_ROOT,true);
...
}
...
void AddInternalKeys(QTreeWidgetItem * item, bool rec)
{
std::string currentPath = GetFullPathKey(item);
// first key name
std::string keyName = GetParentKeyName(item);
RegistryClass * regC = new RegistryClass();
HKEY key;
if(strcmp(keyName.c_str(),"HKEY_CLASSES_ROOT") == 0)
{
key = HKEY_CLASSES_ROOT;
}
else if(strcmp(keyName.c_str(),"HKEY_CURRENT_USER") == 0)
{
key = HKEY_CURRENT_USER;
}
else if(strcmp(keyName.c_str(),"HKEY_LOCAL_MACHINE") == 0)
{
key = HKEY_LOCAL_MACHINE;
}
else if(strcmp(keyName.c_str(),"HKEY_USERS") == 0)
{
key = HKEY_USERS;
}
else // HKEY_CURRENT_CONFIG
{
key = HKEY_CURRENT_CONFIG;
}
if(regC->OpenKey(key,currentPath) == true)
{
internalLog->print("key opened succesfullyn");
}
else internalLog->print("key was not openedn");
int count = regC->GetCountOfKeys();
char ** keys = (char **) malloc(sizeof(char *) * count);
for(int i=0;i<count;i++)
{
keys[i] = (char *) malloc(sizeof(char) * 256);
memset(keys[i],0,sizeof(char) * 256);
}
regC->EnumKeys(keys,&count);
regC->CloseKey();
delete regC;
QTreeWidgetItem * newItem;
count--;
while(count >= 0)
{
newItem = new QTreeWidgetItem();
newItem->setText(0,keys[count]);
item->addChild(newItem);
std::string str = keys[count];
AddKeyNames(newItem,str);
free(keys[count]);
if(rec == true)
AddInternalKeys(newItem,false);
count--;
}
free(keys);
}
您请求的访问权限太多。当你真正需要的只是KEY_READ
时,你要求的是KEY_ALL_ACCESS
。您没有所有访问权限,但您有读取权限。
相关文章:
- 有没有任务栏API可以立即应用注册表更改
- 检查注册表项是否链接到(或副本)另一个注册表项
- 禁用地址共享注册表不起作用
- 如何从注册表项中提取配置单元和注册表名称
- 注册表正在设置 1 个字符
- 清理"QSettings"注册表项的最佳方法(Windows上的Qt 5)
- 在 c++ 中编辑注册表项
- 某些进程的注册表限制
- 在编译时生成某种子类/类型注册表?
- 如何测试注册表项是否存在?
- 注册表-获取值(而不是键)上次更改的时间和键创建日期
- 从注册表中检索已注销用户的 sid
- 如何正确检查 c++ 是否存在注册表项?
- 如何在不使用任何数据库的情况下制作动态注册表单?
- 强制资源管理器重新加载注册表值
- 以编程方式锁定注册表项以避免并发问题
- 如何应用注册表模式使"select class depend on input"遵守开放封闭原则?
- 如何恢复注册表项的有效权限?C++
- 如何防止 C API 注册表中的 Lua 回调被垃圾回收?
- 无法从windows注册表中查询值