首页 > 代码库 > MFC常见函数详解:SetRegistryKey
MFC常见函数详解:SetRegistryKey
在利用mfc框架的时候,在App应用类的InitInstance()函数中,初始化时总有一个 SetRegistryKey("字符串XXX"),不知道究竟有何用处,这天仔细查看了一下,发现如果你使用注册表,则它为你提供了很便利的方法,当然如果不使用系统注册表,这句可以注释掉。
来自官方的解释:
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;
//向注册表HKEY_CURRENT_USER\\software\\boli‘s app\\LogInfo\\分支下写入 UserName 字符串行键值~
WriteProfileString("LogInfo","UserName",strUserName);
//同上~
WriteProfileString("LogInfo","Password",strPassword);
// 这里是读取HKEY_CURRENT_USER\\software\\boli‘s app\\LogInfo\\分支下的 UserName 字符串键值到 strUserName~
strUserName = GetProfileString("LogInfo","UserName");
strPassword = GetProfileString("LogInfo","Password");
如果不是在CWinApp 派生的类中读写注册表,可以直接用:
strUserName = theApp.GetProfileString("LogInfo","UserName");
strPassword = theApp.GetProfileString("LogInfo","Password");
或
strUserName = AfxGetApp()->GetProfileString("LogInfo","UserName");
条条大路通罗马。
下列是mfc实现的代码: //////////////////////////////////////////////////////////////////////////// // CWinApp Settings Helpers #ifdef AFX_INIT_SEG #pragma code_seg(AFX_INIT_SEG) #endif void CWinApp::SetRegistryKey(LPCTSTR lpszRegistryKey) { ASSERT(m_pszRegistryKey == NULL); ASSERT(lpszRegistryKey != NULL); ASSERT(m_pszAppName != NULL); BOOL bEnable = AfxEnableMemoryTracking(FALSE); free((void*)m_pszRegistryKey); m_pszRegistryKey = _tcsdup(lpszRegistryKey); free((void*)m_pszProfileName); m_pszProfileName = _tcsdup(m_pszAppName); AfxEnableMemoryTracking(bEnable); } void CWinApp::SetRegistryKey(UINT nIDRegistryKey) { ASSERT(m_pszRegistryKey == NULL); TCHAR szRegistryKey[256]; VERIFY(AfxLoadString(nIDRegistryKey, szRegistryKey)); SetRegistryKey(szRegistryKey); } // returns key for HKEY_CURRENT_USER\"Software"\RegistryKey\ProfileName // creating it if it doesn't exist // responsibility of the caller to call RegCloseKey() on the returned HKEY HKEY CWinApp::GetAppRegistryKey() { ASSERT(m_pszRegistryKey != NULL); ASSERT(m_pszProfileName != NULL); HKEY hAppKey = NULL; HKEY hSoftKey = NULL; HKEY hCompanyKey = NULL; if (RegOpenKeyEx(HKEY_CURRENT_USER, _T("software"), 0, KEY_WRITE|KEY_READ, &hSoftKey) == ERROR_SUCCESS) { DWORD dw; if (RegCreateKeyEx(hSoftKey, m_pszRegistryKey, 0, REG_NONE, REG_OPTION_NON_VOLATILE, KEY_WRITE|KEY_READ, NULL, &hCompanyKey, &dw) == ERROR_SUCCESS) { RegCreateKeyEx(hCompanyKey, m_pszProfileName, 0, REG_NONE, REG_OPTION_NON_VOLATILE, KEY_WRITE|KEY_READ, NULL, &hAppKey, &dw); } } if (hSoftKey != NULL) RegCloseKey(hSoftKey); if (hCompanyKey != NULL) RegCloseKey(hCompanyKey); return hAppKey; } // returns key for: // HKEY_CURRENT_USER\"Software"\RegistryKey\AppName\lpszSection // creating it if it doesn't exist. // responsibility of the caller to call RegCloseKey() on the returned HKEY HKEY CWinApp::GetSectionKey(LPCTSTR lpszSection) { ASSERT(lpszSection != NULL); HKEY hSectionKey = NULL; HKEY hAppKey = GetAppRegistryKey(); if (hAppKey == NULL) return NULL; DWORD dw; RegCreateKeyEx(hAppKey, lpszSection, 0, REG_NONE, REG_OPTION_NON_VOLATILE, KEY_WRITE|KEY_READ, NULL, &hSectionKey, &dw); RegCloseKey(hAppKey); return hSectionKey; } UINT CWinApp::GetProfileInt(LPCTSTR lpszSection, LPCTSTR lpszEntry, int nDefault) { ASSERT(lpszSection != NULL); ASSERT(lpszEntry != NULL); if (m_pszRegistryKey != NULL) // use registry { HKEY hSecKey = GetSectionKey(lpszSection); if (hSecKey == NULL) return nDefault; DWORD dwValue; DWORD dwType; DWORD dwCount = sizeof(DWORD); LONG lResult = RegQueryValueEx(hSecKey, (LPTSTR)lpszEntry, NULL, &dwType, (LPBYTE)&dwValue, &dwCount); RegCloseKey(hSecKey); if (lResult == ERROR_SUCCESS) { ASSERT(dwType == REG_DWORD); ASSERT(dwCount == sizeof(dwValue)); return (UINT)dwValue; } return nDefault; } else { ASSERT(m_pszProfileName != NULL); return ::GetPrivateProfileInt(lpszSection, lpszEntry, nDefault, m_pszProfileName); } } CString CWinApp::GetProfileString(LPCTSTR lpszSection, LPCTSTR lpszEntry, LPCTSTR lpszDefault) { ASSERT(lpszSection != NULL); ASSERT(lpszEntry != NULL); if (m_pszRegistryKey != NULL) { HKEY hSecKey = GetSectionKey(lpszSection); if (hSecKey == NULL) return lpszDefault; CString strValue; DWORD dwType, dwCount; LONG lResult = RegQueryValueEx(hSecKey, (LPTSTR)lpszEntry, NULL, &dwType, NULL, &dwCount); if (lResult == ERROR_SUCCESS) { ASSERT(dwType == REG_SZ); lResult = RegQueryValueEx(hSecKey, (LPTSTR)lpszEntry, NULL, &dwType, (LPBYTE)strValue.GetBuffer(dwCount/sizeof(TCHAR)), &dwCount); strValue.ReleaseBuffer(); } RegCloseKey(hSecKey); if (lResult == ERROR_SUCCESS) { ASSERT(dwType == REG_SZ); return strValue; } return lpszDefault; } else { ASSERT(m_pszProfileName != NULL); if (lpszDefault == NULL) lpszDefault = _T(""); // don't pass in NULL TCHAR szT[4096]; DWORD dw = ::GetPrivateProfileString(lpszSection, lpszEntry, lpszDefault, szT, _countof(szT), m_pszProfileName); ASSERT(dw < 4095); return szT; } } BOOL CWinApp::GetProfileBinary(LPCTSTR lpszSection, LPCTSTR lpszEntry, BYTE** ppData, UINT* pBytes) { ASSERT(lpszSection != NULL); ASSERT(lpszEntry != NULL); ASSERT(ppData != NULL); ASSERT(pBytes != NULL); *ppData = http://www.mamicode.com/NULL;>
MFC常见函数详解:SetRegistryKey