2013-11-24 60 views
0

我爲Windows註冊表編寫程序並嘗試從中查詢值,但即使我使用管理員權限運行自己的程序,也無法讀取所有參數並且得到錯誤代碼5 - 拒絕訪問某些值。但同時標準的註冊表可以顯示出我的價值。我做錯了什麼?無法從Windows註冊表查詢值

我對註冊表類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 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); 
} 
+0

您可以發佈您迄今爲止所做的代碼:-) –

+0

這也有助於瞭解哪些密鑰難以閱讀。 – john

+0

LOCAL_MACHINE \ SECURITY HKEY_LOCAL_MACHINE \ SAM HKEY_LOCAL_MACHINE \ HARDWARE子項在用戶CURRENT_CONFIG \ SYSTEM和其他一些 – show

回答

2

您要求太多訪問權限。當你真正需要的是KEY_READ時,你正在詢問KEY_ALL_ACCESS。您沒有全部訪問權限,但您擁有讀取權限。

+0

非常感謝 – show