confused_kid
confused_kid

Reputation:

regdeletekey returning file not found

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.

Upvotes: 1

Views: 5859

Answers (2)

Dave S
Dave S

Reputation: 1492

Two things to check for error 2 / "file not found":

  • Make sure it's not a "value" within a key instead of an actual key.

If you create a folder like HKCU > Software > CompanyName and then store a value like option = "foo" with RegSetValueEx, then you need to delete this with RegDeleteValue or RegDeleteValueEx.

  • If it's a 64-bit OS, there are separate registry views for 32-bit vs. 64-bit. By default a 32-bit app will use the 32-bit view for everything but if you created the entry using KEY_WOW64_64KEY for some reason then you need to use that when deleting.

Upvotes: 2

Matthew Iselin
Matthew Iselin

Reputation: 10660

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.

Upvotes: 4

Related Questions