简体   繁体   中英

RegSetValueEx silently fails to write to HKLM

I'm trying to write in HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run from my C++ application like this:

HKEY key;
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"), NULL, KEY_ALL_ACCESS, &key) == ERROR_SUCCESS){
    if (RegSetValueEx(key, TEXT("lcr"), 0, REG_SZ, (const BYTE*)runcmd.c_str(), (runcmd.size()+1)*sizeof(wchar_t)) != ERROR_SUCCESS){
        cout << "ERROR" ;
    }else{
        cout << "OK" << endl;
    }
    RegCloseKey(key);
}else{
    cout << "ERROR" ;
}

But it silently fails and nothing happens!
I tried both running as normal user & running as administrator.
What is the problem?

The problem is that your application is subject to UAC registry virtualization . Because you did not include a manifest in your application, the system drops into an XP (!) compatibility mode. When you write to restricted parts of the registry under HKLM, the system re-directs them to what is known as the virtual store under HKCU.

You should add a manifest to your application so that you are no longer virtualized. If you really do need to write to HKLM then you will need to specify the requireAdministrator option in the manifest so that your application is executed with elevated rights.

Probably your next move is to take some time to read the documentation that I linked to above and be sure that you fully understand all the implications of UAC.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM