Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1447 lines
45 KiB

  1. //+----------------------------------------------------------------------------
  2. //
  3. // File: ciniA.cpp
  4. //
  5. // Module: CMUTIL.DLL
  6. //
  7. // Synopsis: Ansi CIni implementation
  8. //
  9. // Copyright (c) 1997-1999 Microsoft Corporation
  10. //
  11. // Author: henryt - relocated to CMUTIL 03/15/98
  12. // quintinb - created A and W versions 05/12/99
  13. //
  14. //+----------------------------------------------------------------------------
  15. #include "cmmaster.h"
  16. //+----------------------------------------------------------------------------
  17. //
  18. // Function: CIniA_Set
  19. //
  20. // Synopsis: This function takes a pointer to a string and a string as arguments. It
  21. // frees the string currently in the destination pointer, allocates the correct
  22. // amount of memory and then copies the source string to the string pointed
  23. // to by the destination string pointer. The allocated memory is the
  24. // responsibility of the caller.
  25. //
  26. // Arguments: LPSTR *ppszDest - pointer to the destination string
  27. // LPCSTR pszSrc - source string for the set
  28. //
  29. // Returns: Nothing
  30. //
  31. // History: quintinb Created Header 01/05/2000
  32. //
  33. //+----------------------------------------------------------------------------
  34. static void CIniA_Set(LPSTR *ppszDest, LPCSTR pszSrc)
  35. {
  36. MYDBGASSERT(ppszDest);
  37. if (ppszDest)
  38. {
  39. CmFree(*ppszDest);
  40. *ppszDest = ((pszSrc && *pszSrc) ? CmStrCpyAllocA(pszSrc) : NULL);
  41. }
  42. }
  43. //+----------------------------------------------------------------------------
  44. //
  45. // Function: CIniA_LoadCat
  46. //
  47. // Synopsis: This function concatenates the suffix argument onto the string
  48. // argument and returns the resulting string through the return
  49. // value. Note that the function allocates the correct amount of
  50. // memory which must be freed by the caller. Also not passing in
  51. // an empty string returns NULL while passing just an empty suffix
  52. // returns just a copy of the string.
  53. //
  54. // Arguments: LPCSTR pszStr - source string to duplicate
  55. // LPCSTR pszSuffix - suffix to add onto the duplicated string
  56. //
  57. // Returns: LPSTR - a duplicate of the concatenated string
  58. //
  59. // History: quintinb Created Header 01/05/2000
  60. //
  61. //+----------------------------------------------------------------------------
  62. static LPSTR CIniA_LoadCat(LPCSTR pszStr, LPCSTR pszSuffix)
  63. {
  64. LPSTR pszTmp;
  65. if (!pszStr || !*pszStr)
  66. {
  67. return (NULL);
  68. }
  69. if (!pszSuffix || !*pszSuffix)
  70. {
  71. pszTmp = CmStrCpyAllocA(pszStr);
  72. }
  73. else
  74. {
  75. pszTmp = CmStrCpyAllocA(pszStr);
  76. if (pszTmp)
  77. {
  78. CmStrCatAllocA(&pszTmp,pszSuffix);
  79. }
  80. }
  81. MYDBGASSERT(pszTmp);
  82. return (pszTmp);
  83. }
  84. //+----------------------------------------------------------------------------
  85. //
  86. // Function: CIniA_GPPS
  87. //
  88. // Synopsis: Wrapper for the Windows API GetPrivateProfileString. The return
  89. // value is the requested value, allocated on behalf of the caller.
  90. // Note that the function assumes a reasonable default size and then
  91. // loops and reallocates until it can fit the whole string.
  92. //
  93. // Arguments: LPCSTR pszSection - Ini file section to retrieve data from
  94. // LPCSTR pszEntry - key name to retrieve data from
  95. // LPCSTR pszDefault - the default string value to return, defaults
  96. // to the empty string ("") if not specified
  97. // LPCSTR pszFile - full path to the ini file to get the data from
  98. //
  99. // Returns: LPSTR - the requested data from the ini file, must be freed
  100. // by the caller
  101. //
  102. // History: quintinb Created Header 01/05/2000
  103. //
  104. //+----------------------------------------------------------------------------
  105. static LPSTR CIniA_GPPS(LPCSTR pszSection, LPCSTR pszEntry, LPCSTR pszDefault, LPCSTR pszFile)
  106. {
  107. LPSTR pszBuffer;
  108. LPCSTR pszLocalDefault = pszDefault ? pszDefault : "";
  109. if ((NULL == pszFile) || ('\0' == *pszFile))
  110. {
  111. CMASSERTMSG(FALSE, "CIniA_GPPS -- NULL or Empty file path passed.");
  112. return CmStrCpyAllocA(pszLocalDefault);
  113. }
  114. size_t nLen = __max((pszDefault ? lstrlenA(pszDefault) : 0) +4,48);
  115. while (1)
  116. {
  117. size_t nNewLen;
  118. pszBuffer = (LPSTR) CmMalloc(nLen*sizeof(CHAR));
  119. MYDBGASSERT(pszBuffer);
  120. if (pszBuffer)
  121. {
  122. nNewLen = GetPrivateProfileStringA(pszSection, pszEntry, pszLocalDefault,
  123. pszBuffer, nLen, pszFile);
  124. if (nNewLen+2 < nLen)
  125. {
  126. return (pszBuffer);
  127. }
  128. CmFree(pszBuffer);
  129. nLen *= 2;
  130. }
  131. else
  132. {
  133. CMASSERTMSG(FALSE, "CIniA_GPPS -- CmMalloc Failed.");
  134. return CmStrCpyAllocA(pszLocalDefault);
  135. }
  136. }
  137. }
  138. //+----------------------------------------------------------------------------
  139. //
  140. // Function: CIni_SetFile
  141. //
  142. // Synopsis: This function is very similar to CIniA_Set in that it takes
  143. // a source string and duplicates it into the string pointed to
  144. // by the destination pointer. However, the difference is that
  145. // this function assumes the pszSrc argument to be a full path to
  146. // a file and thus calls CreateFile on the pszSrc string
  147. // before duplicating the string.
  148. //
  149. // Arguments: LPSTR* ppszDest - pointer to a string to accept the duplicated buffer
  150. // LPCSTR pszSrc - full path to a file, text to be duplicated
  151. //
  152. // Returns: Nothing
  153. //
  154. // History: quintinb Created Header 01/05/2000
  155. //
  156. //+----------------------------------------------------------------------------
  157. void CIniA::CIni_SetFile(LPSTR *ppszDest, LPCSTR pszSrc)
  158. {
  159. MYDBGASSERT(ppszDest);
  160. if (ppszDest)
  161. {
  162. CmFree(*ppszDest);
  163. *ppszDest = NULL;
  164. if (pszSrc && *pszSrc) // pszSrc could be NULL
  165. {
  166. //
  167. // A full path to an existing file is expected
  168. //
  169. HANDLE hFile = CreateFileA(pszSrc, 0,
  170. FILE_SHARE_READ | FILE_SHARE_WRITE,
  171. NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  172. MYDBGASSERT(hFile != INVALID_HANDLE_VALUE);
  173. if (hFile != INVALID_HANDLE_VALUE)
  174. {
  175. CloseHandle(hFile);
  176. //
  177. // Update internal file
  178. //
  179. *ppszDest = CmStrCpyAllocA(pszSrc);
  180. }
  181. }
  182. }
  183. }
  184. //+----------------------------------------------------------------------------
  185. //
  186. // Function: CIniA::CIniA
  187. //
  188. // Synopsis: CIniA constructor
  189. //
  190. // Arguments: HINSTANCE hInst - Instance handle used to load resources
  191. // LPCSTR pszFile - Ini file the object describes
  192. // LPCSTR pszSection - a section suffix that will be appended to
  193. // all section references
  194. // LPCSTR pszRegPath - a path to be used for registry access
  195. // LPCSTR pszEntry - an entry suffix that will be appended to all
  196. // entry references
  197. //
  198. // Returns: Nothing
  199. //
  200. // History: quintinb Created Header 01/05/2000
  201. //
  202. // t-urama Modified 07/19/2000
  203. //
  204. //+----------------------------------------------------------------------------
  205. CIniA::CIniA(HINSTANCE hInst, LPCSTR pszFile, LPCSTR pszRegPath, LPCSTR pszSection, LPCSTR pszEntry)
  206. {
  207. //
  208. // Input pointers default to NULL and in fact the constructor is rarely called
  209. // with parameters. Thus we will skip checking the input pointers and just
  210. // pass them on to the functions below, which are designed to except NULL inputs.
  211. //
  212. m_hInst = hInst;
  213. //
  214. // Make sure to NULL the string params before setting them below. This
  215. // is because we call free on the inputted params and we don't want to try
  216. // to free garbage.
  217. //
  218. m_pszFile = NULL;
  219. m_pszSection = NULL;
  220. m_pszEntry = NULL;
  221. m_pszPrimaryFile = NULL;
  222. m_pszRegPath = NULL;
  223. m_pszPrimaryRegPath = NULL;
  224. m_pszICSDataPath = NULL;
  225. m_fReadICSData = FALSE;
  226. m_fWriteICSData = FALSE;
  227. SetFile(pszFile);
  228. SetSection(pszSection);
  229. SetEntry(pszEntry);
  230. SetRegPath(pszRegPath);
  231. }
  232. //+----------------------------------------------------------------------------
  233. //
  234. // Function: CIniA::~CIniA
  235. //
  236. // Synopsis: CIniA destructor, frees dynamically allocated strings held onto
  237. // by the CIniA object.
  238. //
  239. // Arguments: None
  240. //
  241. // Returns: Nothing
  242. //
  243. // History: quintinb Created Header 01/05/2000
  244. //
  245. // t-urama modified 07/19/2000
  246. //+----------------------------------------------------------------------------
  247. CIniA::~CIniA()
  248. {
  249. CmFree(m_pszFile);
  250. CmFree(m_pszSection);
  251. CmFree(m_pszEntry);
  252. CmFree(m_pszPrimaryFile);
  253. CmFree(m_pszRegPath);
  254. CmFree(m_pszPrimaryRegPath);
  255. CmFree(m_pszICSDataPath);
  256. }
  257. //+----------------------------------------------------------------------------
  258. //
  259. // Function: CIniA::Clear
  260. //
  261. // Synopsis: Clears all of the member variables of the CIniA class. Used
  262. // so that a single CIniA object can be re-used without having to
  263. // destruct the old object and construct a new one.
  264. //
  265. // Arguments: None
  266. //
  267. // Returns: Nothing
  268. //
  269. // History: quintinb Created Header 01/05/2000
  270. //
  271. // t-urama modified 07/19/2000
  272. //+----------------------------------------------------------------------------
  273. void CIniA::Clear()
  274. {
  275. SetHInst(NULL);
  276. SetFile(NULL);
  277. SetSection(NULL);
  278. SetEntry(NULL);
  279. SetPrimaryFile(NULL);
  280. SetRegPath(NULL);
  281. SetPrimaryRegPath(NULL);
  282. SetICSDataPath(NULL);
  283. }
  284. //+----------------------------------------------------------------------------
  285. //
  286. // Function: CIniA::SetSection
  287. //
  288. // Synopsis: Sets the internal section suffix using the CIniA_Set
  289. // helper function.
  290. //
  291. // Arguments: LPCSTR pszSection - section suffix to remember
  292. //
  293. // Returns: Nothing
  294. //
  295. // History: quintinb Created Header 01/05/2000
  296. //
  297. //+----------------------------------------------------------------------------
  298. void CIniA::SetSection(LPCSTR pszSection)
  299. {
  300. CIniA_Set(&m_pszSection, pszSection);
  301. }
  302. //+----------------------------------------------------------------------------
  303. //
  304. // Function: CIniA::SetEntry
  305. //
  306. // Synopsis: Sets the internal entry suffix using the CIniA_Set
  307. // helper function.
  308. //
  309. // Arguments: LPCSTR pszSection - entry suffix to remember
  310. //
  311. // Returns: Nothing
  312. //
  313. // History: quintinb Created Header 01/05/2000
  314. //
  315. //+----------------------------------------------------------------------------
  316. void CIniA::SetEntry(LPCSTR pszEntry)
  317. {
  318. CIniA_Set(&m_pszEntry, pszEntry);
  319. }
  320. //+----------------------------------------------------------------------------
  321. //
  322. // Function: CIniA::SetEntryFromIdx
  323. //
  324. // Synopsis: Sets the internal entry suffix just as SetEntry does. However,
  325. // the input parameter is a DWORD value that must be converted to
  326. // a string before it is stored as the index
  327. //
  328. // Arguments: DWORD dwEntry - index number to append to entries
  329. //
  330. // Returns: Nothing
  331. //
  332. // History: quintinb Created Header 01/05/2000
  333. //
  334. //+----------------------------------------------------------------------------
  335. void CIniA::SetEntryFromIdx(DWORD dwEntry)
  336. {
  337. CHAR szEntry[sizeof(dwEntry)*6+1];
  338. wsprintfA(szEntry, "%u", dwEntry);
  339. SetEntry(szEntry);
  340. }
  341. //+----------------------------------------------------------------------------
  342. //
  343. // Function: CIniA::LoadSection
  344. //
  345. // Synopsis: This function concatenates the given section parameter and the
  346. // section suffix and returns the result via the return value. Note
  347. // that the memory must be freed by the calller.
  348. //
  349. // Arguments: LPCSTR pszSection - base section to concatenate the suffix to
  350. //
  351. // Returns: LPSTR - a newly allocated string containing the pszSection value
  352. // with the section suffix appended
  353. //
  354. // History: quintinb Created Header 01/05/2000
  355. //
  356. //+----------------------------------------------------------------------------
  357. LPSTR CIniA::LoadSection(LPCSTR pszSection) const
  358. {
  359. return (CIniA_LoadCat(pszSection, m_pszSection));
  360. }
  361. //+----------------------------------------------------------------------------
  362. //
  363. // Function: CIniA::LoadEntry
  364. //
  365. // Synopsis: This function concatenates the given entry parameter and the
  366. // entry suffix and returns the result via the return value. Note
  367. // that the memory must be freed by the calller.
  368. //
  369. // Arguments: LPCSTR pszEntry - base entry to concatenate the suffix to
  370. //
  371. // Returns: LPSTR - a newly allocated string containing the pszEntry value
  372. // with the entry suffix appended
  373. //
  374. // History: quintinb Created Header 01/05/2000
  375. //
  376. //+----------------------------------------------------------------------------
  377. LPSTR CIniA::LoadEntry(LPCSTR pszEntry) const
  378. {
  379. return (CIniA_LoadCat(pszEntry, m_pszEntry));
  380. }
  381. //+----------------------------------------------------------------------------
  382. //
  383. // Function: CIniA::GPPS
  384. //
  385. // Synopsis: CIni's version of GetPrivateProfileString. Duplicates the Win32
  386. // API functionality except that it will append the Section and Entry
  387. // suffixes (if any) before calling the Win32 API. The function all
  388. // allocates the string it returns in the return value which must be
  389. // freed by the caller.
  390. //
  391. // Arguments: LPCSTR pszSection - Ini section to look for the data in
  392. // LPCSTR pszEntry - Ini key name that contains the requested data
  393. // LPCSTR pszDefault - default value to return if the key
  394. // cannot be found
  395. //
  396. // Returns: LPSTR - the requested string value
  397. //
  398. // History: quintinb Created Header 01/05/2000
  399. // t-urama modified 07/15/2000
  400. //
  401. //+----------------------------------------------------------------------------
  402. LPSTR CIniA::GPPS(LPCSTR pszSection, LPCSTR pszEntry, LPCSTR pszDefault) const
  403. {
  404. //
  405. // Skip input pointer check since pszSection could be NULL to get all of
  406. // the Section Names in the file, pszEntry could be NULL to get all of the
  407. // key names in a section, and pszDefault is NULL by default.
  408. // GetPrivateProfileString cannot take a NULL default but this is taken care of
  409. // by CIniA_GPPS.
  410. //
  411. LPSTR pszSectionTmp = LoadSection(pszSection);
  412. LPSTR pszEntryTmp = LoadEntry(pszEntry);
  413. LPSTR pszBuffer = NULL;
  414. if (m_fReadICSData)
  415. {
  416. //
  417. // We need first read the data from ICSData reg key, if it's not present then try to
  418. // get it from the file and then see if we have a primary file and read it from there.
  419. //
  420. pszBuffer = (LPTSTR)CIniA_GetEntryFromReg(HKEY_LOCAL_MACHINE, m_pszICSDataPath, pszEntryTmp, REG_SZ, ((MAX_PATH + 1) * sizeof(CHAR)));
  421. if (NULL == pszBuffer)
  422. {
  423. LPSTR pszICSTmp = NULL;
  424. pszBuffer = CIniA_GPPS(pszSectionTmp, pszEntryTmp, pszDefault, GetFile());
  425. if (m_pszPrimaryFile)
  426. {
  427. pszICSTmp = pszBuffer;
  428. pszBuffer = CIniA_GPPS(pszSectionTmp, pszEntryTmp, pszICSTmp, GetPrimaryFile());
  429. }
  430. if (NULL == pszBuffer)
  431. {
  432. if (pszDefault)
  433. {
  434. pszBuffer = CmStrCpyAllocA(pszDefault);
  435. }
  436. else
  437. {
  438. //
  439. // We should not return a null from this wrapper, but an empty string instead
  440. //
  441. pszBuffer = CmStrCpyAllocA(TEXT(""));
  442. }
  443. }
  444. CmFree(pszICSTmp);
  445. }
  446. }
  447. else
  448. {
  449. //
  450. // If there is a reg path present. Registry access for m_pszFile
  451. // unless we want to read it from the file
  452. //
  453. if (m_pszRegPath)
  454. {
  455. MYDBGASSERT(pszEntryTmp && *pszEntryTmp);
  456. if (pszEntryTmp && *pszEntryTmp)
  457. {
  458. pszBuffer = (LPTSTR) CIniA_GetEntryFromReg(HKEY_CURRENT_USER, m_pszRegPath, pszEntryTmp, REG_SZ, ((MAX_PATH + 1) * sizeof(CHAR)));
  459. }
  460. }
  461. if (NULL == pszBuffer)
  462. {
  463. // This could mean that there is no reg path, or that the reg access failed. Either way, we
  464. // try to get the entry from pszFile
  465. //
  466. // Skip input pointer check since pszSection could be NULL to get all of
  467. // the Section Names in the file, pszEntry could be NULL to get all of the
  468. // key names in a section, and pszDefault is NULL by default.
  469. // GetPrivateProfileString cannot take a NULL default but this is taken care of
  470. // by CIniW_GPPS.
  471. //
  472. pszBuffer = CIniA_GPPS(pszSectionTmp, pszEntryTmp, pszDefault, GetFile());
  473. }
  474. MYDBGASSERT(pszBuffer);
  475. // Now we try to get the entry from the primary file
  476. //
  477. LPTSTR pszTmp = NULL;
  478. if (m_pszPrimaryRegPath)
  479. {
  480. MYDBGASSERT(pszEntryTmp && *pszEntryTmp);
  481. if (pszEntryTmp && *pszEntryTmp)
  482. {
  483. pszTmp = pszBuffer;
  484. pszBuffer = (LPTSTR) CIniA_GetEntryFromReg(HKEY_CURRENT_USER, m_pszPrimaryRegPath, pszEntryTmp, REG_SZ, ((MAX_PATH + 1) * sizeof(CHAR)));
  485. }
  486. }
  487. if (NULL == pszBuffer)
  488. {
  489. // Skip input pointer check since pszSection could be NULL to get all of
  490. // the Section Names in the file, pszEntry could be NULL to get all of the
  491. // key names in a section, and pszDefault is NULL by default.
  492. // GetPrivateProfileString cannot take a NULL default but this is taken care of
  493. // by CIniW_GPPS.
  494. //
  495. pszBuffer = CIniA_GPPS(pszSectionTmp, pszEntryTmp, pszTmp, GetPrimaryFile());
  496. }
  497. CmFree(pszTmp);
  498. }
  499. CmFree(pszEntryTmp);
  500. CmFree(pszSectionTmp);
  501. MYDBGASSERT(pszBuffer);
  502. return (pszBuffer);
  503. }
  504. //+----------------------------------------------------------------------------
  505. //
  506. // Function: CIniA::GPPI
  507. //
  508. // Synopsis: CIni's version of GetPrivateProfileInt. Duplicates the Win32
  509. // API functionality except that it will append the Section and Entry
  510. // suffixes (if any) before calling the Win32 API. The function all
  511. // allocates the string it returns in the return value which must be
  512. // freed by the caller.
  513. //
  514. // Arguments: LPCSTR pszSection - Ini section to look for the data in
  515. // LPCSTR pszEntry - Ini key name that contains the requested data
  516. // DWORD dwDefault - default value to return if the key
  517. // cannot be found
  518. //
  519. // Returns: DWORD - the requested numerical value
  520. //
  521. // History: quintinb Created Header 01/05/2000
  522. //
  523. // t-urama modified 07/19/2000
  524. //
  525. //+----------------------------------------------------------------------------
  526. DWORD CIniA::GPPI(LPCSTR pszSection, LPCSTR pszEntry, DWORD dwDefault) const
  527. {
  528. //
  529. // GetPrivateProfileInt doesn't take NULL's for the section and entry
  530. // parameters as GetPrivateProfileString will. Thus check the values returned
  531. // from LoadSection and LoadEntry, which will return NULL if the input parameter
  532. // is either NULL or empty. Since we don't really know what to do in this
  533. // situation lets just assert and return the default value.
  534. //
  535. DWORD dwRet = dwDefault;
  536. LPSTR pszSectionTmp = LoadSection(pszSection);
  537. LPSTR pszEntryTmp = LoadEntry(pszEntry);
  538. LPCSTR pszFileTmp = GetFile();
  539. DWORD* pdwData = NULL;
  540. if (m_fReadICSData)
  541. {
  542. //
  543. // We need first read the data from ICSData reg key, if it's not present then try to
  544. // get it from the file and then see if we have a primary file and read it from there.
  545. //
  546. pdwData = (DWORD*)CIniA_GetEntryFromReg(HKEY_LOCAL_MACHINE, m_pszICSDataPath, pszEntryTmp, REG_DWORD, sizeof(DWORD));
  547. //
  548. // If we got something, assign it to the return value, otherwise try reading from the files
  549. // and using the default.
  550. //
  551. if (NULL == pdwData)
  552. {
  553. //
  554. // The registry access failed, or there is no reg. path. try to get the
  555. // entry from pszFile
  556. //
  557. MYDBGASSERT(pszSectionTmp && pszEntryTmp && pszFileTmp && *pszFileTmp);
  558. if (pszSectionTmp && pszEntryTmp && pszFileTmp && *pszFileTmp)
  559. {
  560. dwRet = GetPrivateProfileIntA(pszSectionTmp, pszEntryTmp, dwDefault, pszFileTmp);
  561. }
  562. if (m_pszPrimaryFile)
  563. {
  564. //
  565. // The registry access failed, or there is no reg. path. try to get the
  566. // entry from pszPrimaryFile
  567. //
  568. pszFileTmp = GetPrimaryFile();
  569. if (pszSectionTmp && pszEntryTmp && pszFileTmp && *pszFileTmp)
  570. {
  571. dwRet = GetPrivateProfileIntA(pszSectionTmp, pszEntryTmp, dwRet, pszFileTmp);
  572. }
  573. }
  574. }
  575. else
  576. {
  577. dwRet = *pdwData;
  578. }
  579. }
  580. else
  581. {
  582. //
  583. // Follow the normal rules
  584. //
  585. if (m_pszRegPath)
  586. {
  587. MYDBGASSERT(pszEntryTmp && *pszEntryTmp);
  588. if (pszEntryTmp && *pszEntryTmp)
  589. {
  590. pdwData = (DWORD*)CIniA_GetEntryFromReg(HKEY_CURRENT_USER, m_pszRegPath, pszEntryTmp, REG_DWORD, sizeof(DWORD));
  591. }
  592. }
  593. if (NULL == pdwData)
  594. {
  595. //
  596. // The registry access failed, or there is no reg. path. try to get the
  597. // entry from pszFile
  598. //
  599. MYDBGASSERT(pszSectionTmp && pszEntryTmp && pszFileTmp && *pszFileTmp);
  600. if (pszSectionTmp && pszEntryTmp && pszFileTmp && *pszFileTmp)
  601. {
  602. dwRet = GetPrivateProfileIntA(pszSectionTmp, pszEntryTmp, dwDefault, pszFileTmp);
  603. }
  604. }
  605. else
  606. {
  607. dwRet = *pdwData;
  608. }
  609. if (m_pszPrimaryRegPath)
  610. {
  611. MYDBGASSERT(pszEntryTmp && *pszEntryTmp);
  612. if (pszEntryTmp && *pszEntryTmp)
  613. {
  614. CmFree(pdwData);
  615. pdwData = (DWORD*)CIniA_GetEntryFromReg(HKEY_CURRENT_USER, m_pszPrimaryRegPath, pszEntryTmp, REG_DWORD, sizeof(DWORD));
  616. if (pdwData)
  617. {
  618. dwRet = *pdwData;
  619. }
  620. }
  621. }
  622. if (NULL == pdwData && m_pszPrimaryFile)
  623. {
  624. //
  625. // The registry access failed, or there is no reg. path. try to get the
  626. // entry from pszPrimaryFile
  627. //
  628. pszFileTmp = GetPrimaryFile();
  629. if (pszSectionTmp && pszEntryTmp && pszFileTmp && *pszFileTmp)
  630. {
  631. dwRet = GetPrivateProfileIntA(pszSectionTmp, pszEntryTmp, dwRet, pszFileTmp);
  632. }
  633. }
  634. }
  635. CmFree(pdwData);
  636. CmFree(pszEntryTmp);
  637. CmFree(pszSectionTmp);
  638. return dwRet;
  639. }
  640. //+----------------------------------------------------------------------------
  641. //
  642. // Function: CIniA::GPPB
  643. //
  644. // Synopsis: CIni's version of GetPrivateProfileBool (which doesn't exactly
  645. // exist). Basically this function is the same as GPPI except that
  646. // the return value is cast to a BOOL value (1 or 0).
  647. //
  648. // Arguments: LPCSTR pszSection - Ini section to look for the data in
  649. // LPCSTR pszEntry - Ini key name that contains the requested data
  650. // DWORD dwDefault - default value to return if the key
  651. // cannot be found
  652. //
  653. // Returns: DWORD - the requested BOOL value
  654. //
  655. // History: quintinb Created Header 01/05/2000
  656. //
  657. //+----------------------------------------------------------------------------
  658. BOOL CIniA::GPPB(LPCSTR pszSection, LPCSTR pszEntry, BOOL bDefault) const
  659. {
  660. return (GPPI(pszSection, pszEntry, (DWORD)bDefault) != 0);
  661. }
  662. //+----------------------------------------------------------------------------
  663. //
  664. // Function: CIniA::WPPI
  665. //
  666. // Synopsis: CIni's version of WritePrivateProfileInt (which doesn't exist as
  667. // a Win32 function). Basically takes the inputted DWORD and prints
  668. // it into a string and then calls WPPS.
  669. //
  670. // Arguments: LPCSTR pszSection - Ini section to write the data to
  671. // LPCSTR pszEntry - Ini key name to store the data at
  672. // DWORD dwBuffer - Numeric value to write
  673. //
  674. // Returns: Nothing
  675. //
  676. // History: quintinb Created Header 01/05/2000
  677. //
  678. // t-urama modified 07/19/2000
  679. //+----------------------------------------------------------------------------
  680. void CIniA::WPPI(LPCSTR pszSection, LPCSTR pszEntry, DWORD dwBuffer)
  681. {
  682. // Technically pszEntry could be NULL, which would erase all of the keys in
  683. // the section pointed to by pszSection. However, this doesn't seem to be
  684. // in the spirit of this wrapper so we will check both string pointers to make
  685. // sure they are valid.
  686. BOOL bRes = FALSE;
  687. //
  688. // Check is we are allowed to save info
  689. //
  690. if ((NULL != pszSection) && ('\0' != pszSection[0]) &&
  691. (NULL != pszEntry) && ('\0' != pszEntry[0]))
  692. {
  693. LPSTR pszEntryTmp = LoadEntry(pszEntry);
  694. if(m_pszRegPath)
  695. {
  696. MYDBGASSERT(pszEntryTmp || (NULL == pszEntry) || ('\0' == pszEntry[0]));
  697. if (NULL != pszEntryTmp && *pszEntryTmp)
  698. {
  699. bRes = CIniA_WriteEntryToReg(HKEY_CURRENT_USER, m_pszRegPath, pszEntryTmp, (BYTE *) &dwBuffer, REG_DWORD, sizeof(DWORD));
  700. }
  701. }
  702. if(!bRes)
  703. {
  704. // This loop is only entered if we are trying to write to the cmp and the registry
  705. // write failed, or we are writing to the cms, in which case we will not even
  706. // try to write to the reg.
  707. LPSTR pszSectionTmp = LoadSection(pszSection);
  708. LPCSTR pszFileTmp = GetFile();
  709. MYDBGASSERT(pszFileTmp && *pszFileTmp);
  710. MYDBGASSERT(pszSectionTmp && *pszSectionTmp);
  711. CHAR szBuffer[sizeof(dwBuffer)*6+1] = {0};
  712. wsprintfA(szBuffer, "%u", dwBuffer);
  713. if (pszFileTmp && *pszFileTmp && pszSectionTmp && *pszSectionTmp && pszEntryTmp && *pszEntryTmp)
  714. {
  715. bRes = WritePrivateProfileStringA(pszSectionTmp, pszEntryTmp, szBuffer, pszFileTmp);
  716. }
  717. if (!bRes)
  718. {
  719. DWORD dwError = GetLastError();
  720. CMTRACE3A("CIniA::WPPI() WritePrivateProfileString[*pszSection=%s,*pszEntry=%s,*pszBuffer=%s", pszSectionTmp, MYDBGSTRA(pszEntryTmp), MYDBGSTRA(szBuffer));
  721. CMTRACE2A("*pszFile=%s] failed, GLE=%u", pszFileTmp, dwError);
  722. }
  723. CmFree(pszSectionTmp);
  724. }
  725. if (m_fWriteICSData)
  726. {
  727. if (NULL != pszEntryTmp && *pszEntryTmp)
  728. {
  729. bRes = CIniA_WriteEntryToReg(HKEY_LOCAL_MACHINE, m_pszICSDataPath, pszEntryTmp, (BYTE *) &dwBuffer, REG_DWORD, sizeof(DWORD));
  730. }
  731. }
  732. CmFree(pszEntryTmp);
  733. }
  734. else
  735. {
  736. CMASSERTMSG(FALSE, "Invalid input paramaters to CIniA::WPPI");
  737. }
  738. }
  739. //+----------------------------------------------------------------------------
  740. //
  741. // Function: CIniA::WPPB
  742. //
  743. // Synopsis: CIni's version of WritePrivateProfileBool (which doesn't exist as
  744. // a Win32 function). Basically takes the inputted BOOL and prints
  745. // either 1 or 0 into a string and then calls WPPI.
  746. //
  747. // Arguments: LPCSTR pszSection - Ini section to write the data to
  748. // LPCSTR pszEntry - Ini key name to store the data at
  749. // DWORD dwBuffer - Numeric value to write
  750. //
  751. // Returns: Nothing
  752. //
  753. // History: quintinb Created Header 01/05/2000
  754. //
  755. //+----------------------------------------------------------------------------
  756. void CIniA::WPPB(LPCSTR pszSection, LPCSTR pszEntry, BOOL bBuffer)
  757. {
  758. WPPI(pszSection, pszEntry, bBuffer ? 1 : 0);
  759. }
  760. //+----------------------------------------------------------------------------
  761. //
  762. // Function: CIniA::WPPS
  763. //
  764. // Synopsis: CIni's version of WritePrivateProfileString
  765. //
  766. // Arguments: LPCSTR pszSection - Ini section to write the data to
  767. // LPCSTR pszEntry - Ini key name to store the data at
  768. // LPCSTR pszBuffer - data buffer to write to the ini file
  769. //
  770. // Returns: Nothing
  771. //
  772. // History: quintinb Created Header 01/05/2000
  773. //
  774. // t-urama modified 07/19/2000
  775. //+----------------------------------------------------------------------------
  776. void CIniA::WPPS(LPCSTR pszSection, LPCSTR pszEntry, LPCSTR pszBuffer)
  777. {
  778. LPSTR pszEntryTmp = LoadEntry(pszEntry);
  779. LPSTR pszSectionTmp = LoadSection(pszSection);
  780. LPCSTR pszFileTmp = GetFile();
  781. MYDBGASSERT(pszFileTmp && *pszFileTmp);
  782. MYDBGASSERT(pszSectionTmp && *pszSectionTmp);
  783. MYDBGASSERT(pszEntryTmp || (NULL == pszEntry) || (L'\0' == pszEntry[0]));
  784. // Both pszEntry and pszBuffer could be NULL or Empty. However, pszSection and
  785. // the file path must not be NULL or empty. We also don't want to have a non-NULL
  786. // or non-Empty value for pszEntry and then get a NULL value back from LoadEntry
  787. // (indicating that LoadEntry had text to duplicate but failed for some reason).
  788. // Writing with a NULL value accidently will delete the key value we were trying to set.
  789. // Make sure to assert and prevent data loss in this case.
  790. //
  791. //
  792. // Check is we are allowed to save info
  793. //
  794. if(pszEntryTmp || (NULL == pszEntry) || (L'\0' == pszEntry[0]))
  795. {
  796. BOOL bRes = FALSE;
  797. // First try to write to registry if pszRegPath exists
  798. if(m_pszRegPath)
  799. {
  800. if (NULL == pszBuffer)
  801. {
  802. CIniA_DeleteEntryFromReg(HKEY_CURRENT_USER, m_pszRegPath, pszEntryTmp);
  803. bRes = TRUE; // never erase from the cmp or cms file if there is a regpath.
  804. }
  805. else
  806. {
  807. DWORD dwSize = (lstrlenA(pszBuffer) + 1) * sizeof(CHAR);
  808. bRes = CIniA_WriteEntryToReg(HKEY_CURRENT_USER, m_pszRegPath, pszEntryTmp, (BYTE *) pszBuffer, REG_SZ, dwSize);
  809. }
  810. }
  811. if(!bRes)
  812. {
  813. // This loop is only entered if we are trying to write to the cmp and the registry
  814. // write failed, or we are writing to the cms, in which case we will not even
  815. // try to write to the reg.
  816. if (pszFileTmp && *pszFileTmp && pszSectionTmp && *pszSectionTmp )
  817. {
  818. bRes = WritePrivateProfileStringA(pszSectionTmp, pszEntryTmp, pszBuffer, pszFileTmp);
  819. }
  820. }
  821. if (!bRes)
  822. {
  823. DWORD dwError = GetLastError();
  824. CMTRACE3A("CIniA::WPPS() WritePrivateProfileStringA[*pszSection=%s,*pszEntry=%s,*pszBuffer=%s", pszSectionTmp, MYDBGSTRA(pszEntryTmp), MYDBGSTRA(pszBuffer));
  825. CMTRACE2A("*pszFile=%s] failed, GLE=%u", GetFile(), dwError);
  826. }
  827. if (m_fWriteICSData)
  828. {
  829. //
  830. // The return values are ignored and are here to prevent prefix errors
  831. //
  832. if (NULL == pszBuffer)
  833. {
  834. bRes = CIniA_DeleteEntryFromReg(HKEY_LOCAL_MACHINE, m_pszICSDataPath, pszEntryTmp);
  835. }
  836. else
  837. {
  838. DWORD dwSize = (lstrlenA(pszBuffer) + 1) * sizeof(CHAR);
  839. bRes = CIniA_WriteEntryToReg(HKEY_LOCAL_MACHINE, m_pszICSDataPath, pszEntryTmp, (BYTE *) pszBuffer, REG_SZ, dwSize);
  840. }
  841. }
  842. }
  843. CmFree(pszEntryTmp);
  844. CmFree(pszSectionTmp);
  845. }
  846. //+----------------------------------------------------------------------------
  847. //
  848. // Function: CIniA::GetSection
  849. //
  850. // Synopsis: Accessor function for the Section suffix member variable. Will
  851. // return the empty string if m_pszSection is NULL.
  852. //
  853. // Arguments: None
  854. //
  855. // Returns: LPCSTR - Value of the section suffix member variable or ""
  856. // if it is NULL
  857. //
  858. // History: quintinb Created Header 01/05/2000
  859. //
  860. //+----------------------------------------------------------------------------
  861. LPCSTR CIniA::GetSection() const
  862. {
  863. return (m_pszSection ? m_pszSection : "");
  864. }
  865. //+----------------------------------------------------------------------------
  866. //
  867. // Function: CIniA::GetPrimaryFile
  868. //
  869. // Synopsis: Accessor function for the Primary File member variable. Will
  870. // return the empty string if m_pszPrimaryFile is NULL.
  871. //
  872. // Arguments: None
  873. //
  874. // Returns: LPCSTR - Value of the primary file member variable or ""
  875. // if it is NULL
  876. //
  877. // History: quintinb Created Header 01/05/2000
  878. //
  879. //+----------------------------------------------------------------------------
  880. LPCSTR CIniA::GetPrimaryFile() const
  881. {
  882. return (m_pszPrimaryFile ? m_pszPrimaryFile : "");
  883. }
  884. //+----------------------------------------------------------------------------
  885. //
  886. // Function: CIniA::GetHInst
  887. //
  888. // Synopsis: Accessor function for the m_hInst member variable.
  889. //
  890. // Arguments: None
  891. //
  892. // Returns: HINSTANCE - Value of the m_hInst
  893. //
  894. // History: quintinb Created Header 01/05/2000
  895. //
  896. //+----------------------------------------------------------------------------
  897. HINSTANCE CIniA::GetHInst() const
  898. {
  899. return (m_hInst);
  900. }
  901. //+----------------------------------------------------------------------------
  902. //
  903. // Function: CIniA::SetFile
  904. //
  905. // Synopsis: Function to set the m_pszFile member variable. Uses CIni_SetFile.
  906. // Note that if the input parameter is NULL or the empty string then
  907. // m_pszFile will be set to NULL.
  908. //
  909. // Arguments: LPCSTR pszFile - full path to set the m_pszFile member var to
  910. //
  911. // Returns: Nothing
  912. //
  913. // History: quintinb Created Header 01/05/2000
  914. //
  915. //+----------------------------------------------------------------------------
  916. void CIniA::SetFile(LPCSTR pszFile)
  917. {
  918. CIni_SetFile(&m_pszFile, pszFile);
  919. }
  920. //+----------------------------------------------------------------------------
  921. //
  922. // Function: CIniA::SetPrimaryFile
  923. //
  924. // Synopsis: Function to set the m_pszPrimaryFile member variable. Uses CIni_SetFile.
  925. // Note that if the input parameter is NULL or the empty string then
  926. // m_pszPrimaryFile will be set to NULL.
  927. //
  928. // Arguments: LPCSTR pszFile - full path to set the m_pszPrimaryFile member var to
  929. //
  930. // Returns: Nothing
  931. //
  932. // History: quintinb Created Header 01/05/2000
  933. //
  934. //+----------------------------------------------------------------------------
  935. void CIniA::SetPrimaryFile(LPCSTR pszFile)
  936. {
  937. CIni_SetFile(&m_pszPrimaryFile, pszFile);
  938. }
  939. //+----------------------------------------------------------------------------
  940. //
  941. // Function: CIniA::GetFile
  942. //
  943. // Synopsis: Accessor function for the File member variable. Will
  944. // return the empty string if m_pszFile is NULL.
  945. //
  946. // Arguments: None
  947. //
  948. // Returns: LPCSTR - the contents of m_pszFile or "" if it is NULL
  949. //
  950. // History: quintinb Created Header 01/05/2000
  951. //
  952. //+----------------------------------------------------------------------------
  953. LPCSTR CIniA::GetFile() const
  954. {
  955. return (m_pszFile ? m_pszFile : "");
  956. }
  957. //+----------------------------------------------------------------------------
  958. //
  959. // Function: CIniA::SetHInst
  960. //
  961. // Synopsis: Function to set the m_hInst member variable.
  962. //
  963. // Arguments: HINSTANCE hInst - instance handle to set m_hInst to
  964. //
  965. // Returns: Nothing
  966. //
  967. // History: quintinb Created Header 01/05/2000
  968. //
  969. //+----------------------------------------------------------------------------
  970. void CIniA::SetHInst(HINSTANCE hInst)
  971. {
  972. m_hInst = hInst;
  973. }
  974. //
  975. // Loading sections by string resource isn't used anymore
  976. //
  977. #if 0
  978. LPSTR CIniA::LoadSection(UINT nSection) const
  979. {
  980. LPSTR pszTmp = CmLoadStringA(GetHInst(),nSection);
  981. CmStrCatAllocA(&pszTmp,GetSection());
  982. return (pszTmp);
  983. }
  984. #endif
  985. //+----------------------------------------------------------------------------
  986. //
  987. // Function: CIniA::SetRegPath
  988. //
  989. // Synopsis: Sets the registry path for registry access
  990. //
  991. // Arguments: LPCSTR pszRegPath - entry suffix to remember
  992. //
  993. // Returns: Nothing
  994. //
  995. // History: t-urama Created Header 07/13/2000
  996. //
  997. //+----------------------------------------------------------------------------
  998. void CIniA::SetRegPath(LPCSTR pszRegPath)
  999. {
  1000. CIniA_Set(&m_pszRegPath, pszRegPath);
  1001. }
  1002. //+----------------------------------------------------------------------------
  1003. //
  1004. // Function: CIniA::SetPrimaryRegPath
  1005. //
  1006. // Synopsis: Sets the primary registry path for registry access
  1007. //
  1008. // Arguments: LPCSTR pszPrimaryRegPath - Primary reg path
  1009. //
  1010. // Returns: Nothing
  1011. //
  1012. // History: t-urama Created Header 07/13/2000
  1013. //
  1014. //+----------------------------------------------------------------------------
  1015. void CIniA::SetPrimaryRegPath(LPCSTR pszPrimaryRegPath)
  1016. {
  1017. CIniA_Set(&m_pszPrimaryRegPath, pszPrimaryRegPath);
  1018. }
  1019. //+----------------------------------------------------------------------------
  1020. //
  1021. // Function: CIniA::SetICSDataPath
  1022. //
  1023. // Synopsis: Sets the internal registry key to store data for ICS.
  1024. // Need to make sure the string isn't empty since we don't want
  1025. // to write in HKLM
  1026. //
  1027. // Arguments: None
  1028. //
  1029. // Returns: Nothing
  1030. //
  1031. // History: 03/30/2001 tomkel Created
  1032. //
  1033. //+----------------------------------------------------------------------------
  1034. void CIniA::SetICSDataPath(LPCSTR pszICSPath)
  1035. {
  1036. CIniA_Set(&m_pszICSDataPath, pszICSPath);
  1037. }
  1038. //+----------------------------------------------------------------------------
  1039. //
  1040. // Function: CIniA::SetReadICSData
  1041. //
  1042. // Synopsis: Sets the read flag, to read data from the ICS registry key.
  1043. //
  1044. // Arguments: fValue
  1045. //
  1046. // Returns: Nothing
  1047. //
  1048. // History: 03/30/2001 tomkel Created
  1049. //
  1050. //+----------------------------------------------------------------------------
  1051. void CIniA::SetReadICSData(BOOL fValue)
  1052. {
  1053. m_fReadICSData = fValue;
  1054. }
  1055. //+----------------------------------------------------------------------------
  1056. //
  1057. // Function: CIniA::SetWriteICSData
  1058. //
  1059. // Synopsis: Sets the write flag, to write data to the ICS registry key.
  1060. //
  1061. // Arguments: None
  1062. //
  1063. // Returns: Nothing
  1064. //
  1065. // History: 03/30/2001 tomkel Created
  1066. //
  1067. //+----------------------------------------------------------------------------
  1068. void CIniA::SetWriteICSData(BOOL fValue)
  1069. {
  1070. m_fWriteICSData = fValue;
  1071. }
  1072. //+----------------------------------------------------------------------------
  1073. //
  1074. // Function: CIniA::CiniA_GetRegPath
  1075. //
  1076. // Synopsis: Function to get the value ofm_pszRegPath
  1077. //
  1078. // Arguments: none
  1079. //
  1080. // Returns: LPCSTR - Value of m_pszRegPath
  1081. //
  1082. // History: t-urama Created Header 07/15/2000
  1083. //
  1084. //+----------------------------------------------------------------------------
  1085. LPCSTR CIniA::GetRegPath() const
  1086. {
  1087. return (m_pszRegPath ? m_pszRegPath : "");
  1088. }
  1089. //+----------------------------------------------------------------------------
  1090. //
  1091. // Function: CIniA::CiniA_GetPrimaryRegPath
  1092. //
  1093. // Synopsis: Function to get the value ofm_pszPrimaryRegPath
  1094. //
  1095. // Arguments: none
  1096. //
  1097. // Returns: LPCSTR - Value of m_pszPrimaryRegPath
  1098. //
  1099. // History: t-urama Created 07/15/2000
  1100. //
  1101. //+----------------------------------------------------------------------------
  1102. LPCSTR CIniA::GetPrimaryRegPath() const
  1103. {
  1104. return (m_pszPrimaryRegPath ? m_pszPrimaryRegPath : "");
  1105. }
  1106. //+----------------------------------------------------------------------------
  1107. //
  1108. // Function: CIniA::CIniA_DeleteEntryFromReg
  1109. //
  1110. // Synopsis: Function to delete an entry from the registry.
  1111. //
  1112. // Arguments: HKEY - hkey
  1113. // LPCSTR pszRegPathTmp - Reg path
  1114. // LPCSTR pszEntry - Registry value name to be deleted
  1115. //
  1116. // Returns: BOOL - Success or failure
  1117. //
  1118. // History: t-urama Created 07/15/2000
  1119. // 04/03/2001 tomkel Added reg key string to parameters
  1120. //
  1121. //+----------------------------------------------------------------------------
  1122. BOOL CIniA::CIniA_DeleteEntryFromReg(HKEY hKey, LPCSTR pszRegPathTmp, LPCSTR pszEntry) const
  1123. {
  1124. MYDBGASSERT(pszEntry);
  1125. if (NULL == pszEntry || !*pszEntry || NULL == pszRegPathTmp || !*pszRegPathTmp || NULL == hKey)
  1126. {
  1127. return FALSE;
  1128. }
  1129. //
  1130. // Everything is ok. We have a reg path and a entry name.
  1131. //
  1132. HKEY hKeyCm;
  1133. BOOL dwRes = RegOpenKeyExA(hKey,
  1134. pszRegPathTmp,
  1135. 0,
  1136. KEY_SET_VALUE,
  1137. &hKeyCm);
  1138. //
  1139. // If we opened the key successfully, retrieve the value
  1140. //
  1141. if (ERROR_SUCCESS == dwRes)
  1142. {
  1143. dwRes = RegDeleteValueA(hKeyCm, pszEntry);
  1144. }
  1145. return (ERROR_SUCCESS == dwRes);
  1146. }
  1147. //+----------------------------------------------------------------------------
  1148. //
  1149. // Function: CIniA::CiniA_GetEntryFromReg
  1150. //
  1151. // Synopsis: Function to get the value from the registry. The function
  1152. // allocates the string it returns in the return value which must be
  1153. // freed by the caller.
  1154. //
  1155. // Arguments: HKEY hKey - reg hkey
  1156. // pszRegPathTmp - reg key name
  1157. // LPCSTR pszEntry - Registry value name that contains the requested data
  1158. // DWORD dwType - Type of value
  1159. // DWORD dwSize - Size of value
  1160. //
  1161. //
  1162. // Returns: LPBYTE - the requested value
  1163. //
  1164. // History: 07/15/2000 t-urama Created Header
  1165. // 04/03/2001 tomkel Changed to pass in registry key string
  1166. //
  1167. //+----------------------------------------------------------------------------
  1168. LPBYTE CIniA::CIniA_GetEntryFromReg(HKEY hKey, LPCSTR pszRegPathTmp, LPCSTR pszEntry, DWORD dwType, DWORD dwSize) const
  1169. {
  1170. MYDBGASSERT(pszEntry);
  1171. if (NULL == pszEntry || !*pszEntry || NULL == pszRegPathTmp || !*pszRegPathTmp || NULL == hKey)
  1172. {
  1173. return NULL;
  1174. }
  1175. //
  1176. // Everything is ok. We have a reg path and a entry name.
  1177. //
  1178. DWORD dwTypeTmp = dwType;
  1179. DWORD dwSizeTmp = dwSize;
  1180. HKEY hKeyCm;
  1181. LPBYTE lpData = NULL;
  1182. //
  1183. // Open the sub key under HKCU
  1184. //
  1185. DWORD dwRes = RegOpenKeyExA(hKey,
  1186. pszRegPathTmp,
  1187. 0,
  1188. KEY_QUERY_VALUE,
  1189. &hKeyCm);
  1190. //
  1191. // If we opened the key successfully, retrieve the value
  1192. //
  1193. if (ERROR_SUCCESS == dwRes)
  1194. {
  1195. do
  1196. {
  1197. //
  1198. // Allocate a buffer
  1199. //
  1200. CmFree(lpData);
  1201. lpData = (BYTE *) CmMalloc(dwSizeTmp);
  1202. if (NULL == lpData)
  1203. {
  1204. RegCloseKey(hKeyCm);
  1205. return FALSE;
  1206. }
  1207. dwRes = RegQueryValueExA(hKeyCm,
  1208. pszEntry,
  1209. NULL,
  1210. &dwTypeTmp,
  1211. lpData,
  1212. &dwSizeTmp);
  1213. } while (ERROR_MORE_DATA == dwRes);
  1214. RegCloseKey(hKeyCm);
  1215. }
  1216. if (ERROR_SUCCESS == dwRes && dwTypeTmp == dwType)
  1217. {
  1218. return lpData;
  1219. }
  1220. else
  1221. {
  1222. CmFree(lpData);
  1223. return NULL;
  1224. }
  1225. }
  1226. //+----------------------------------------------------------------------------
  1227. //
  1228. // Function: CIniA::CiniA_WriteEntryToReg
  1229. //
  1230. // Synopsis: Function to write and entry to the registry.
  1231. //
  1232. // Arguments: HKEY hKey
  1233. // LPCSTR pszRegPathTmp - name of the reg key
  1234. // LPCSTR pszEntry - Registry value name to which data is to be written
  1235. // CONST BYTE *lpData - Data to be written
  1236. // DWORD dwType - The type of value to be entered
  1237. // DWORD dwSize - The size of the value entered
  1238. //
  1239. // Returns: BOOL - Success or failure
  1240. //
  1241. // History: t-urama Created Header 07/15/2000
  1242. //
  1243. //+----------------------------------------------------------------------------
  1244. BOOL CIniA::CIniA_WriteEntryToReg(HKEY hKey, LPCSTR pszRegPathTmp, LPCSTR pszEntry, CONST BYTE *lpData, DWORD dwType, DWORD dwSize) const
  1245. {
  1246. MYDBGASSERT(pszEntry && *pszEntry);
  1247. MYDBGASSERT(lpData);
  1248. MYDBGASSERT(pszRegPathTmp && *pszRegPathTmp);
  1249. if (NULL == pszEntry || !*pszEntry || NULL == lpData || NULL == pszRegPathTmp || !*pszRegPathTmp || NULL == hKey)
  1250. {
  1251. return FALSE;
  1252. }
  1253. HKEY hKeyCm;
  1254. DWORD dwDisposition;
  1255. DWORD dwRes = 1;
  1256. dwRes = RegCreateKeyExA(hKey,
  1257. pszRegPathTmp,
  1258. 0,
  1259. NULL,
  1260. REG_OPTION_NON_VOLATILE,
  1261. KEY_SET_VALUE,
  1262. NULL,
  1263. &hKeyCm,
  1264. &dwDisposition);
  1265. //
  1266. // If we opened the key successfully, write the value
  1267. //
  1268. if (ERROR_SUCCESS == dwRes)
  1269. {
  1270. dwRes = RegSetValueExA(hKeyCm,
  1271. pszEntry,
  1272. 0,
  1273. dwType,
  1274. lpData,
  1275. dwSize);
  1276. RegCloseKey(hKeyCm);
  1277. }
  1278. #ifdef DEBUG
  1279. if (ERROR_SUCCESS != dwRes)
  1280. {
  1281. CMTRACE1(TEXT("CIniA_WriteEntryToReg() - %s failed"), (LPTSTR)pszEntry);
  1282. }
  1283. #endif
  1284. return (ERROR_SUCCESS == dwRes);
  1285. }