如何使用mfc创建,保存和检索DWORD值到注册表 [英] How to create, save and retrieve DWORD value to registry using mfc

查看:153
本文介绍了如何使用mfc创建,保存和检索DWORD值到注册表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个复选框和两个命令按钮设置并获得



我想在注册表中创建新密钥,即MyRegDemo



然后单击设置按钮我想在该键中创建一个DWORD值并保存值0或1



和点击获取按钮我想从注册表中获取DWORD值并设置复选框

相应地选中或取消选中



并建议我为0和1值DWORD是最好的与否。实际上我的主要动机是在应用程序关闭时保存复选框的状态并在应用程序打开时获取状态,如果有任何建议也请告诉我。但首先我要解决这个问题。



谢谢!!!



我是什么尝试过:



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);
}

推荐答案

知道什么不起作用会很好。



首先,阅读已使用函数的文档。



然后检查每个函数调用的返回值并报告错误代码(至少在调试版本在开发期间检测到):

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
}





我没有测试过代码但是假设这个失败并且句柄错误无效。请参阅 CRegKey :: Create [ ^ ]。 hKeyParent 参数muste是 open 键的句柄。



但是没有必要创建密钥。只需使用 Open(),因为如果它确实存在,它将创建密钥:



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();
}





当读取存储在DWORD中的布尔值时,零值应解释为 FALSE 并且非零为 TRUE 。通常为 TRUE 写一个'1',但是有些也写了所有位设置的值。



因此,在读取这些值时应始终检查零/非零:



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);



请注意,我已初始化<$ c $上例中的c> dvalue 。虽然这应该总是这样做,但是在第一次执行程序时这很重要,因为该值尚不存在(它是在离开程序时创建的)。


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).


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);
}


这篇关于如何使用mfc创建,保存和检索DWORD值到注册表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆