Click here to Skip to main content
14,641,793 members
Rate this:
Please Sign up or sign in to vote.
See more:
I'm running this code to change registry key. However; I want to add an if statement to it that query the key before the change is made. Let's say I want to change the key to "0", I would query the key and if it was "0" then the code would stop, If it's a "3" then it would continue. How can this be done with this working code that I have? Thank you.

#define BUFFER_SIZE 1024 //add this to your header		
HKEY hKey;			
dwErr = RegOpenKeyEx (HKEY_CURRENT_USER,L"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};
	dwErr = RegQueryValueEx(hKey,L"1803",NULL,&dwType,data,&buffersize);
	if(ERROR_SUCCESS == dwErr && REG_DWORD == dwType)
	//If Key "1803" Value = 0 then
		*((LPDWORD)data) = 0;
		dwErr = RegSetValueExW(hKey,L"1803",0,REG_DWORD,data,sizeof(DWORD));
	RegCloseKey (hKey);

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

	if(ERROR_SUCCESS == dwErr && REG_DWORD == dwType && *((LPDWORD)data) != 0)

Why not also make data a DWORD type since you know it contains a DWORD value?
Member 7766180 19-Sep-11 15:21pm
Thank you, Richard I appreciate the input. I will change data to DWORD on your recommendation. Have a nice day!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100