I’m trying make a function that lists all subkeys of a specific (windows registry) key.
What’s happening is that only the 1st item is outputted correctly, all the others are truncated or repeated – I think it’s a buffer issue.
I’ve already read the function documentation, but it hasn’t helped me much.
Here’s the code:
#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;
}
Add the following line to before the call to
RegEnumKeyEx():as
dwSizeis both an input and output parameter. On input it states the size of the buffer. FromRegEnumKeyEx():Note, you should always check return values from functions (like you have done for
RegOpenKeyEx()).