Search code examples
windowswinapiregistrystartup

RegOpenKeyEx fails on HKLM even tho requireAdministrator is enabled


I saw this question saying that in order to access Windows Registry keys you need to have requireAdministrator execution level in your manifest file. Something about Registry virtualization.

I am working with Visual Studio 2017, and in the project settings (Linker - Manifest file) I changed the execution level to requireAdministrator (and VS17 asks me to run it with admin rights to debug the program), but it still doesn't work.

EDIT: All registry functions return with no errors (claiming everything is fine). Here is a snippet:

void AddStartupKey(char* path)
{
    HKEY hKey;
    LPCTSTR sk = TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");

    RegOpenKeyEx(HKEY_LOCAL_MACHINE, sk, 0, KEY_ALL_ACCESS, &hKey);

    LPCTSTR value = TEXT("Test");

    RegSetValueEx(hKey, value, 0, REG_SZ, (LPBYTE)path, strlen(path));

    RegCloseKey(hKey);
}

path equals to argv[0] (exe path).

My question is, why no value is added to the key even though all functions are successful?

EDIT: Here is a screenshot of the manifest settings in VS17:

Visual Studio 2017 Manifest File Settings


Solution

  • Solved with adding KEY_WOW64_64KEY to RegOpenKeyEx(). Thanks everyone.