home *** CD-ROM | disk | FTP | other *** search
/ Supercompiler 1997 / SUPERCOMPILER97.iso / MS_VC.50 / VC / MFC / SRC / APPUI3.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-30  |  9.2 KB  |  371 lines

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