1. 程式人生 > >MFC 登錄檔操作

MFC 登錄檔操作

BOOL CWinApp::WriteProfileString(LPCTSTR lpszSection, LPCTSTR lpszEntry,
   LPCTSTR lpszValue)
{
 ASSERT(lpszSection != NULL);
 if (m_pszRegistryKey != NULL)
 {
  LONG lResult;
  if (lpszEntry == NULL) //delete whole section
  {
   HKEY hAppKey = GetAppRegistryKey();
   if (hAppKey == NULL)
    return FALSE;
   lResult = ::RegDeleteKey(hAppKey, lpszSection);
   RegCloseKey(hAppKey);
  }
  else if (lpszValue == NULL)
  {
   HKEY hSecKey = GetSectionKey(lpszSection);
   if (hSecKey == NULL)
    return FALSE;
   // necessary to cast away const below
   lResult = ::RegDeleteValue(hSecKey, (LPTSTR)lpszEntry);
   RegCloseKey(hSecKey);
  }
  else
  {
   HKEY hSecKey = GetSectionKey(lpszSection);
   if (hSecKey == NULL)
    return FALSE;
   lResult = RegSetValueEx(hSecKey, lpszEntry, NULL, REG_SZ,
    (LPBYTE)lpszValue, (lstrlen(lpszValue)+1)*sizeof(TCHAR));
   RegCloseKey(hSecKey);
  }
  return lResult == ERROR_SUCCESS;
 }
 else
 {
  ASSERT(m_pszProfileName != NULL);
  ASSERT(lstrlen(m_pszProfileName) < 4095); // can't read in bigger
  return ::WritePrivateProfileString(lpszSection, lpszEntry, lpszValue,
   m_pszProfileName);
 }
}