appui3.cpp 8.8 KB

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