RegInst.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. //**************************************************************************
  2. //
  3. // Filename : RegInst.c
  4. //
  5. // Purpose : registry routines
  6. //
  7. // Modification history :
  8. //
  9. // 02dec96:HJH - Creation
  10. //
  11. //**************************************************************************
  12. //**************************************************************************
  13. //
  14. // Includes
  15. //
  16. //**************************************************************************
  17. #ifdef JA2_PRECOMPILED_HEADERS
  18. #include "JA2 SGP ALL.H"
  19. #elif defined( WIZ8_PRECOMPILED_HEADERS )
  20. #include "WIZ8 SGP ALL.H"
  21. #else
  22. #include "types.h"
  23. #include "RegInst.h"
  24. #include "WCheck.h"
  25. #endif
  26. //**************************************************************************
  27. //
  28. // Defines
  29. //
  30. //**************************************************************************
  31. #define REG_KEY_SIZE 50
  32. //**************************************************************************
  33. //
  34. // Variables
  35. //
  36. //**************************************************************************
  37. // INI strings are not localized
  38. static const TCHAR szSoftware[] = _T("Software");
  39. static CHAR gszRegistryKey[REG_KEY_SIZE];
  40. static CHAR gszAppName[REG_KEY_SIZE];
  41. static CHAR gszProfileName[REG_KEY_SIZE];
  42. //**************************************************************************
  43. //
  44. // Functions
  45. //
  46. //**************************************************************************
  47. BOOLEAN InitializeRegistryKeys(STR lpszAppName, STR lpszRegistryKey)
  48. {
  49. CHECKF(lpszAppName != NULL);
  50. CHECKF(lpszRegistryKey != NULL);
  51. //CHECKF(gpszRegistryKey == NULL);
  52. //CHECKF(gpszAppName == NULL);
  53. //CHECKF(gpszProfileName == NULL);
  54. // Note: this will leak the original gpszProfileName, but it
  55. // will be freed when the application exits. No assumptions
  56. // can be made on how gpszProfileName was allocated.
  57. strcpy( gszAppName, lpszAppName);
  58. strcpy( gszRegistryKey, lpszRegistryKey );
  59. strcpy( gszProfileName, gszAppName);
  60. return(TRUE);
  61. }
  62. // returns key for HKEY_CURRENT_USER\"Software"\RegistryKey\ProfileName
  63. // creating it if it doesn't exist
  64. // responsibility of the caller to call RegCloseKey() on the returned HKEY
  65. HKEY GetAppRegistryKey()
  66. {
  67. HKEY hAppKey = NULL;
  68. HKEY hSoftKey = NULL;
  69. HKEY hCompanyKey = NULL;
  70. assert(gszRegistryKey[0] != '\0');
  71. //assert(gpszProfileName != NULL);
  72. if (RegOpenKeyEx(HKEY_CURRENT_USER, szSoftware, 0, KEY_WRITE|KEY_READ,
  73. &hSoftKey) == ERROR_SUCCESS)
  74. {
  75. DWORD dw;
  76. if (RegCreateKeyEx(hSoftKey, gszRegistryKey, 0, REG_NONE,
  77. REG_OPTION_NON_VOLATILE, KEY_WRITE|KEY_READ, NULL,
  78. &hCompanyKey, &dw) == ERROR_SUCCESS)
  79. {
  80. RegCreateKeyEx(hCompanyKey, gszProfileName, 0, REG_NONE,
  81. REG_OPTION_NON_VOLATILE, KEY_WRITE|KEY_READ, NULL,
  82. &hAppKey, &dw);
  83. }
  84. }
  85. if (hSoftKey != NULL)
  86. RegCloseKey(hSoftKey);
  87. if (hCompanyKey != NULL)
  88. RegCloseKey(hCompanyKey);
  89. return hAppKey;
  90. }
  91. // returns key for:
  92. // HKEY_CURRENT_USER\"Software"\RegistryKey\AppName\lpszSection
  93. // creating it if it doesn't exist.
  94. // responsibility of the caller to call RegCloseKey() on the returned HKEY
  95. HKEY GetSectionKey(STR lpszSection)
  96. {
  97. HKEY hSectionKey = NULL;
  98. HKEY hAppKey = GetAppRegistryKey();
  99. DWORD dw;
  100. assert(lpszSection != NULL);
  101. if (hAppKey == NULL)
  102. return NULL;
  103. RegCreateKeyEx(hAppKey, lpszSection, 0, REG_NONE,
  104. REG_OPTION_NON_VOLATILE, KEY_WRITE|KEY_READ, NULL,
  105. &hSectionKey, &dw);
  106. RegCloseKey(hAppKey);
  107. return hSectionKey;
  108. }
  109. UINT GetProfileInteger(STR lpszSection, STR lpszEntry, int nDefault)
  110. {
  111. DWORD dwValue;
  112. DWORD dwType;
  113. DWORD dwCount = sizeof(DWORD);
  114. LONG lResult;
  115. assert(lpszSection != NULL);
  116. assert(lpszEntry != NULL);
  117. if (gszRegistryKey[0] != '\0') // use registry
  118. {
  119. HKEY hSecKey = GetSectionKey(lpszSection);
  120. if (hSecKey == NULL)
  121. return nDefault;
  122. lResult = RegQueryValueEx(hSecKey, (LPTSTR)lpszEntry, NULL, &dwType,
  123. (LPBYTE)&dwValue, &dwCount);
  124. RegCloseKey(hSecKey);
  125. if (lResult == ERROR_SUCCESS)
  126. {
  127. assert(dwType == REG_DWORD);
  128. assert(dwCount == sizeof(dwValue));
  129. return (UINT)dwValue;
  130. }
  131. return nDefault;
  132. }
  133. else
  134. {
  135. assert(gszProfileName[0] != '\0');
  136. return GetPrivateProfileInt(lpszSection, lpszEntry, nDefault,
  137. gszProfileName);
  138. }
  139. }
  140. BOOLEAN GetProfileChar(STR lpszSection, STR lpszEntry, STR lpszDefault, STR lpszValue)
  141. {
  142. DWORD dwType, dwCount;
  143. LONG lResult;
  144. BOOLEAN fRet = TRUE;
  145. CHAR strValue[200];
  146. assert(lpszSection != NULL);
  147. assert(lpszEntry != NULL);
  148. assert(lpszDefault != NULL);
  149. if (gszRegistryKey[0] != '\0')
  150. {
  151. HKEY hSecKey = GetSectionKey(lpszSection);
  152. if (hSecKey == NULL)
  153. {
  154. strcpy( lpszValue, lpszDefault );
  155. return(TRUE);
  156. }
  157. lResult = RegQueryValueEx(hSecKey, (LPTSTR)lpszEntry, NULL, &dwType,
  158. NULL, &dwCount);
  159. if (lResult == ERROR_SUCCESS)
  160. {
  161. assert(dwType == REG_SZ);
  162. lResult = RegQueryValueEx(hSecKey, (LPTSTR)lpszEntry, NULL, &dwType,
  163. (LPBYTE)strValue, &dwCount);
  164. }
  165. RegCloseKey(hSecKey);
  166. if (lResult == ERROR_SUCCESS)
  167. {
  168. assert(dwType == REG_SZ);
  169. strcpy( lpszValue, strValue );
  170. return(TRUE);
  171. }
  172. strcpy( lpszValue, lpszDefault );
  173. return(TRUE);
  174. }
  175. // else
  176. // {
  177. // assert(gpszProfileName != NULL);
  178. //
  179. // if (lpszDefault == NULL)
  180. // lpszDefault = &afxChNil; // don't pass in NULL
  181. // TCHAR szT[4096];
  182. // DWORD dw = ::GetPrivateProfileString(lpszSection, lpszEntry,
  183. // lpszDefault, szT, _countof(szT), gpszProfileName);
  184. // assert(dw < 4095);
  185. // return szT;
  186. // }
  187. return( fRet );
  188. }
  189. BOOL GetProfileBinary(STR lpszSection, STR lpszEntry,
  190. BYTE** ppData, UINT* pBytes)
  191. {
  192. // DWORD dwType, dwCount;
  193. // LONG lResult;
  194. //
  195. // assert(lpszSection != NULL);
  196. // assert(lpszEntry != NULL);
  197. // assert(ppData != NULL);
  198. // assert(pBytes != NULL);
  199. // *ppData = NULL;
  200. // *pBytes = 0;
  201. //
  202. // if (gpszRegistryKey != NULL)
  203. // {
  204. // LPBYTE lpByte = NULL;
  205. // HKEY hSecKey = GetSectionKey(lpszSection);
  206. // if (hSecKey == NULL)
  207. // return FALSE;
  208. //
  209. // lResult = RegQueryValueEx(hSecKey, (LPTSTR)lpszEntry, NULL, &dwType,
  210. // NULL, &dwCount);
  211. // *pBytes = dwCount;
  212. // if (lResult == ERROR_SUCCESS)
  213. // {
  214. // assert(dwType == REG_BINARY);
  215. // *ppData = new BYTE[*pBytes];
  216. // lResult = RegQueryValueEx(hSecKey, (LPTSTR)lpszEntry, NULL, &dwType,
  217. // *ppData, &dwCount);
  218. // }
  219. // RegCloseKey(hSecKey);
  220. // if (lResult == ERROR_SUCCESS)
  221. // {
  222. // assert(dwType == REG_BINARY);
  223. // return TRUE;
  224. // }
  225. // else
  226. // {
  227. // delete [] *ppData;
  228. // *ppData = NULL;
  229. // }
  230. // return FALSE;
  231. // }
  232. // else
  233. // {
  234. // //assert(gpszProfileName != NULL);
  235. // //
  236. // //CString str = GetProfileString(lpszSection, lpszEntry, NULL);
  237. // //if (str.IsEmpty())
  238. // // return FALSE;
  239. // //assert(str.GetLength()%2 == 0);
  240. // //int nLen = str.GetLength();
  241. // //*pBytes = nLen/2;
  242. // //*ppData = new BYTE[*pBytes];
  243. // //for (int i=0;i<nLen;i+=2)
  244. // //{
  245. // // (*ppData)[i/2] = (BYTE)
  246. // // (((str[i+1] - _T('A')) << 4) + (str[i] - _T('A')));
  247. // //}
  248. // return TRUE;
  249. // }
  250. return TRUE;
  251. }
  252. BOOL WriteProfileInt(STR lpszSection, STR lpszEntry, int nValue)
  253. {
  254. // LONG lResult;
  255. // TCHAR szT[16];
  256. //
  257. // assert(lpszSection != NULL);
  258. // assert(lpszEntry != NULL);
  259. //
  260. // if (gpszRegistryKey != NULL)
  261. // {
  262. // HKEY hSecKey = GetSectionKey(lpszSection);
  263. // if (hSecKey == NULL)
  264. // return FALSE;
  265. // lResult = RegSetValueEx(hSecKey, lpszEntry, NULL, REG_DWORD,
  266. // (LPBYTE)&nValue, sizeof(nValue));
  267. // RegCloseKey(hSecKey);
  268. // return lResult == ERROR_SUCCESS;
  269. // }
  270. // else
  271. // {
  272. // assert(gpszProfileName != NULL);
  273. //
  274. // wsprintf(szT, _T("%d"), nValue);
  275. // return ::WritePrivateProfileString(lpszSection, lpszEntry, szT,
  276. // gpszProfileName);
  277. // }
  278. return TRUE;
  279. }
  280. BOOL WriteProfileChar(STR lpszSection, STR lpszEntry, STR lpszValue)
  281. {
  282. assert(lpszSection != NULL);
  283. if (gszRegistryKey[0] != '\0')
  284. {
  285. LONG lResult;
  286. if (lpszEntry == NULL) //delete whole section
  287. {
  288. HKEY hAppKey = GetAppRegistryKey();
  289. if (hAppKey == NULL)
  290. return FALSE;
  291. lResult = RegDeleteKey(hAppKey, lpszSection);
  292. RegCloseKey(hAppKey);
  293. }
  294. else if (lpszValue == NULL)
  295. {
  296. HKEY hSecKey = GetSectionKey(lpszSection);
  297. if (hSecKey == NULL)
  298. return FALSE;
  299. // necessary to cast away const below
  300. lResult = RegDeleteValue(hSecKey, (LPTSTR)lpszEntry);
  301. RegCloseKey(hSecKey);
  302. }
  303. else
  304. {
  305. HKEY hSecKey = GetSectionKey(lpszSection);
  306. if (hSecKey == NULL)
  307. return FALSE;
  308. lResult = RegSetValueEx(hSecKey, lpszEntry, 0, REG_SZ,
  309. (LPBYTE)lpszValue, (lstrlen(lpszValue)+1)*sizeof(TCHAR));
  310. RegCloseKey(hSecKey);
  311. }
  312. return lResult == ERROR_SUCCESS;
  313. }
  314. // else
  315. // {
  316. // assert(gpszProfileName != NULL);
  317. // assert(lstrlen(gpszProfileName) < 4095); // can't read in bigger
  318. // return ::WritePrivateProfileString(lpszSection, lpszEntry, lpszValue,
  319. // gpszProfileName);
  320. // }
  321. return TRUE;
  322. }
  323. BOOL WriteProfileBinary(STR lpszSection, STR lpszEntry, LPBYTE pData, UINT nBytes)
  324. {
  325. // assert(lpszSection != NULL);
  326. //
  327. // if (gpszRegistryKey != NULL)
  328. // {
  329. // LONG lResult;
  330. // HKEY hSecKey = GetSectionKey(lpszSection);
  331. // if (hSecKey == NULL)
  332. // return FALSE;
  333. // lResult = RegSetValueEx(hSecKey, lpszEntry, NULL, REG_BINARY,
  334. // pData, nBytes);
  335. // RegCloseKey(hSecKey);
  336. // return lResult == ERROR_SUCCESS;
  337. // }
  338. //
  339. // // convert to string and write out
  340. // LPTSTR lpsz = new TCHAR[nBytes*2+1];
  341. // for (UINT i = 0; i < nBytes; i++)
  342. // {
  343. // lpsz[i*2] = (TCHAR)((pData[i] & 0x0F) + _T('A')); //low nibble
  344. // lpsz[i*2+1] = (TCHAR)(((pData[i] >> 4) & 0x0F) + _T('A')); //high nibble
  345. // }
  346. // lpsz[i*2] = 0;
  347. //
  348. // assert(gpszProfileName != NULL);
  349. //
  350. // BOOL bResult = WriteProfileString(lpszSection, lpszEntry, lpsz);
  351. // delete[] lpsz;
  352. // return bResult;
  353. return TRUE;
  354. }