我正在尝试创建一个列出特定(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/