Click here to Skip to main content
15,888,143 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have one check box and two command buttons set and get

I want to create new key in registry i.e. MyRegDemo

then on click of set button I want to create a DWORD value in that key and save the value either 0 or 1

and on click of get button I want to get the DWORD value from registry and set check box
checked or unchecked accordingly

and also suggest me for 0 and 1 value DWORD is best or not. Actually my main motive is saving the state of check box on application close and get state while application open if any suggestion for that please tell that also. But first I want to solve this one.

Thanks!!!

What I have tried:

void CRegistryDemoDlg::OnBnClickedSet()
{
	// TODO: Add your control notification handler code here
	CRegKey reg;

	HKEY m_hKeyParent=HKEY_CURRENT_USER;

	LPCTSTR m_myKey=L"\\Software\\MyRegDemo";

	reg.Create(m_hKeyParent,m_myKey);

	if(reg.Open(m_hKeyParent,m_myKey)==ERROR_SUCCESS)
	{
		DWORD data=1;
		reg.SetDWORDValue(L"Test",data);
		reg.Close();
	}
}

void CRegistryDemoDlg::OnBnClickedGet()
{
	// TODO: Add your control notification handler code here
	CRegKey reg;
	HKEY m_hkeyParent=HKEY_CURRENT_USER;
	LPCTSTR m_myKey=L"\\Software\\MyRegDemo";
	DWORD dvalue;
	if(reg.Open(m_hkeyParent,m_myKey)==ERROR_SUCCESS)
	{
		reg.QueryDWORDValue(L"Test",dvalue);
		reg.Close();
	}
	if(dvalue==1)
		m_chkbox1.SetCheck(true);
	else
		m_chkbox1.SetCheck(false);
}
Posted
Updated 28-Feb-17 21:56pm

It would be good to know what is not working.

First, read the documentation of the used functions.

Then check the return value of each function call and report the error code (at least in debug builds to be detected during development):
LONG err = reg.Create(m_hKeyParent,m_myKey);
if (ERROR_SUCCESS != err)
{
    // Report error here
}


I have not tested the code but assume that this fails with an invalid handle error. See CRegKey::Create[^]. The hKeyParent parameter muste be a handle of an open key.

But there is no need to create the key. Just use Open() because it will create the key if it does note exist yet:
// Open key (creates it if it does not exist yet)
LONG err = reg.Open(HKEY_CURRENT_USER, L"Software\\MyRegDemo");
if (ERROR_SUCCESS != err)
{
    // Report error here
}
else
{
    reg.Close();
}


When reading a boolean value stored in a DWORD, a zero value should be interpreted as FALSE and non-zero as TRUE. It is common to write a '1' for TRUE but some write also the value with all bits set.

So you should always check for zero / non-zero when reading such values:
DWORD dvalue = 0; // Default
// ...
m_chkbox1.SetCheck(dvalue != 0);

Note that I have initialised dvalue in the above example. While this should be always done, it is important here when your program is executed the first time because the value does not exist yet (it is created when leaving your program).
 
Share this answer
 
Comments
Premnath Mali 1-Mar-17 3:55am    
I made a typo mistake while creating key I used L"\\Software\\MyRegDemo" instead of L"Software\\MyRegDemo" if I removed the slashes then my code is also running successfully but the other information is also useful for me.

Thanks for that!!!
void CRegistryDemoDlg::OnBnClickedSet()
{
	// TODO: Add your control notification handler code here
	CRegKey reg;

	HKEY m_hKeyParent=HKEY_CURRENT_USER;

//Here I Was wrong while giving path
	LPCTSTR m_myKey=L"Software\\MyRegDemo";

	reg.Create(m_hKeyParent,m_myKey);

	if(reg.Open(m_hKeyParent,m_myKey)==ERROR_SUCCESS)
	{
		DWORD data=1;
		reg.SetDWORDValue(L"Test",data);
		reg.Close();
	}
}

void CRegistryDemoDlg::OnBnClickedGet()
{
	// TODO: Add your control notification handler code here
	CRegKey reg;
	HKEY m_hkeyParent=HKEY_CURRENT_USER;

//Here I Was wrong while giving path
	LPCTSTR m_myKey=L"Software\\MyRegDemo";
	DWORD dvalue;
	if(reg.Open(m_hkeyParent,m_myKey)==ERROR_SUCCESS)
	{
		reg.QueryDWORDValue(L"Test",dvalue);
		reg.Close();
	}
	if(dvalue==1)
		m_chkbox1.SetCheck(true);
	else
		m_chkbox1.SetCheck(false);
}
 
Share this answer
 

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