无法从 Windows 注册表查询值 [英] can not query values from windows registry

查看:33
本文介绍了无法从 Windows 注册表查询值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为 Windows 注册表编写程序并尝试从中查询值,但即使我以管理员权限运行自己的程序,我也无法读取所有参数并收到错误代码 5 - 某些值的访问被拒绝.但与此同时,标准的 regedit 可以向我展示这个价值.我做错了什么?

I writing program for the Windows registry and trying to query values from it, but even if I running my own program with the permissions of Administrator, I can not read all parameters and got error code 5 - Access Denied for some values. But at the same time standard regedit could show me that value. What did i doing wrong?

我有一个注册表 RegistryClass 类

I have a class for the registry RegistryClass

RegistryClass.h:

RegistryClass.h:

class RegistryClass
{
    HKEY hKey;
    public:
        int GetCountOfKeys();
        bool OpenKey(HKEY key, std::string path);
        void EnumKeys(char ** result, int * count);
        ...
}

RegistryClass.cpp

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);
}

在主程序中我试图检索密钥

in main program i trying to retrive keys

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 succesfully\n");
    }
    else internalLog->print("key was not opened\n");

    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.您没有所有访问权限,但您有读取权限.

You are asking for too much access. You are asking for KEY_ALL_ACCESS when all you actually need is KEY_READ. You do not have all-access permission, but you do have read permission.

这篇关于无法从 Windows 注册表查询值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆