Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: IE9 Registry.
I'm trying to set two registry keys at once without running this code twice. How can I do this? Thanks!
 
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 18-Sep-11 10:11am
Edited 20-Sep-11 14:34pm
v3
Comments
Philippe Mori at 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 at 20-Sep-11 21:21pm
   
Thank you Phillipe. I fixed that. "/". I will also take your suggestion.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
Comments
Member 7766180 at 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?
SAKryukov at 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 at 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.
SAKryukov at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Does this work?
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"));
}
  Permalink  
Comments
Member 7766180 at 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 at 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)

  Print Answers RSS
0 BillWoodruff 330
1 Mathew Soji 309
2 Maciej Los 295
3 DamithSL 225
4 Afzaal Ahmad Zeeshan 217
0 OriginalGriff 6,369
1 Sergey Alexandrovich Kryukov 5,973
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 4,025


Advertise | Privacy | Mobile
Web01 | 2.8.1411019.1 | Last Updated 26 Sep 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

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