RegSetValueEx静默无法写入HKLM [英] RegSetValueEx silently fails to write to HKLM

查看:252
本文介绍了RegSetValueEx静默无法写入HKLM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的C ++应用程序中写 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run ,如下:

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?

推荐答案

问题是, UAC 注册表虚拟化。因为您没有在应用程序中包含清单,所以系统会进入XP(!)兼容模式。当您在HKLM下写入注册表的受限部分时,系统会将它们重定向到HKCU下的虚拟存储。

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.

您应该添加清单您的应用程序,以便您不再虚拟化。如果你确实需要写HKLM,那么你需要在清单中指定 requireAdministrator 选项,这样你的应用程序就可以通过提升的权限来执行。

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.

可能你的下一步是花一些时间阅读我链接到上面的文档,并确保你完全理解UAC的所有影响。

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.

这篇关于RegSetValueEx静默无法写入HKLM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆