简体   繁体   English

创建注册表项-拒绝访问

[英]Creating a registry key - access denied

Hi I am trying to create a registry key in C++ but I keep getting the error 5 which googling told me it was access denied but I don't know to do get the correct privileges. 嗨,我试图在C ++中创建一个注册表项,但我不断收到错误5,谷歌搜索告诉我它被拒绝访问,但我不知道该怎么获得正确的特权。 I'm using windows 7 and here's my code. 我正在使用Windows 7,这是我的代码。 Thanks 谢谢

HRESULT hr = S_OK;
UINT er = ERROR_SUCCESS;
LPWSTR szValueBuf = NULL;
char szProductName[MAX_PATH];
LPSECURITY_ATTRIBUTES lpsa;
HKEY hOrchKey;
DWORD dwOpenStatus,
      dwType;
char szProuductKey[MAX_PATH];

hr = WcaInitialize(hInstall, "CreateProductKey");
ExitOnFailure(hr, "Failed to initialize");

WcaLog(LOGMSG_STANDARD, "Initialized.");


if (!(lpsa = default_sa()))
    return FALSE;

hr = WcaGetProperty(L"PRODUCTNAME",&szValueBuf);
ExitOnFailure(hr, "failed to get Product Name");

wcstombs(szProductName, szValueBuf, 260);

sprintf(szProuductKey,"SOFTWARE\\Company\\%s",szProductName);

// Open the registery Orchestrator key
if (RegCreateKeyEx(HKEY_LOCAL_MACHINE,
                szProuductKey,
                0,
                "",
                REG_OPTION_NON_VOLATILE,
                KEY_QUERY_VALUE,
                lpsa,
                &hOrchKey,
                &dwOpenStatus) != ERROR_SUCCESS )
    return FALSE;   

OS_RegCloseKey(hOrchKey);
return TRUE;

以管理员身份运行,以具有更高的访问权限运行它。

You must access the registry key HKEY_LOCAL_MACHINE as administrator in order to edit values. 您必须以管理员身份访问注册表项HKEY_LOCAL_MACHINE,才能编辑值。 (eg If you wanted to edit the key via the Registry Editor application, then you would have to right click and select "run as administrator") Since you want to write the values in code, so you need to set your compiler to have administrator rights when you run it. (例如,如果您想通过注册表编辑器应用程序来编辑密钥,则必须右键单击并选择“以管理员身份运行”)。由于要在代码中写入值,因此需要将编译器设置为具有管理员权限运行时的权利。 In Visual Studio 2008 this can be done in the Properties Page of your solution, you set it to run as admin. 在Visual Studio 2008中,可以在解决方案的“属性”页中完成此操作,将其设置为以管理员身份运行。

Heres how to do it; 这是怎么做的; Right click your solution in the Solution Explorer and select Properties; 在解决方案资源管理器中右键单击您的解决方案,然后选择属性; Go to Configuration Properties->Linker->Manifest File; 转到配置属性->链接器->清单文件; Set UAC Execution Level as "requireAdministrator". 将UAC执行级别设置为“ requireAdministrator”。

Next time you hit run, it should prompt you to open it as admin, and then it'll allow you to change the key. 下次按下run时,它会提示您以admin身份打开它,然后它将允许您更改密钥。 I'm not sure how to do this with other compilers, but it should be relatively the same. 我不确定如何使用其他编译器执行此操作,但是应该相对相同。 However, it will always ask you for admin rights, even in the release, not ideal for most programs. 但是,即使在发行版中,它总是会要求您提供管理员权限,因此对于大多数程序而言并不理想。 If this is an installer or something, then Id say that'd be fine, but if this is an app that'll be run a lot, Id suggest using HKEY_LOCAL_USER, it doesn't require admin rights. 如果这是安装程序或其他工具,则ID表示可以,但是如果此应用可以运行很多,则ID建议使用HKEY_LOCAL_USER,它不需要管理员权限。 I recently went through all that malarkey and the registry is a bitch to get right, so I'd suggest avoiding it as much as possible! 我最近经历了所有的恶意事件,注册表是个正确的选择,所以我建议尽可能避免使用它!

Hope that helps! 希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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