Search code examples
c++visual-studioregistrywindows-7-x64

Delete key from registry HKEY_LOCAL_MACHINE


I want to make uninstaller which should delete registry key from HKEY_LOCAL_MACHINE. Problem is that every time when i try delete location get LASTERROR 2, which is usually wrong name.

HKEY hKey = HKEY_LOCAL_MACHINE;
LSTATUS deletes = SHDeleteKey(hKey, L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\NameOfApp\\");
if (deletes == ERROR_SUCCESS)
{
    return true;
}

I have same code in C# which work, but C++ not. Also try as administrator, still nothing (with C# didn't put any special privileges and work).

OS: Windows 7, 64 bit.

Do you have any idea, what is happening?


Solution

  • On 64-bit Windows there are two separated registry trees. One for 32-bit applications and the other for 64-bit ones. It depends on your application (32-bit vs 64-bit) which tree is used when calling SHDeleteKey and that could be why it is working with c# but not with c++.

    You can use RegDeleteKeyEx to explicitly define the tree you want to access.