views:

124

answers:

1

I've been playing with this and I can't understand why the RegDeleteKey function is resulting to a file not found error..

I created this test key and it exists. HKLM\Software\test I am also the administrator of this computer. OS is Vista 32 bit.

int main()
{
    HKEY hReg;
    LONG oresult;
    LONG dresult;

    oresult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"SOFTWARE\\test", 0, KEY_ALL_ACCESS, &hReg);

    if(oresult == ERROR_SUCCESS)
    {
            cout << "Key opened successfully." << endl;
    }


    dresult = RegDeleteKey(hReg, L"SOFTWARE\\test");
    if(dresult == ERROR_SUCCESS)
    {
     cout << "Key deleted succssfully." << endl;
    }
    else
    {
     if(dresult == ERROR_FILE_NOT_FOUND)
     {
      cout << "Delete failed. Key not found." << endl;
      cout << "\n";
     }
    }

    RegCloseKey(hReg);

    return 0;
}

The output is:

key opened successfully delete failed. key not found.

+3  A: 

According to the MSDN page, the second parameter is a subkey of the key in hKey:

The name of the key to be deleted. It must be a subkey of the key that hKey identifies, but it cannot have subkeys. This parameter cannot be NULL.

That means your code actually tries to delete HLKM\SOFTWARE\test\SOFTWARE\test.

You probably want to try something like:

RegDeleteKey(HKEY_LOCAL_MACHINE, L"SOFTWARE\\test");

This may come in handy.

Matthew Iselin
Gah!! Nice catch, thank you.