c - RegEnumKeyEx 不能正常工作

标签 c winapi registry

我正在尝试创建一个列出特定(Windows 注册表)项的所有子项的函数。

发生的情况是只有第一个项目被正确输出,所有其他项目都被截断或重复 - 我认为这是一个缓冲区问题。

我已经阅读了函数文档,但对我帮助不大。

代码如下:

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

void print_list(HKEY hkey, char* path){

char dwValue[255];
DWORD  dwSize = 0;
DWORD n; // subkeys
HKEY tmp;
int i;

if(RegOpenKeyEx(hkey, path, 0, KEY_READ, &tmp) == ERROR_SUCCESS){

    DWORD  dwSize = sizeof(dwValue);

    RegQueryInfoKey(tmp,NULL,NULL,NULL,&n,NULL,NULL,NULL,NULL,NULL,NULL,NULL);      

    for(i=0; i< n; i++){
        RegEnumKeyEx(tmp,i,dwValue,&dwSize,NULL,NULL,NULL,NULL);
        printf("%s\n", dwValue);
    }

    RegCloseKey(tmp);
}
}

int main(){
print_list(HKEY_LOCAL_MACHINE, "SOFTWARE");
return 0;
}

最佳答案

在调用 RegEnumKeyEx() 之前添加以下行:

dwSize = sizeof(dwValue);

as dwSize 既是输入参数又是输出参数。在输入时它说明缓冲区的大小。来自 RegEnumKeyEx() :

A pointer to a variable that specifies the size of the buffer specified by the lpName parameter, in characters. This size should include the terminating null character. If the function succeeds, the variable pointed to by lpcName contains the number of characters stored in the buffer, not including the terminating null character.

请注意,您应该始终检查函数的返回值(就像您对 RegOpenKeyEx() 所做的那样)。

关于c - RegEnumKeyEx 不能正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11802590/

相关文章:

c - 从链表中删除最后一个元素

c - C语言删除句子中所有出现的单词的函数

android - 将 SDL C++ 应用程序交叉编译到 Android

python - 通过 Python winreg 在注册表中设置 Windows 系统 PATH

c - 如何将字符串值添加到结构中?

Python - 使用 WConio 将 unicode 打印到控制台窗口

c - 如何为 Visual Studio 2005 安装 pthread_win32(Windows pthread/posix 线程库)?

c - 拨号连接端口

windows - 编辑Windows注册表时(如果需要)可使用哪个注册表编辑器版本来启动reg文件?

c++ - 确定注册表项是否包含注册表值或子项的最佳方法是什么?