home *** CD-ROM | disk | FTP | other *** search
/ Team Palmtops 7 / Palmtops_numero07.iso / WinCE / SDKWindowsCE / HandHeldPCPro30 / sdk.exe / Jupiter SDK / data1.cab / MFC / src / appui3.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-02-19  |  9.7 KB  |  381 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 defined(_WIN32_WCE)
  64.     // Check whether the key HKEY_CURRENT_USER\"Software" exists and, if not,
  65.     // then create it.
  66.     if (RegOpenKeyEx(HKEY_CURRENT_USER, _T("software"), 0, KEY_WRITE|KEY_READ,
  67.         &hSoftKey) == ERROR_SUCCESS)
  68.     {
  69.         RegCloseKey( hSoftKey );
  70.     }
  71.     else
  72.     {
  73.         DWORD dw;
  74.         if (RegCreateKeyEx(HKEY_CURRENT_USER, _T("software"), 0, REG_NONE,
  75.         REG_OPTION_NON_VOLATILE, KEY_WRITE|KEY_READ, NULL,
  76.         &hSoftKey, &dw) == ERROR_SUCCESS)
  77.             RegCloseKey( hSoftKey );
  78.         else
  79.             ASSERT(FALSE);
  80.     }
  81. #endif // _WIN32_WCE
  82.     if (RegOpenKeyEx(HKEY_CURRENT_USER, _T("software"), 0, KEY_WRITE|KEY_READ,
  83.         &hSoftKey) == ERROR_SUCCESS)
  84.     {
  85.         DWORD dw;
  86.         if (RegCreateKeyEx(hSoftKey, m_pszRegistryKey, 0, REG_NONE,
  87.             REG_OPTION_NON_VOLATILE, KEY_WRITE|KEY_READ, NULL,
  88.             &hCompanyKey, &dw) == ERROR_SUCCESS)
  89.         {
  90.             RegCreateKeyEx(hCompanyKey, m_pszProfileName, 0, REG_NONE,
  91.                 REG_OPTION_NON_VOLATILE, KEY_WRITE|KEY_READ, NULL,
  92.                 &hAppKey, &dw);
  93.         }
  94.     }
  95.     if (hSoftKey != NULL)
  96.         RegCloseKey(hSoftKey);
  97.     if (hCompanyKey != NULL)
  98.         RegCloseKey(hCompanyKey);
  99.  
  100.     return hAppKey;
  101. }
  102.  
  103. // returns key for:
  104. //      HKEY_CURRENT_USER\"Software"\RegistryKey\AppName\lpszSection
  105. // creating it if it doesn't exist.
  106. // responsibility of the caller to call RegCloseKey() on the returned HKEY
  107. HKEY CWinApp::GetSectionKey(LPCTSTR lpszSection)
  108. {
  109.     ASSERT(lpszSection != NULL);
  110.  
  111.     HKEY hSectionKey = NULL;
  112.     HKEY hAppKey = GetAppRegistryKey();
  113.     if (hAppKey == NULL)
  114.         return NULL;
  115.  
  116.     DWORD dw;
  117.     RegCreateKeyEx(hAppKey, lpszSection, 0, REG_NONE,
  118.         REG_OPTION_NON_VOLATILE, KEY_WRITE|KEY_READ, NULL,
  119.         &hSectionKey, &dw);
  120.     RegCloseKey(hAppKey);
  121.     return hSectionKey;
  122. }
  123.  
  124. UINT CWinApp::GetProfileInt(LPCTSTR lpszSection, LPCTSTR lpszEntry,
  125.     int nDefault)
  126. {
  127.     ASSERT(lpszSection != NULL);
  128.     ASSERT(lpszEntry != NULL);
  129.     if (m_pszRegistryKey != NULL) // use registry
  130.     {
  131.         HKEY hSecKey = GetSectionKey(lpszSection);
  132.         if (hSecKey == NULL)
  133.             return nDefault;
  134.         DWORD dwValue;
  135.         DWORD dwType;
  136.         DWORD dwCount = sizeof(DWORD);
  137.         LONG lResult = RegQueryValueEx(hSecKey, (LPTSTR)lpszEntry, NULL, &dwType,
  138.             (LPBYTE)&dwValue, &dwCount);
  139.         RegCloseKey(hSecKey);
  140.         if (lResult == ERROR_SUCCESS)
  141.         {
  142.             ASSERT(dwType == REG_DWORD);
  143.             ASSERT(dwCount == sizeof(dwValue));
  144.             return (UINT)dwValue;
  145.         }
  146.         return nDefault;
  147.     }
  148. #if defined(_WIN32_WCE)
  149.         return FALSE;
  150. #else // _WIN32_WCE
  151.     else
  152.     {
  153.         ASSERT(m_pszProfileName != NULL);
  154.         return ::GetPrivateProfileInt(lpszSection, lpszEntry, nDefault,
  155.             m_pszProfileName);
  156.     }
  157. #endif // _WIN32_WCE
  158. }
  159.  
  160. CString CWinApp::GetProfileString(LPCTSTR lpszSection, LPCTSTR lpszEntry,
  161.     LPCTSTR lpszDefault)
  162. {
  163.     ASSERT(lpszSection != NULL);
  164.     ASSERT(lpszEntry != NULL);
  165.     if (m_pszRegistryKey != NULL)
  166.     {
  167.         HKEY hSecKey = GetSectionKey(lpszSection);
  168.         if (hSecKey == NULL)
  169.             return lpszDefault;
  170.         CString strValue;
  171.         DWORD dwType, dwCount;
  172.         LONG lResult = RegQueryValueEx(hSecKey, (LPTSTR)lpszEntry, NULL, &dwType,
  173.             NULL, &dwCount);
  174.         if (lResult == ERROR_SUCCESS)
  175.         {
  176.             ASSERT(dwType == REG_SZ);
  177.             lResult = RegQueryValueEx(hSecKey, (LPTSTR)lpszEntry, NULL, &dwType,
  178.                 (LPBYTE)strValue.GetBuffer(dwCount/sizeof(TCHAR)), &dwCount);
  179.             strValue.ReleaseBuffer();
  180.         }
  181.         RegCloseKey(hSecKey);
  182.         if (lResult == ERROR_SUCCESS)
  183.         {
  184.             ASSERT(dwType == REG_SZ);
  185.             return strValue;
  186.         }
  187.         return lpszDefault;
  188.     }
  189. #if defined(_WIN32_WCE)
  190.         return lpszDefault;
  191. #else // _WIN32_WCE
  192.     else
  193.     {
  194.         ASSERT(m_pszProfileName != NULL);
  195.  
  196.         if (lpszDefault == NULL)
  197.             lpszDefault = &afxChNil;    // don't pass in NULL
  198.         TCHAR szT[4096];
  199.         DWORD dw = ::GetPrivateProfileString(lpszSection, lpszEntry,
  200.             lpszDefault, szT, _countof(szT), m_pszProfileName);
  201.         ASSERT(dw < 4095);
  202.         return szT;
  203.     }
  204. #endif // _WIN32_WCE
  205. }
  206.  
  207. BOOL CWinApp::GetProfileBinary(LPCTSTR lpszSection, LPCTSTR lpszEntry,
  208.     BYTE** ppData, UINT* pBytes)
  209. {
  210.     ASSERT(lpszSection != NULL);
  211.     ASSERT(lpszEntry != NULL);
  212.     ASSERT(ppData != NULL);
  213.     ASSERT(pBytes != NULL);
  214.     *ppData = NULL;
  215.     *pBytes = 0;
  216.     if (m_pszRegistryKey != NULL)
  217.     {
  218.         HKEY hSecKey = GetSectionKey(lpszSection);
  219.         if (hSecKey == NULL)
  220.             return FALSE;
  221.  
  222.         DWORD dwType, dwCount;
  223.         LONG lResult = RegQueryValueEx(hSecKey, (LPTSTR)lpszEntry, NULL, &dwType,
  224.             NULL, &dwCount);
  225.         *pBytes = dwCount;
  226.         if (lResult == ERROR_SUCCESS)
  227.         {
  228.             ASSERT(dwType == REG_BINARY);
  229.             *ppData = new BYTE[*pBytes];
  230.             lResult = RegQueryValueEx(hSecKey, (LPTSTR)lpszEntry, NULL, &dwType,
  231.                 *ppData, &dwCount);
  232.         }
  233.         RegCloseKey(hSecKey);
  234.         if (lResult == ERROR_SUCCESS)
  235.         {
  236.             ASSERT(dwType == REG_BINARY);
  237.             return TRUE;
  238.         }
  239.         else
  240.         {
  241.             delete [] *ppData;
  242.             *ppData = NULL;
  243.         }
  244.         return FALSE;
  245.     }
  246.     else
  247.     {
  248.         ASSERT(m_pszProfileName != NULL);
  249.  
  250.         CString str = GetProfileString(lpszSection, lpszEntry, NULL);
  251.         if (str.IsEmpty())
  252.             return FALSE;
  253.         ASSERT(str.GetLength()%2 == 0);
  254.         int nLen = str.GetLength();
  255.         *pBytes = nLen/2;
  256.         *ppData = new BYTE[*pBytes];
  257.         for (int i=0;i<nLen;i+=2)
  258.         {
  259.             (*ppData)[i/2] = (BYTE)
  260.                 (((str[i+1] - 'A') << 4) + (str[i] - 'A'));
  261.         }
  262.         return TRUE;
  263.     }
  264. }
  265.  
  266. #ifdef AFX_CORE3_SEG
  267. #pragma code_seg(AFX_CORE3_SEG)
  268. #endif
  269.  
  270. BOOL CWinApp::WriteProfileInt(LPCTSTR lpszSection, LPCTSTR lpszEntry,
  271.     int nValue)
  272. {
  273.     ASSERT(lpszSection != NULL);
  274.     ASSERT(lpszEntry != NULL);
  275.     if (m_pszRegistryKey != NULL)
  276.     {
  277.         HKEY hSecKey = GetSectionKey(lpszSection);
  278.         if (hSecKey == NULL)
  279.             return FALSE;
  280.         LONG lResult = RegSetValueEx(hSecKey, lpszEntry, NULL, REG_DWORD,
  281.             (LPBYTE)&nValue, sizeof(nValue));
  282.         RegCloseKey(hSecKey);
  283.         return lResult == ERROR_SUCCESS;
  284.     }
  285.     else
  286. #if defined(_WIN32_WCE)
  287.         return FALSE;
  288. #else // _WIN32_WCE
  289.     {
  290.         ASSERT(m_pszProfileName != NULL);
  291.  
  292.         TCHAR szT[16];
  293.         wsprintf(szT, _T("%d"), nValue);
  294.         return ::WritePrivateProfileString(lpszSection, lpszEntry, szT,
  295.             m_pszProfileName);
  296.     }
  297. #endif // _WIN32_WCE
  298. }
  299.  
  300. BOOL CWinApp::WriteProfileString(LPCTSTR lpszSection, LPCTSTR lpszEntry,
  301.             LPCTSTR lpszValue)
  302. {
  303.     ASSERT(lpszSection != NULL);
  304.     if (m_pszRegistryKey != NULL)
  305.     {
  306.         LONG lResult;
  307.         if (lpszEntry == NULL) //delete whole section
  308.         {
  309.             HKEY hAppKey = GetAppRegistryKey();
  310.             if (hAppKey == NULL)
  311.                 return FALSE;
  312.             lResult = ::RegDeleteKey(hAppKey, lpszSection);
  313.             RegCloseKey(hAppKey);
  314.         }
  315.         else if (lpszValue == NULL)
  316.         {
  317.             HKEY hSecKey = GetSectionKey(lpszSection);
  318.             if (hSecKey == NULL)
  319.                 return FALSE;
  320.             // necessary to cast away const below
  321.             lResult = ::RegDeleteValue(hSecKey, (LPTSTR)lpszEntry);
  322.             RegCloseKey(hSecKey);
  323.         }
  324.         else
  325.         {
  326.             HKEY hSecKey = GetSectionKey(lpszSection);
  327.             if (hSecKey == NULL)
  328.                 return FALSE;
  329.             lResult = RegSetValueEx(hSecKey, lpszEntry, NULL, REG_SZ,
  330.                 (LPBYTE)lpszValue, (lstrlen(lpszValue)+1)*sizeof(TCHAR));
  331.             RegCloseKey(hSecKey);
  332.         }
  333.         return lResult == ERROR_SUCCESS;
  334.     }
  335. #if defined(_WIN32_WCE)
  336.         return FALSE;
  337. #else // _WIN32_WCE
  338.     else
  339.     {
  340.         ASSERT(m_pszProfileName != NULL);
  341.         ASSERT(lstrlen(m_pszProfileName) < 4095); // can't read in bigger
  342.         return ::WritePrivateProfileString(lpszSection, lpszEntry, lpszValue,
  343.             m_pszProfileName);
  344.     }
  345. #endif // _WIN32_WCE
  346. }
  347.  
  348. BOOL CWinApp::WriteProfileBinary(LPCTSTR lpszSection, LPCTSTR lpszEntry,
  349.     LPBYTE pData, UINT nBytes)
  350. {
  351.     ASSERT(lpszSection != NULL);
  352.     if (m_pszRegistryKey != NULL)
  353.     {
  354.         LONG lResult;
  355.         HKEY hSecKey = GetSectionKey(lpszSection);
  356.         if (hSecKey == NULL)
  357.             return FALSE;
  358.         lResult = RegSetValueEx(hSecKey, lpszEntry, NULL, REG_BINARY,
  359.             pData, nBytes);
  360.         RegCloseKey(hSecKey);
  361.         return lResult == ERROR_SUCCESS;
  362.     }
  363.  
  364.     // convert to string and write out
  365.     LPTSTR lpsz = new TCHAR[nBytes*2+1];
  366.     for (UINT i = 0; i < nBytes; i++)
  367.     {
  368.         lpsz[i*2] = (TCHAR)((pData[i] & 0x0F) + 'A'); //low nibble
  369.         lpsz[i*2+1] = (TCHAR)(((pData[i] >> 4) & 0x0F) + 'A'); //high nibble
  370.     }
  371.     lpsz[i*2] = 0;
  372.  
  373.     ASSERT(m_pszProfileName != NULL);
  374.  
  375.     BOOL bResult = WriteProfileString(lpszSection, lpszEntry, lpsz);
  376.     delete[] lpsz;
  377.     return bResult;
  378. }
  379.  
  380. /////////////////////////////////////////////////////////////////////////////
  381.