SetRegistryKey
语法算法 2010-06-07 10:01:07 阅读219 评论0 字号:大中小 订阅
SetRegistryKey | Causes application settings to be stored in the registry instead of .INI files. |
SetRegistryKey 这个函数功能是设置MFC程序的注册表访问键,并把读写 ini 文件的成员函数映射到读写注册表。只要调用一下 SetRegistryKey 并指定注册表键值,那么下面6个成员函数,就被映射到进行注册表读取了~
WriteProfileBinary | Writes binary data to an entry in the application's .INI file. |
WriteProfileInt | Writes an integer to an entry in the application's .INI file. |
WriteProfileString | Writes a string to an entry in the application's .INI file. |
GetProfileBinary | Retrieves binary data from an entry in the application's .INI file. |
GetProfileInt | Retrieves an integer from an entry in the application's .INI file. |
GetProfileString | Retrieves a string from an entry in the application's .INI file. |
MSDN上面写上面6个函数是写到INI文件的。所以俺就忽略了其访问注册表的功能。无意中看了其MFC实现才有所了解。
例子如下:
SetRegistryKey(_T("boli's app")); //这里是准备在注册表HKEY_CURRENT_USER//software 下面生成一个boli's app 分支~为什么说是准备呢?因为如果不调用相关函数,如上面提到的6个函数,它是不会真正读写注册表的。具体本文最最下面的MFC实现摘录。
CString strUserName,strPassword;
WriteProfileString("LogInfo","UserName",strUserName); //向注册表HKEY_CURRENT_USER//software//boli's app//LogInfo//分支下写入 UserName 字符串行键值~
WriteProfileString("LogInfo","Password",strPassword);//同上~
strUserName = GetProfileString("LogInfo","UserName");// 这里是读取HKEY_CURRENT_USER//software//boli's app//LogInfo//分支下的 UserName 字符串键值到 strUserName~
strPassword = GetProfileString("LogInfo","Password");
如果不是在CWinApp 派生的类中读写注册表,可以直接用:
strUserName = theApp.GetProfileString("LogInfo","UserName");
strPassword = theApp.GetProfileString("LogInfo","Password");
或
strUserName = AfxGetApp()->GetProfileString("LogInfo","UserName");
条条大路通罗马。
CRegKey 应用:
从注册表中读取信息
CString ReadRk()
{
CRegKey rk;
DWORD dwResult = 0;
CString csRkPath;
csRkPath = _T("Software//....//....");
if (rk.Open(HKEY_CURRENT_USER, csRkPath) != ERROR_SUCCESS)
{
rk.Close();
return _T("matrix");
}
if (rk.QueryDWORDValue(_T("mainMenuStyle"), dwResult) == ERROR_SUCCESS)
{
if (dwResult == 2)
{
rk.Close();
return _T("tyle2");
}
else
{
rk.Close();
return _T("tyle1");
}
}
else
{
rk.Close();
return _T("tyle1");
}
}
向注册表中写入信息
BOOL WriteRk()
{
CRegKey rk;
DWORD dwResult = 0;
CString csRkPath;
csRkPath = _T("Software//OPPO//...//...");
if (rk.Open(HKEY_LOCAL_MACHINE, csRkPath) != ERROR_SUCCESS)
{
if(rk.Create(HKEY_LOCAL_MACHINE, csRkPath) != ERROR_SUCCESS)
{
AfxMessageBox(_T("Error creating registry"));
rk.Close();
return FALSE;
}
if (rk.SetDWORDValue(_TEXT("mainMenuStyle"), 2) != ERROR_SUCCESS )
{
AfxMessageBox(_T("Error creating registry"));
rk.Close();
return FALSE;
}
rk.Close();
}
if(!m_csIdentMenuStyle.CompareNoCase(_T("style1")))
{
if (rk.SetDWORDValue(_TEXT("mainMenuStyle"), 1)!= ERROR_SUCCESS)
{
AfxMessageBox(_T("Error writing into registry"));
rk.Close();
return FALSE;
}
}
if(!m_csIdentMenuStyle.CompareNoCase(_T("style2")))
{
if (rk.SetDWORDValue(_TEXT("mainMenuStyle"), 2)!= ERROR_SUCCESS)
{
AfxMessageBox(_T("Error writing into registry"));
rk.Close();
return FALSE;
}
}
rk.Close();
return TRUE;
}