home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / mfc / src / appui3.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-06-16  |  8.8 KB  |  346 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1998 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10.  
  11. #include "stdafx.h"
  12.  
  13. #ifdef AFX_INIT_SEG
  14. #pragma code_seg(AFX_INIT_SEG)
  15. #endif
  16.  
  17. #ifdef _DEBUG
  18. #undef THIS_FILE
  19. static char THIS_FILE[] = __FILE__;
  20. #endif
  21.  
  22. /////////////////////////////////////////////////////////////////////////////
  23. // CWinApp Settings Helpers
  24.  
  25. #ifdef AFX_INIT_SEG
  26. #pragma code_seg(AFX_INIT_SEG)
  27. #endif
  28.  
  29. void CWinApp::SetRegistryKey(LPCTSTR lpszRegistryKey)
  30. {
  31.     ASSERT(m_pszRegistryKey == NULL);
  32.     ASSERT(lpszRegistryKey != NULL);
  33.     ASSERT(m_pszAppName != NULL);
  34.  
  35.     BOOL bEnable = AfxEnableMemoryTracking(FALSE);
  36.     free((void*)m_pszRegistryKey);
  37.     m_pszRegistryKey = _tcsdup(lpszRegistryKey);
  38.     free((void*)m_pszProfileName);
  39.     m_pszProfileName = _tcsdup(m_pszAppName);
  40.     AfxEnableMemoryTracking(bEnable);
  41. }
  42.  
  43. void CWinApp::SetRegistryKey(UINT nIDRegistryKey)
  44. {
  45.     ASSERT(m_pszRegistryKey == NULL);
  46.  
  47.     TCHAR szRegistryKey[256];
  48.     VERIFY(AfxLoadString(nIDRegistryKey, szRegistryKey));
  49.     SetRegistryKey(szRegistryKey);
  50. }
  51.  
  52. // returns key for HKEY_CURRENT_USER\"Software"\RegistryKey\ProfileName
  53. // creating it if it doesn't exist
  54. // responsibility of the caller to call RegCloseKey() on the returned HKEY
  55. HKEY CWinApp::GetAppRegistryKey()
  56. {
  57.     ASSERT(m_pszRegistryKey != NULL);
  58.     ASSERT(m_pszProfileName != NULL);
  59.  
  60.     HKEY hAppKey = NULL;
  61.     HKEY hSoftKey = NULL;
  62.     HKEY hCompanyKey = NULL;
  63.     if (RegOpenKeyEx(HKEY_CURRENT_USER, _T("software"), 0, KEY_WRITE|KEY_READ,
  64.         &hSoftKey) == ERROR_SUCCESS)
  65.     {
  66.         DWORD dw;
  67.         if (RegCreateKeyEx(hSoftKey, m_pszRegistryKey, 0, REG_NONE,
  68.             REG_OPTION_NON_VOLATILE, KEY_WRITE|KEY_READ, NULL,
  69.             &hCompanyKey, &dw) == ERROR_SUCCESS)
  70.         {
  71.             RegCreateKeyEx(hCompanyKey, m_pszProfileName, 0, REG_NONE,
  72.                 REG_OPTION_NON_VOLATILE, KEY_WRITE|KEY_READ, NULL,
  73.                 &hAppKey, &dw);
  74.         }
  75.     }
  76.     if (hSoftKey != NULL)
  77.         RegCloseKey(hSoftKey);
  78.     if (hCompanyKey != NULL)
  79.         RegCloseKey(hCompanyKey);
  80.  
  81.     return hAppKey;
  82. }
  83.  
  84. // returns key for:
  85. //      HKEY_CURRENT_USER\"Software"\RegistryKey\AppName\lpszSection
  86. // creating it if it doesn't exist.
  87. // responsibility of the caller to call RegCloseKey() on the returned HKEY
  88. HKEY CWinApp::GetSectionKey(LPCTSTR lpszSection)
  89. {
  90.     ASSERT(lpszSection != NULL);
  91.  
  92.     HKEY hSectionKey = NULL;
  93.     HKEY hAppKey = GetAppRegistryKey();
  94.     if (hAppKey == NULL)
  95.         return NULL;
  96.  
  97.     DWORD dw;
  98.     RegCreateKeyEx(hAppKey, lpszSection, 0, REG_NONE,
  99.         REG_OPTION_NON_VOLATILE, KEY_WRITE|KEY_READ, NULL,
  100.         &hSectionKey, &dw);
  101.     RegCloseKey(hAppKey);
  102.     return hSectionKey;
  103. }
  104.  
  105. UINT CWinApp::GetProfileInt(LPCTSTR lpszSection, LPCTSTR lpszEntry,
  106.     int nDefault)
  107. {
  108.     ASSERT(lpszSection != NULL);
  109.     ASSERT(lpszEntry != NULL);
  110.     if (m_pszRegistryKey != NULL) // use registry
  111.     {
  112.         HKEY hSecKey = GetSectionKey(lpszSection);
  113.         if (hSecKey == NULL)
  114.             return nDefault;
  115.         DWORD dwValue;
  116.         DWORD dwType;
  117.         DWORD dwCount = sizeof(DWORD);
  118.         LONG lResult = RegQueryValueEx(hSecKey, (LPTSTR)lpszEntry, NULL, &dwType,
  119.             (LPBYTE)&dwValue, &dwCount);
  120.         RegCloseKey(hSecKey);
  121.         if (lResult == ERROR_SUCCESS)
  122.         {
  123.             ASSERT(dwType == REG_DWORD);
  124.             ASSERT(dwCount == sizeof(dwValue));
  125.             return (UINT)dwValue;
  126.         }
  127.         return nDefault;
  128.     }
  129.     else
  130.     {
  131.         ASSERT(m_pszProfileName != NULL);
  132.         return ::GetPrivateProfileInt(lpszSection, lpszEntry, nDefault,
  133.             m_pszProfileName);
  134.     }
  135. }
  136.  
  137. CString CWinApp::GetProfileString(LPCTSTR lpszSection, LPCTSTR lpszEntry,
  138.     LPCTSTR lpszDefault)
  139. {
  140.     ASSERT(lpszSection != NULL);
  141.     ASSERT(lpszEntry != NULL);
  142.     if (m_pszRegistryKey != NULL)
  143.     {
  144.         HKEY hSecKey = GetSectionKey(lpszSection);
  145.         if (hSecKey == NULL)
  146.             return lpszDefault;
  147.         CString strValue;
  148.         DWORD dwType, dwCount;
  149.         LONG lResult = RegQueryValueEx(hSecKey, (LPTSTR)lpszEntry, NULL, &dwType,
  150.             NULL, &dwCount);
  151.         if (lResult == ERROR_SUCCESS)
  152.         {
  153.             ASSERT(dwType == REG_SZ);
  154.             lResult = RegQueryValueEx(hSecKey, (LPTSTR)lpszEntry, NULL, &dwType,
  155.                 (LPBYTE)strValue.GetBuffer(dwCount/sizeof(TCHAR)), &dwCount);
  156.             strValue.ReleaseBuffer();
  157.         }
  158.         RegCloseKey(hSecKey);
  159.         if (lResult == ERROR_SUCCESS)
  160.         {
  161.             ASSERT(dwType == REG_SZ);
  162.             return strValue;
  163.         }
  164.         return lpszDefault;
  165.     }
  166.     else
  167.     {
  168.         ASSERT(m_pszProfileName != NULL);
  169.  
  170.         if (lpszDefault == NULL)
  171.             lpszDefault = &afxChNil;    // don't pass in NULL
  172.         TCHAR szT[4096];
  173.         DWORD dw = ::GetPrivateProfileString(lpszSection, lpszEntry,
  174.             lpszDefault, szT, _countof(szT), m_pszProfileName);
  175.         ASSERT(dw < 4095);
  176.         return szT;
  177.     }
  178. }
  179.  
  180. BOOL CWinApp::GetProfileBinary(LPCTSTR lpszSection, LPCTSTR lpszEntry,
  181.     BYTE** ppData, UINT* pBytes)
  182. {
  183.     ASSERT(lpszSection != NULL);
  184.     ASSERT(lpszEntry != NULL);
  185.     ASSERT(ppData != NULL);
  186.     ASSERT(pBytes != NULL);
  187.     *ppData = NULL;
  188.     *pBytes = 0;
  189.     if (m_pszRegistryKey != NULL)
  190.     {
  191.         HKEY hSecKey = GetSectionKey(lpszSection);
  192.         if (hSecKey == NULL)
  193.             return FALSE;
  194.  
  195.         DWORD dwType, dwCount;
  196.         LONG lResult = RegQueryValueEx(hSecKey, (LPTSTR)lpszEntry, NULL, &dwType,
  197.             NULL, &dwCount);
  198.         *pBytes = dwCount;
  199.         if (lResult == ERROR_SUCCESS)
  200.         {
  201.             ASSERT(dwType == REG_BINARY);
  202.             *ppData = new BYTE[*pBytes];
  203.             lResult = RegQueryValueEx(hSecKey, (LPTSTR)lpszEntry, NULL, &dwType,
  204.                 *ppData, &dwCount);
  205.         }
  206.         RegCloseKey(hSecKey);
  207.         if (lResult == ERROR_SUCCESS)
  208.         {
  209.             ASSERT(dwType == REG_BINARY);
  210.             return TRUE;
  211.         }
  212.         else
  213.         {
  214.             delete [] *ppData;
  215.             *ppData = NULL;
  216.         }
  217.         return FALSE;
  218.     }
  219.     else
  220.     {
  221.         ASSERT(m_pszProfileName != NULL);
  222.  
  223.         CString str = GetProfileString(lpszSection, lpszEntry, NULL);
  224.         if (str.IsEmpty())
  225.             return FALSE;
  226.         ASSERT(str.GetLength()%2 == 0);
  227.         int nLen = str.GetLength();
  228.         *pBytes = nLen/2;
  229.         *ppData = new BYTE[*pBytes];
  230.         for (int i=0;i<nLen;i+=2)
  231.         {
  232.             (*ppData)[i/2] = (BYTE)
  233.                 (((str[i+1] - 'A') << 4) + (str[i] - 'A'));
  234.         }
  235.         return TRUE;
  236.     }
  237. }
  238.  
  239. #ifdef AFX_CORE3_SEG
  240. #pragma code_seg(AFX_CORE3_SEG)
  241. #endif
  242.  
  243. BOOL CWinApp::WriteProfileInt(LPCTSTR lpszSection, LPCTSTR lpszEntry,
  244.     int nValue)
  245. {
  246.     ASSERT(lpszSection != NULL);
  247.     ASSERT(lpszEntry != NULL);
  248.     if (m_pszRegistryKey != NULL)
  249.     {
  250.         HKEY hSecKey = GetSectionKey(lpszSection);
  251.         if (hSecKey == NULL)
  252.             return FALSE;
  253.         LONG lResult = RegSetValueEx(hSecKey, lpszEntry, NULL, REG_DWORD,
  254.             (LPBYTE)&nValue, sizeof(nValue));
  255.         RegCloseKey(hSecKey);
  256.         return lResult == ERROR_SUCCESS;
  257.     }
  258.     else
  259.     {
  260.         ASSERT(m_pszProfileName != NULL);
  261.  
  262.         TCHAR szT[16];
  263.         wsprintf(szT, _T("%d"), nValue);
  264.         return ::WritePrivateProfileString(lpszSection, lpszEntry, szT,
  265.             m_pszProfileName);
  266.     }
  267. }
  268.  
  269. BOOL CWinApp::WriteProfileString(LPCTSTR lpszSection, LPCTSTR lpszEntry,
  270.             LPCTSTR lpszValue)
  271. {
  272.     ASSERT(lpszSection != NULL);
  273.     if (m_pszRegistryKey != NULL)
  274.     {
  275.         LONG lResult;
  276.         if (lpszEntry == NULL) //delete whole section
  277.         {
  278.             HKEY hAppKey = GetAppRegistryKey();
  279.             if (hAppKey == NULL)
  280.                 return FALSE;
  281.             lResult = ::RegDeleteKey(hAppKey, lpszSection);
  282.             RegCloseKey(hAppKey);
  283.         }
  284.         else if (lpszValue == NULL)
  285.         {
  286.             HKEY hSecKey = GetSectionKey(lpszSection);
  287.             if (hSecKey == NULL)
  288.                 return FALSE;
  289.             // necessary to cast away const below
  290.             lResult = ::RegDeleteValue(hSecKey, (LPTSTR)lpszEntry);
  291.             RegCloseKey(hSecKey);
  292.         }
  293.         else
  294.         {
  295.             HKEY hSecKey = GetSectionKey(lpszSection);
  296.             if (hSecKey == NULL)
  297.                 return FALSE;
  298.             lResult = RegSetValueEx(hSecKey, lpszEntry, NULL, REG_SZ,
  299.                 (LPBYTE)lpszValue, (lstrlen(lpszValue)+1)*sizeof(TCHAR));
  300.             RegCloseKey(hSecKey);
  301.         }
  302.         return lResult == ERROR_SUCCESS;
  303.     }
  304.     else
  305.     {
  306.         ASSERT(m_pszProfileName != NULL);
  307.         ASSERT(lstrlen(m_pszProfileName) < 4095); // can't read in bigger
  308.         return ::WritePrivateProfileString(lpszSection, lpszEntry, lpszValue,
  309.             m_pszProfileName);
  310.     }
  311. }
  312.  
  313. BOOL CWinApp::WriteProfileBinary(LPCTSTR lpszSection, LPCTSTR lpszEntry,
  314.     LPBYTE pData, UINT nBytes)
  315. {
  316.     ASSERT(lpszSection != NULL);
  317.     if (m_pszRegistryKey != NULL)
  318.     {
  319.         LONG lResult;
  320.         HKEY hSecKey = GetSectionKey(lpszSection);
  321.         if (hSecKey == NULL)
  322.             return FALSE;
  323.         lResult = RegSetValueEx(hSecKey, lpszEntry, NULL, REG_BINARY,
  324.             pData, nBytes);
  325.         RegCloseKey(hSecKey);
  326.         return lResult == ERROR_SUCCESS;
  327.     }
  328.  
  329.     // convert to string and write out
  330.     LPTSTR lpsz = new TCHAR[nBytes*2+1];
  331.     for (UINT i = 0; i < nBytes; i++)
  332.     {
  333.         lpsz[i*2] = (TCHAR)((pData[i] & 0x0F) + 'A'); //low nibble
  334.         lpsz[i*2+1] = (TCHAR)(((pData[i] >> 4) & 0x0F) + 'A'); //high nibble
  335.     }
  336.     lpsz[i*2] = 0;
  337.  
  338.     ASSERT(m_pszProfileName != NULL);
  339.  
  340.     BOOL bResult = WriteProfileString(lpszSection, lpszEntry, lpsz);
  341.     delete[] lpsz;
  342.     return bResult;
  343. }
  344.  
  345. /////////////////////////////////////////////////////////////////////////////
  346.