2013-10-28 127 views
2

我想要显示HKLM \ SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Run中的所有注册表项,子项和值,以查看启动时运行的程序。我正在使用此代码从MS。枚举注册表项C++

void QueryKey(HKEY hKey) 
{ 
TCHAR achKey[MAX_KEY_LENGTH]; // buffer for subkey name 
DWORD cbName;     // size of name string 
TCHAR achClass[MAX_PATH] = TEXT(""); // buffer for class name 
DWORD cchClassName = MAX_PATH; // size of class string 
DWORD cSubKeys=0;    // number of subkeys 
DWORD cbMaxSubKey;    // longest subkey size 
DWORD cchMaxClass;    // longest class string 
DWORD cValues;    // number of values for key 
DWORD cchMaxValue;   // longest value name 
DWORD cbMaxValueData;  // longest value data 
DWORD cbSecurityDescriptor; // size of security descriptor 
FILETIME ftLastWriteTime;  // last write time 

DWORD i, retCode; 

TCHAR achValue[MAX_VALUE_NAME]; 
DWORD cchValue = MAX_VALUE_NAME; 

// Get the class name and the value count. 
retCode = RegQueryInfoKey(
    hKey,     // key handle 
    achClass,    // buffer for class name 
    &cchClassName,   // size of class string 
    NULL,     // reserved 
    &cSubKeys,    // number of subkeys 
    &cbMaxSubKey,   // longest subkey size 
    &cchMaxClass,   // longest class string 
    &cValues,    // number of values for this key 
    &cchMaxValue,   // longest value name 
    &cbMaxValueData,   // longest value data 
    &cbSecurityDescriptor, // security descriptor 
    &ftLastWriteTime);  // last write time 

// Enumerate the subkeys, until RegEnumKeyEx fails. 

if (cSubKeys == 0) 
{ 
    printf("No values found\n");       
} 


if (cSubKeys) 
{ 
    printf("\nNumber of subkeys: %d\n", cSubKeys); 



    for (i=0; i<cSubKeys; i++) 
    { 
     cbName = MAX_KEY_LENGTH; 
     retCode = RegEnumKeyEx(hKey, i, 
       achKey, 
       &cbName, 
       NULL, 
       NULL, 
       NULL, 
       &ftLastWriteTime); 
     if (retCode == ERROR_SUCCESS) 
     { 
      _tprintf(TEXT("(%d) %s\n"), i+1, achKey); 

     } 
    } 
} 

// Enumerate the key values. 

if (cValues) 
{ 
    printf("\nNumber of values: %d\n", cValues); 

    for (i=0, retCode=ERROR_SUCCESS; i<cValues; i++) 
    { 
     cchValue = MAX_VALUE_NAME; 
     achValue[0] = '\0'; 
     retCode = RegEnumValue(hKey, i, 
      achValue, 
      &cchValue, 
      NULL, 
      NULL, 
      NULL, 
      NULL); 

     if (retCode == ERROR_SUCCESS) 
     { 
      _tprintf(TEXT("(%d) %s\n"), i+1, achValue); 
     } 
    } 
} 
} 








int RegKeyCount = 0; 

int main(int argc, char *argv[]) 
{ 


HKEY hTestKey; 

    if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\Microsoft\\Windows \\CurrentVersion\\Run"), 0, KEY_READ, &hTestKey) == ERROR_SUCCESS) 
    { 
    QueryKey(hTestKey); 
    } 
} 

我在这混乱如果我在“SOFTWARE \微软\的Windows \ CurrentVersion”,它会告诉我所有子项和值(我可以看到,运行是CURRENTVERSION的子项)的代码,但是当我试图让它向我展示运行它的子项和值,即使有条目,也没有发现任何东西。

我也应该说我不知道​​子键/值的值的名称,它们可能是任何东西。

这确实是RegEnumValue应该做什么或我需要使用另一个注册表功能?

回答

1

我发现的唯一问题是您的参数中的空格为RegOpenKeyEx(),如果您取出嵌入的空间以使其读取TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"),则程序运行正常。

您的printf开始时有点混乱,也许您应该将"No values found\n"更改为"No keys found\n"

if (cSubKeys == 0) 
    printf("No keys found\n");       

另外:如果你构建/运行该代码作为一个64位操作系统的32位程序,要知道,你会得到HKLM \ SOFTWARE内容\ Wow6432Node \微软\的Windows \ CurrentVersion \运行,而不是HKLM \ SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Run!