c++ - 枚举注册表项 C++

标签 c++ winapi registry

我想显示 HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run 中的所有注册表项、子项和值,以查看启动时运行的程序。

我正在使用来自 MS 的这段代码。

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\Microsoft\Windows\CurrentVersion”运行这段代码,它会显示所有子项和值(我可以看到 Run 是 CurrentVersion 的子项),但是当我尝试获取它向我显示 Run 的子项和值,它说即使有条目也没有找到任何内容。

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

这确实是 RegEnumValue 应该做的,还是我需要使用另一个注册表函数?

最佳答案

我发现的唯一问题是 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\Microsoft\Windows\CurrentVersion\Run 的内容,而不是 HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run!

关于c++ - 枚举注册表项 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19635982/

相关文章:

C++链表追加方法

c++ - 带下拉菜单的 wxToolBar : no reaction on selected item

windows - 在不禁用主题的情况下设置 Windows 区域

C++ 创建 LPWSTR 的简单数组

batch-file - 批处理文件 - 发现的程序路径(变量) - 从发现的 reg 值(路径)运行程序

c++ - 2^power 不使用 math.pow 和乘法

C++,对数字数组进行排序以找到唯一性

C++ WIN API : When creating a child process using CreateProcess, 是否需要使输入参数具有全局生命周期?

c# - Visual Studio 2010 和创建本地数据库连接导致注册码错误

windows - 始终将 Windows 中的特定文件类型保存到一个位置?