Walk
Walk

Reputation: 757

Enumerating registry subkeys in HKEY_LOCAL_MACHINE\SOFTWARE in c++

I have trouble enumerating subkeys of HKEY_LOCAL_MACHINE\SOFTWARE, all I can get is subkeys on HKEY_LOCAL_MACHINE.

WCHAR Temp[255];
DWORD TMP = 255;
HKEY hKey;
int count = 0;
long regError;

...

regError = RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"SOFTWARE\\", NULL, KEY_ENUMERATE_SUB_KEYS, &hKey);
if (regError == ERROR_SUCCESS){
    file<<"Key opened!\nSubkeys of HKEY_LOCAL_MACHINE\\SOFTWARE:\n\n";
    while ((regError = RegEnumKeyEx(HKEY_LOCAL_MACHINE, count, Temp, &TMP, NULL, NULL, NULL, NULL)) == ERROR_SUCCESS){
        TMP = sizeof (Temp);
        count++;
        file<<count<<". "<<Temp<<std::endl;
    }
    if (regError == ERROR_NO_MORE_ITEMS) file<<"Done.";
    else file << std::endl <<"RegEnumKeyEx error!";
}
else file << std::endl <<"RegOpenKeyEx error!";

RegCloseKey(hKey);

Here is my file:

Key opened!
Subkeys of HKEY_LOCAL_MACHINE\SOFTWARE:

1. BCD00000000
2. DRIVERS
3. HARDWARE
4. SAM
5. SECURITY
6. SOFTWARE
7. SYSTEM
Done.

How can I output keys inside of HKEY_LOCAL_MACHINE\SOFTWARE and not just HKLM? Thanks.

Upvotes: 2

Views: 1423

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 596332

You are passing the wrong HKEY to RegEnumKeyEx(). You are passing the HKLM root, but you need to pass the HKEY that RegOpenKeyEx() returns. In other words, change this:

RegEnumKeyEx(HKEY_LOCAL_MACHINE, ...)

To this:

RegEnumKeyEx(hKey, ...)

Upvotes: 3

Related Questions