Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,
Does any one know how to read/alter multiple registries pragmatically(VC++) with a single keyhandler.Now i am doing this in the following way[Keyhandle1 and keyhandle2 are declared globally)

C++
///GLobally Declared
HKEY Keyhandle1;
HKEY Keyhandle2;
void CReadRegistryDlg::OnRead() 
{
	// TODO: Add your control notification handler code here
	char rgValue [1024]={'\0'};
	
    DWORD size1=1024;	
    DWORD Type1;
	
//1.(keyHandle1)
	if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SOFTWARE\\VideoLAN\\VLC",0,KEY_ALL_ACCESS, &keyHandle1) == ERROR_SUCCESS)
	{
			UpdateData(true);
		   
           size1=1024;
			if(	RegQueryValueEx(keyHandle1,"Language",NULL,&Type1,(LPBYTE)rgValue,&size1) ==ERROR_SUCCESS)
			{
			SetDlgItemText(IDC_Disp1,"Language");
			SetDlgItemText(IDC_FLD1, rgValue);
			UpdateData(true);
			}
			else
			{
			SetDlgItemText(IDC_Disp1," Language  KeyNotFound!");

			}
			
	}

		else
		{
				SetDlgItemText(IDC_Disp1,"Couldn't access Language information!");
		}
//2.(keyHandle2)
		
	if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SOFTWARE\\VMware, Inc.\\VMware Drivers",0,KEY_ALL_ACCESS, &keyHandle2) == ERROR_SUCCESS)
	{
			char rgValue1 [1024]={'\0'};
			UpdateData(true);
		   
           size1=1024;
			if(	RegQueryValueEx(keyHandle2,"VmciHostDevInst",NULL,&Type1,(LPBYTE)rgValue,&size1)==ERROR_SUCCESS)
			{
			SetDlgItemText(IDC_Disp2,"VmciHostDevInst");
			SetDlgItemText(IDC_FLD2, rgValue);
			UpdateData(true);
			}
			else
			{
				SetDlgItemText(IDC_Disp2,"VmciHostDevInst KeyNotFound!");

			}
	}

	else
		{
				SetDlgItemText(IDC_Disp2,"Couldn't access VmciHostDevInst information!");
							
		}
void CReadRegistryDlg::OnSave() 
	{
		UpdateData(true);
	

	LPCSTR	data1	=	m_fld1;//taking the input from eeditbox	;
	LPCSTR	data2	=	m_fld2;//taking the input from eeditbox;
	RegSetValueEx (keyHandle1, "Language", 0, REG_SZ, (LPBYTE)data1, (strlen(data1)));
	RegSetValueEx (keyHandle2, "VmciHostDevInst", 0, REG_SZ, (LPBYTE)data2, (strlen(data2)));
	
	RegCloseKey(keyHandle1);
	RegCloseKey(keyHandle2);
	}
Posted
Updated 6-Jul-15 3:04am
v3
Comments
Richard MacCutchan 6-Jul-15 10:09am    
Just create a new function that can handle different parameters and does not use hardcoded values. This is basic C/C++, not specific to Windows.
KarstenK 6-Jul-15 10:23am    
Thats not good style. You better open and close the key after every read and write.
Usually it is one handle for every key used, so you need to open one for every value. Except subkeys.

It is really no performance issue, but you need clear code (as little globals as possible and no open handles).

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