Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a value in windows registry? (C++)

Tags:

c++

registry

I want to edit key "HKEY_LOCAL_MACHINE\Software\company name\game name\settings\value" to "1" (DWORD)

This is my code:

HKEY hkey;
 DWORD dwDisposition;
 if(RegCreateKeyEx(HKEY_LOCAL_MACHINE, TEXT("Software\\company name\\game name\\settings"), 0, NULL, 0, 0, NULL, &hkey, &dwDisposition) == ERROR_SUCCESS){
  DWORD dwType, dwSize;
  dwType = REG_DWORD;
  dwSize = sizeof(DWORD);
  DWORD rofl = 1;
  RegSetValueEx(hkey, TEXT("value"), 0, dwType, (PBYTE)&rofl, dwSize); // does not create anything
  RegCloseKey(hkey);
 }

But it doesnt do anything. RegCreateKeyEx() is the only function that actually does something: creates the "folders" in the registry only. So once again how im failing? How i can create "files" in the registry?

like image 505
Newbie Avatar asked Dec 22 '22 06:12

Newbie


1 Answers

Always check the return value of API functions. You'll see that RegSetValueEx() returns 5, access denied. You didn't ask for write permission. Fix:

  if(RegCreateKeyEx(HKEY_LOCAL_MACHINE, 
      TEXT("Software\\company name\\game name\\settings"), 
      0, NULL, 0, 
      KEY_WRITE, NULL, 
      &hkey, &dwDisposition) == ERROR_SUCCESS) {
    // etc..
  }
like image 168
Hans Passant Avatar answered Jan 04 '23 22:01

Hans Passant