分类: C/C++
2010-05-24 13:52:43
用CRegKey类来操作注册表是非常方便的。CRegKey类并不是一个MFC类,而是一个ATL类,所以在使用的时候不要忘记在StdAfx.h头文件中加入#include
// 获取注册表中整数值
CRegKey rk;
DWORD dwValue;
CString strTemp;
LPCTSTR lpszKeyName = _T ("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer");
if (rk.Open (HKEY_CURRENT_USER, lpszKeyName) == ERROR_SUCCESS)
{
TRACE0 ("Open Success.\n");
if (rk.QueryValue (dwValue, _T ("Browse For Folder Height")) == ERROR_SUCCESS)
{
TRACE0 ("Query Success.\n");
strTemp.Format ("%0X", dwValue);
SetDlgItemText (IDC_EDIT1, strTemp);
}
}
rk.Close();
// 获取注册表中字符串值
CRegKey rk;
DWORD dwCount = 1024;
CString strTemp;
TCHAR szValue [1024];
LPCTSTR lpszKeyName = _T ("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer");
if (rk.Open (HKEY_CURRENT_USER, lpszKeyName) == ERROR_SUCCESS)
{
TRACE0 ("Open Success.\n");
if (rk.QueryValue (szValue, _T ("Logon User Name"), &dwCount) == ERROR_SUCCESS)
{
TRACE0 ("Query Success.\n");
strTemp = szValue;
SetDlgItemText (IDC_EDIT1, strTemp);
}
}
rk.Close();
// 设置注册表中字符串值
CRegKey rk;
LPCTSTR lpszKeyName = _T ("Software\\JueYuanLongStudio\\Test");
// Create能在Open失败的情况下自动尝试建立
if (rk.Create (HKEY_CURRENT_USER, lpszKeyName) == ERROR_SUCCESS)
{
TRACE0 ("Open Success.\n");
if (rk.SetValue (_T ("palagon"), _T ("Logon User Name")) == ERROR_SUCCESS)
{
TRACE0 ("SetValue Success.\n");
}
}
rk.Close();
// 设置注册表中数据值
CRegKey rk;
LPCTSTR lpszKeyName = _T ("Software\\JueYuanLong Studio\\Test");
// Create能在Open失败的情况下自动尝试建立
if (rk.Create (HKEY_CURRENT_USER, lpszKeyName) == ERROR_SUCCESS)
{
TRACE0 ("Open Success.\n");
if (rk.SetValue (110, _T ("Logon User Data")) == ERROR_SUCCESS)
{
TRACE0 ("SetValue Success.\n");
}
}
rk.Close();
// 删除注册表中键值
CRegKey rk;
LPCTSTR lpszKeyName = _T ("Software\\JueYuanLong Studio\\Test");
// Create能在Open失败的情况下自动尝试建立
if (rk.Create (HKEY_CURRENT_USER, lpszKeyName) == ERROR_SUCCESS)
{
TRACE0 ("Open Success.\n");
if (rk.DeleteValue (_T ("Logon User Data")) == ERROR_SUCCESS)
{
TRACE0 ("DeleteValue Success.\n");
}
}
rk.Close();