Reputation: 3807
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. I'm using windows 7 and here's my code. 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;
Upvotes: 1
Views: 2429
Reputation: 21
You must access the registry key HKEY_LOCAL_MACHINE as administrator in order to edit values. (e.g. 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.
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".
Next time you hit run, it should prompt you to open it as admin, and then it'll allow you to change the key. 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. 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!
Upvotes: 2