Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I'm trying to set two registry keys at once without running this code twice. How can I do this? Thanks!

C++
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);
fwErr = 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; //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"));
	}
Posted
Updated 20-Sep-11 13:34pm
v3
Comments
Philippe Mori 20-Sep-11 20:30pm    
Using space character and new line more often in your code would improve readability...

By the way the are some \ that are not properly doubled...
Member 7766180 20-Sep-11 21:21pm    
Thank you Phillipe. I fixed that. "/". I will also take your suggestion.

You need to master such a basic method of abstraction as "function with parameters". The registry keys and a values to set should be passed as parameters to the code.

The fact you show this code with access to OS API and immediate constants hard-coded in this fragment (which is really bad in general) but without showing a single function looks disturbing. It looks like you develop your programming skills in a very disproportional manner. You're getting deep enough into Windows API without basic understanding of how to write code. Are you really in such rush? Then the usefulness of your activity is questionable.

You claim you're using C++, but in fact not only you're not using OOP, you need to get familiar with the basics of programming which go well before OOP and other advanced topics. I would advise to go along the full course of C++ and using of basic language features in practice, without skipping of any topics, before you start any program using OS APIs, UI and the like.

—SA
 
Share this answer
 
Comments
Member 7766180 19-Sep-11 17:19pm    
Thank you for your advice. Now that this is out of the way and I fully understand you, can you be kind enough to show me how I would do this?
Sergey Alexandrovich Kryukov 26-Sep-11 13:08pm    
How can I show that if you did not write a method. OK, make your code sample into method.
Here are your immediate constants:
"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Zones\\3", "1803" or whatever else.
Make them parameters of the method -- the above parameters are of the string type. Call the method with appropriate parameters.

Can I hope you understand what is "method" and "parameter"?
-SA
Member 7766180 26-Sep-11 13:35pm    
Thank you. This is now a dead issue because I can't get IE9 to reconize the changes even though I can clearly see the changes. IE9 needs to be restarted and that does me no good.
Sergey Alexandrovich Kryukov 26-Sep-11 17:04pm    
I don't understand what are you talking about and how this is related. You really need to learn elementary basics of programming before even trying to address this problem, otherwise it will hit you again and again.
--SA
Does this work?
C++
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"));
}
 
Share this answer
 
Comments
Member 7766180 20-Sep-11 15:21pm    
Is there a way to print out the current regkey value as the code is running so that I can see what the current setting is, just as a trouble shooter?
Member 7766180 20-Sep-11 19:32pm    
No doesn't work. Local Machine Key changes but Current User Key Doesn't

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