RegOpenKeyEx returns ERROR_SUCCESS, but it should not (Windows 7)

I have a problem with RegOpenKeyEx, code:

#include <tchar.h>
#include <stdio.h>
#include <windows.h>

#pragma comment (lib, "Advapi32.lib")


int main () {
    TCHAR *keyName = _T("SOFTWARE\\foobar2000\\capabilities");
    HKEY key = NULL;
    if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, keyName, 0, KEY_ALL_ACCESS, &key) != ERROR_SUCCESS) {
        printf("open key failed!\n");
        return -1;
    } else {
            printf("open key success!\n");
    }

    TCHAR *value = _T("123");
    if (RegSetValueEx(key, _T("xxx"), 0, REG_SZ,
            (const BYTE *)value, sizeof(TCHAR) * (_tcslen(value) + 1)) != ERROR_SUCCESS) {
        printf("set value failed!\n");
    }
    RegCloseKey(key);
    return 0;
}

Save the code, for example reg.cpp, and in command mode:

cl reg.cpp

and I got reg.exe, ran it:

D: \ TMP> reg.exe

public key success!

But the value was not recorded in the registry.

Another weird thing: if I use visual studio to create a CLI project and paste the code into main (), RegOpenKeyEx () will return false.

The platform is Windows 7, and UAC is enabled.

+5
source share
1 answer

, . , HKLM\Software, HKEY_USERS\<User SID>_Classes\VirtualStore\Machine\Software. , . , , . , HKLM HKCU.

++/CLI, , asInvoker , HKLM.

+5

All Articles