Change Registry Value

Sep 11, 2011 at 7:53pm
I'm trying to chage the value in the registry from 0 to 3, but this code isn;t working. It compiles but doesn't change the value.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "stdafx.h"
#include <windows.h>
#include <WinReg.h>

#pragma comment(lib,"Advapi32.lib")

int main () {
	HKEY hKey;			
	DWORD buffersize = 1024;
	char* lpData = new char[buffersize];
	
if(ERROR_SUCCESS == RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Zones\\3", 0, 0, &hKey)) 
{ 
    if(RegSetValueExW(HKEY_LOCAL_MACHINE, _T("1803"), 3, REG_DWORD, (LPBYTE)&lpData, sizeof(DWORD))) 
    { 
        RegCloseKey (hKey); 
        return FALSE; 
    } 
 
    RegCloseKey (hKey); 
} 
}
Sep 11, 2011 at 8:45pm
You're opening a key for write access under HKLM (or so you think you are; see next paragraph). This requires elevation. Are you running elevated?

You are calling RegOpenKeyExW() but you are not stating the type of access that you want. This is important: If you want read access, the request can be granted even if you're not running elevated, but if you want write access you need elevation. Passing zero is not giving you a useful handle, if it is giving you one at all! Since you want to set a key value, pass KEY_SET_VALUE to the second-to-last parameter of RegOpenKeyExW().

Another error: You are passing HKEY_LOCAL_MACHINE to RegSetValueExW() when you need to be passing hKey instead (assuming the call to RegOpenKeyExW() succeeds with the right desired access). Also your use of the _T() macro is incorrect because you are explicitly calling for the Unicode version of RegSetValueEx.

Another error: The 3rd parameter to RegSetValueExW() is reserved and must be zero; your code shows it is 3.

Side note: Your buffer size is 1KB, but all you need is 4 bytes. consider changing it to:

1
2
3
4
5
DWORD data = <desired value goes here>;

...
if (RegSetValueExW(hKey, L"1083", 0, REG_DWORD, (LPBYTE)&data, sizeof(data)))
...


Another error: You pass &lpData when you should have passed lpData, and I don't see anywhere in your code where you initialize the lpData buffer, pressumably with the value you want to write.

After reading the whole thing, I think you are missing a memset() call and I guess your desired value is 3.

EDIT: One more thing. Since this was a question that can only pertain MS Windows, I recommend posting this and other related questions in the Windows Programming forum, not the General C++ forum or any other forum.
Last edited on Sep 11, 2011 at 8:46pm
Sep 11, 2011 at 10:53pm
Thank you. First I am doing c++ exclusivly in Windows so I should post where you suggest...correct!
Second, not sure what you mean about elevated? Third than you so very much for your help it is greatly appreciated!!!!! Here is what I've done, I hope its right, but it's still not changing the value to 3.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "stdafx.h"
#include <windows.h>
#include <WinReg.h>
#include <iostream>
#pragma comment(lib,"Advapi32.lib")
using namespace std;
int main () {
	HKEY hKey;			
	DWORD buffersize = 4;
	DWORD data = 3;
	
if(ERROR_SUCCESS == RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Zones\\3", 0, KEY_SET_VALUE, &hKey)) 
{ 
    if (RegSetValueExW(hKey, L"1083", 0, REG_DWORD, (LPBYTE)&data, sizeof(data)))
    { 
        RegCloseKey (hKey); 
        return FALSE; 
    } 

    RegCloseKey (hKey); 
} 
}

Sep 12, 2011 at 1:13am
That looks better, yes. If your value is not changing it could be because you don't have rights to write the value in the registry. If you don't know what elevation is, you need to study this. In a nutshell, an application never runs with administrative rights, even if run under an administrator account (in Windows Vista or Windows 7; not the case in Windows XP). If you really need/want administrator rights, the application (executable) needs to be run elevated. Your code writes to the local machine's registry hive. This hive is read-only unless you log in as administrator and the executable is run elevated (if in Windows Vista or 7). Since you don't know what elevation is, I am guessing the code is not running elevated, and that is most likely the problem here.
Sep 12, 2011 at 1:53am
I'm running the code with Admin rights. Anything else that I can do to make this work?
Sep 12, 2011 at 2:25am
Add else statements that pick up the return code of the functions RegOpenKeyExW() and RegSetValueExW(). These functions return the error code, so no need to call GetLastError(). Pick up the error codes and debug. If you want the error message, use FormatMessage() with the error code returned by the functions.
Sep 12, 2011 at 2:38am
Do I need this as well?
RegQueryValueEx
Sep 12, 2011 at 3:50am
Huh?? Why do you need that? I mean, you can verify if the value changes using the registry editor, am I right? But I guess you can query for the value if you so choose to (you need to add the appropriate read access right request in the call for RegOpenKeyExW()).

Just debug your program and collect the return values of the registry functions. Then optionally get the corresponding error messages and go from there.
Topic archived. No new replies allowed.