Setting 2 Reg Keys

I'm trying to set two registry keys with this code and it doesn't seem to be setting the CURRENT_USER one. Any help is appreciated thank you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
HKEY hKey; DWORD buffersize = 1024; DWORD dwErr = NO_ERROR;
#define BUFFER_SIZE 1024
dwErr = RegOpenKeyEx (HKEY_CURRENT_USER,"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Zones\\3",NULL,KEY_SET_VALUE|KEY_QUERY_VALUE,&hKey)
&& RegOpenKeyEx (HKEY_LOCAL_MACHINE,"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Zones\\3",NULL,KEY_SET_VALUE|KEY_QUERY_VALUE,&hKey);
if(ERROR_SUCCESS == dwErr)
{
BYTE data[BUFFER_SIZE] = {0};
DWORD dwType = REG_NONE;
dwErr = RegQueryValueExW(hKey,L"1803",NULL,&dwType,data,&buffersize);
if(ERROR_SUCCESS == dwErr && REG_DWORD == dwType && *((LPDWORD)data) != 0)	
	{
	*((LPDWORD)data) = 0; //0 is Enable 3 is Disable
	dwErr = RegSetValueExW(hKey,L"1803",0,REG_DWORD,data,sizeof(DWORD));
	}
	RegCloseKey (hKey);
SendNotifyMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0, (LPARAM)_T("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Zones\\3"));
SendNotifyMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0, (LPARAM)_T("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Zones\\3"));
					}
You're treating the return value of RegOpenKeyEx() as a boolean. Let's see what happens:

Case A: Call 1 returns ERROR_SUCCESS and call 2 returns some error, like PATH_NOT_FOUND. Numeric values are 0 and 2, if I remember correctly. Interpreting these as boolean means that ERROR_SUCCESS is interpreted as false, and PATH_NOT_FOUND is interpreted as true. And'ing these two yield false, or 0, meaning dwErr now equals ERROR_SUCCESS. But was it successful? No. Ony one of the calls was successful.

I could produce other cases, but you get the idea, I hope. It is plain wrong.

Another problem: You use the same HKEY for the two calls to RegOpenKeyEx(). That is just plain wrong too. 2 calls to RegOpenKeyEx() require two HKEY's.

Finally, to stress my first point: Check the return values of each call to RegOpenKeyEx() independently.
Thank you. I thought something was amiss! I will do it seperately. Basically, I am trying to do this if the value of key 1803 is 0 then do nothing, if it's 3 then change it to 0.
Ok I tried to run one after another and I get a couple of errors: C2086 HKEY hKey redefinition......C2374 "buffersize" redefinition and multiple initialization .....................C2374 dwErr redefinition and multiple initialization.....how can I fix these? Thanks..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
HKEY hKey; DWORD buffersize = 1024; DWORD dwErr = NO_ERROR; 
#define BUFFER_SIZE 1024
dwErr = RegOpenKeyEx (HKEY_LOCAL_MACHINE,"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Zones\\3",NULL,KEY_SET_VALUE|KEY_QUERY_VALUE,&hKey);
if(ERROR_SUCCESS == dwErr)
	{
	BYTE data[BUFFER_SIZE] = {0};
	DWORD dwType = REG_NONE;
	dwErr = RegQueryValueExW(hKey,L"1803",NULL,&dwType,data,&buffersize);
	if(ERROR_SUCCESS == dwErr && REG_DWORD == dwType && *((LPDWORD)data) != 3)
	{
	*((LPDWORD)data) = 3; //0 is Enable 3 is Disable
	dwErr = RegSetValueExW(hKey,L"1803",0,REG_DWORD,data,sizeof(DWORD));
	}
	RegCloseKey (hKey);
	SendNotifyMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0, (LPARAM)_T                        ("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Zones\\3"));
				}
////////////////////////Reg Disable Current User//////////////
HKEY hKey; DWORD buffersize = 1024; DWORD dwErr = NO_ERROR; 
#define BUFFER_SIZE 1024
dwErr = RegOpenKeyEx (HKEY_CURRENT_USER,"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Zones\\3",NULL,KEY_SET_VALUE|KEY_QUERY_VALUE,&hKey);
if(ERROR_SUCCESS == dwErr)
{
BYTE data[BUFFER_SIZE] = {0};
DWORD dwType = REG_NONE;
dwErr = RegQueryValueExW(hKey,L"1803",NULL,&dwType,data,&buffersize);
if(ERROR_SUCCESS == dwErr && REG_DWORD == dwType && *((LPDWORD)data) != 3)
{
*((LPDWORD)data) = 3; //0 is Enable 3 is Disable
dwErr = RegSetValueExW(hKey,L"1803",0,REG_DWORD,data,sizeof(DWORD));
}
RegCloseKey (hKey);
SendNotifyMessage(HWND_BROADCAST, WM_SETTINGCHANGE, 0, (LPARAM)_T("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Zones\\3"));
}
You are re-defining the variable hKey. You can reuse it, just don't re-declare it. Line 18 above re-declares it. Just get rid of "HKEY hkey" in that line. To be in the safe side (because I don't know if RegOpenKeyEx() checks the value of hKey), set hKey to NULL just before line 20.
Thank You! Much appreiated!
Topic archived. No new replies allowed.