How to Use Regcreatekeyex?
I Have a Question About Using Regcreatekeyex in C++. When I'm Trying to Create a Key Under Hklm - It Does Not Work, but Other Situation(Creating the Key at the...
I have a question about using RegCreateKeyEx in C++.
When I'm trying to create a key under HKLM - it does not work,
but other situation(creating the key at the HKCU) - it does work well (can create key).
My OS is win7 x86 - does it matter when using RegCreateKeyEx?
The code I'm running is the following:
#include<Windows.h>
#include<stdio.h>
int main()
{
HKEY hKey;
LONG result = 0;
char filename[] = "C:\test.jpg";
char *path = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\\test";
if (RegCreateKeyEx(HKEY_LOCAL_MACHINE, path, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL) == ERROR_SUCCESS)
printf("1. success \n"); // ← doesn`t work
else printf("fail\n");
if (RegCreateKeyEx(HKEY_CURRENT_USER, "Console\papadaks", 0, NULL,
REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL) == ERROR_SUCCESS)
printf("2. success \n"); // ← works well
else printf("fail\n");
return 0;
}
Thanks.
1 Answer
You will almost certainly be getting error 5 (access denied), because even if you are an administrator on that machine, for NT6 variants under UAC you would need to be elevated. Just because you're logged in under an admin account doesn't mean you have actual admin rights, thanks to the wonder that is UAC. This is actually a good thing.
You could test this hypothesis by temporarily modifying the security on the HKLM registry key you're creating under to allow everyone write capability (remember to set it back afterwards). Your code would then work... and your system would be slightly less secure.
The appropriate approach is to give your app a manifest that marks it as requiring elevation, so the user either has to acknowledge that the change is permitted (if they're an admin) or go and find someone actually is an admin to enter the password.