Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.

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

1 solution

C++
	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?
 
Share this answer
 
Comments
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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900