c++ - 打印名称、类型和注册表数据

标签 c++ winapi registry

我正在尝试打印有关注册表的信息。我的问题出在第一个 for 循环中。

我无法正确打印数据类型和数据。

此外,将它们添加到同一打印件中会使程序崩溃或无法正确打印。

#include "stdafx.h"
#include <windows.h>
#include <stdio.h>

void EnumerateValues(HKEY hKey, DWORD numValues)
{
 DWORD dwIndex = 0;
    LPSTR valueName = new CHAR[64];
 DWORD valNameLen;
 DWORD dataType;
 DWORD data;
 DWORD dataSize;

    for (int i = 0; i < numValues; i++)
 {
  RegEnumValue(hKey,
     dwIndex,
     valueName,
     &valNameLen,
     NULL,
     &dataType,
     (BYTE*)&data,
     &dataSize);

  dwIndex++;

_tprintf(TEXT("(%d) %s %d\n"), i+1, valueName, dataType); 
      // printf("Code: 0x%08X\n", data);

 }
}


void EnumerateSubKeys(HKEY RootKey, char* subKey, unsigned int tabs = 0) 
{
 HKEY hKey;
    DWORD cSubKeys;        //Used to store the number of Subkeys
    DWORD maxSubkeyLen;    //Longest Subkey name length
    DWORD cValues;        //Used to store the number of Subkeys
    DWORD maxValueLen;    //Longest Subkey name length
    DWORD retCode;        //Return values of calls

 RegOpenKeyEx(RootKey, subKey, 0, KEY_ALL_ACCESS, &hKey);

    RegQueryInfoKey(hKey,            // key handle
                    NULL,            // buffer for class name
                    NULL,            // size of class string
                    NULL,            // reserved
                    &cSubKeys,        // number of subkeys
                    &maxSubkeyLen,    // longest subkey length
                    NULL,            // longest class string 
                    &cValues,        // number of values for this key 
                    &maxValueLen,    // longest value name 
                    NULL,            // longest value data 
                    NULL,            // security descriptor 
                    NULL);            // last write time

    if(cSubKeys>0)
 {
        char currentSubkey[MAX_PATH];

        for(int i=0;i < cSubKeys;i++){
   DWORD currentSubLen=MAX_PATH;

            retCode=RegEnumKeyEx(hKey,    // Handle to an open/predefined key
            i,                // Index of the subkey to retrieve.
            currentSubkey,            // buffer to receives the name of the subkey
            &currentSubLen,            // size of that buffer
            NULL,                // Reserved
            NULL,                // buffer for class string 
            NULL,                // size of that buffer
            NULL);                // last write time

            if(retCode==ERROR_SUCCESS)
   {
                for (int i = 0; i < tabs; i++)
                    printf("\t");
                printf("(%d) %s\n", i+1, currentSubkey);

                char* subKeyPath = new char[currentSubLen + strlen(subKey)];
                sprintf(subKeyPath, "%s\\%s", subKey, currentSubkey);
    EnumerateSubKeys(RootKey, subKeyPath, (tabs + 1));
   }
  }
 }
    else
 {
  EnumerateValues(hKey, cValues);
 }

 RegCloseKey(hKey); 
}


int main()
{
    EnumerateSubKeys(HKEY_CURRENT_USER,"SOFTWARE\\Dropbox");
   getchar();
    return 0;
}

最佳答案

您对 RegEnumValue 的调用不正确。它存在以下问题:

  1. 您需要在调用该函数之前初始化 valNameLen
  2. 您需要在调用该函数之前初始化 dataSize
  3. 您未能检查 RegEnumValue 的返回值,因此假定函数成功。事实上它失败了,因为你犯了上述错误。

让我们暂时忽略这个值,因为它要复杂得多。让我们尝试枚举值的名称。该代码将如下所示:

void EnumerateValues(HKEY hKey, DWORD numValues)
{
    for (DWORD dwIndex = 0; dwIndex < numValues; dwIndex++)
    {
        char valueName[64];
        DWORD valNameLen = sizeof(valueName);
        DWORD dataType;
        DWORD dataSize = 0;
        DWORD retval = RegEnumValue(hKey, dwIndex, valueName, &valNameLen,
            NULL, &dataType, NULL, &dataSize);
        if (retval == ERROR_SUCCESS)
        {
            printf("(%d) %s %d\n", dwIndex+1, valueName, dataType);
        }
        else
        {
            // handle error
        }
    }
}

另请注意,我不再像您的代码那样使用动态分配的字符数组。你的代码泄露了那个数组。显然,如果您需要处理任意大的值名称,那么您将需要使用动态分配的数组。

至于提取数据,我认为这是一项更大的任务,不在 这个问题。每种数据类型都需要特殊代码。

例如,要读取 REG_SZ,您可以使用如下代码:

char *data = new char [dataSize+1];
data[dataSize] = '\0';
valNameLen = sizeof(valueName);
DWORD retval = RegEnumValue(hKey, dwIndex, valueName, &valNameLen,
    NULL, NULL, (LPBYTE)data, &dataSize);
if (retval == ERROR_SUCCESS)
{
    printf("(%d) %s %d %s\n", dwIndex+1, valueName, dataType, data);
}
else
{
    // handle error
}
delete[] data;

关于c++ - 打印名称、类型和注册表数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22133265/

相关文章:

c++ - 如何直接从通过模板函数而不是变量的类型检查类型?

c++ - C 和 C++ 中 struct 的区别

C++:实现 ln(a) 的数值近似

c - 如何将对话框位置坐标复制到另一个对话框中?

c - Windows中的系统调用

c# - 控制台和 GUI 应用程序之间的注册表项访问差异

python - 如何在 Python 上关闭注册表重定向?

c++ - 使用 C++ 更改默认浏览器值

c++ - 命名空间 std 中没有名为 'function' 的类型

c++全局定义和初始化结构