1

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; 
6
  • are you an administrator on the pc you're working on? Commented Oct 25, 2012 at 14:30
  • I'm running this as a custom action through an MSI. This MSI is not elevated at the particular point the custom action is called but I am an administrator. Commented Oct 25, 2012 at 14:32
  • did you try using KEY_SET_VALUE instead of KEY_QUERY_VALUE? Commented Oct 25, 2012 at 14:38
  • 1
    Otherwise something is wrong with your access rights, try using HKEY_LOCAL_USER instead if this is at all a viable option for you. If that works for you then chances are your problem is you don't have administrator rights. Commented Oct 25, 2012 at 14:45
  • 2
    You've already answered your own question: you need to be elevated to access administrator functions: setting HKLM registry keys. Commented Oct 25, 2012 at 15:05

2 Answers 2

3

Run as administrator to run it with elevated access.

Sign up to request clarification or add additional context in comments.

3 Comments

Muddled up a bit: an Administrator without elevated rights does not have Administrator permissions. NC1 already states the installer is executed as an Administrator.
"This MSI is not elevated at the particular point the custom action is called but I am an administrator" From what I get he isn't executing as an admin, just that he has the rights, you must explicitly choose to elevate a process in win vista/7.
@swabs you are right, I did not realize this. Thanks..:) P.s your he is a she..lol :)
2

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!

1 Comment

Hi Thanks for trying to help but I have those settings and it did not help. It was a problem with my MSI. Gonna try a work around..:)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.