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.

1041 lines
21 KiB

  1. //--------------------------------------------------------------------------;
  2. //
  3. // File: IniStuff.c
  4. //
  5. // Copyright (C) Microsoft Corporation, 1994 - 1996 All rights reserved
  6. //
  7. // Abstract:
  8. // Set of functions to make dealing with .ini files a little easier.
  9. //
  10. // Contents:
  11. // GetApplicationDir()
  12. // InitIniFile()
  13. // InitIniFile_FullPath()
  14. // EndIniFile()
  15. // GetRegInfo()
  16. // HexValue()
  17. // atoDW()
  18. // GetIniBinSize()
  19. // WriteIniString()
  20. // WriteIniDWORD()
  21. // WriteIniBin()
  22. // GetIniString()
  23. // GetIniDWORD()
  24. // GetIniBin()
  25. //
  26. // History:
  27. // 01/26/94 Fwong Just trying to lend a hand.
  28. //
  29. //--------------------------------------------------------------------------;
  30. #include <windows.h>
  31. #include "IniMgr.h"
  32. #ifdef WIN32
  33. #include <tchar.h>
  34. #endif
  35. #ifndef WIN32
  36. #define GlobalHandleOfPtr(p) (HGLOBAL)LOWORD(GlobalHandle(SELECTOROF(p)))
  37. #else
  38. #define GlobalHandleOfPtr(p) (HGLOBAL)GlobalHandle(p)
  39. #ifndef lstrcpyn
  40. #define lstrcpyn _tcsncpy
  41. #endif // lstrncpy
  42. #endif // WIN32
  43. #define SIZEOF(a) (sizeof(a)/sizeof(a[0]))
  44. #ifndef TEXT
  45. #define TEXT(a) (a)
  46. #endif // TEXT
  47. LPTSTR pszAppIniFile = NULL;
  48. TCHAR gszMmregIni[] = TEXT("mmreg.ini");
  49. //--------------------------------------------------------------------------;
  50. //
  51. // void GetApplicationDir
  52. //
  53. // Description:
  54. // Gets the directory of the application.
  55. //
  56. // Arguments:
  57. // HINSTANCE hinst: Instance handle of .exe.
  58. //
  59. // LPSTR pszPath: Buffer to store string in.
  60. //
  61. // UINT uSize: Size of buffer in bytes.
  62. //
  63. // Return (void):
  64. //
  65. // History:
  66. // 04/04/94 Fwong To "Do the right thing."
  67. //
  68. //--------------------------------------------------------------------------;
  69. void FNGLOBAL GetApplicationDir
  70. (
  71. HINSTANCE hinst,
  72. LPTSTR pszPath,
  73. UINT uSize
  74. )
  75. {
  76. int iCount;
  77. iCount = GetModuleFileName(hinst,pszPath,uSize);
  78. if(0 == iCount)
  79. {
  80. pszPath[0] = 0;
  81. return;
  82. }
  83. for(iCount--;iCount;iCount--)
  84. {
  85. if('\\' == pszPath[iCount])
  86. {
  87. pszPath[iCount] = 0;
  88. return;
  89. }
  90. }
  91. } // GetApplicationDir()
  92. //--------------------------------------------------------------------------;
  93. //
  94. // BOOL InitIniFile
  95. //
  96. // Description:
  97. // Sets the name of the file for .ini file API's
  98. //
  99. // Arguments:
  100. // HINSTANCE hinst: HINSTANCE of application to get full path.
  101. //
  102. // LPTSTR pszIniFileName: Name of file.
  103. //
  104. // Return (BOOL):
  105. // TRUE if successful, FALSE otherwise.
  106. //
  107. // History:
  108. // 01/26/94 Fwong To generalize .ini API's
  109. //
  110. //--------------------------------------------------------------------------;
  111. BOOL FNGLOBAL InitIniFile
  112. (
  113. HINSTANCE hinst,
  114. LPTSTR pszIniFileName
  115. )
  116. {
  117. HGLOBAL hmem;
  118. TCHAR szPath[MAXINISTR];
  119. DWORD cbSize;
  120. if(NULL != pszAppIniFile)
  121. {
  122. hmem = GlobalHandleOfPtr(pszAppIniFile);
  123. GlobalUnlock(hmem);
  124. GlobalFree(hmem);
  125. pszAppIniFile = NULL;
  126. }
  127. GetApplicationDir(hinst,szPath,SIZEOF(szPath));
  128. //
  129. // Note: Two additional one byte for '\0' and one for '\\'
  130. //
  131. cbSize = lstrlen(pszIniFileName) + lstrlen(szPath) + 2;
  132. hmem = GlobalAlloc(GMEM_MOVEABLE|GMEM_SHARE,cbSize);
  133. if(NULL == hmem)
  134. {
  135. return FALSE;
  136. }
  137. pszAppIniFile = GlobalLock(hmem);
  138. lstrcpy(pszAppIniFile,szPath);
  139. lstrcat(pszAppIniFile,TEXT("\\"));
  140. lstrcat(pszAppIniFile,pszIniFileName);
  141. return TRUE;
  142. } // InitIniFile()
  143. //--------------------------------------------------------------------------;
  144. //
  145. // BOOL InitIniFile_FullPath
  146. //
  147. // Description:
  148. // Sets the name of the file for .ini file API's
  149. //
  150. // Arguments:
  151. // LPTSTR pszFullPath: Full pathed name of file.
  152. //
  153. // Return (BOOL):
  154. // TRUE if successful, FALSE otherwise.
  155. //
  156. // History:
  157. // 04/08/94 Fwong To generalize .ini API's
  158. //
  159. //--------------------------------------------------------------------------;
  160. BOOL FNGLOBAL InitIniFile_FullPath
  161. (
  162. LPTSTR pszFullPath
  163. )
  164. {
  165. HGLOBAL hmem;
  166. if(NULL != pszAppIniFile)
  167. {
  168. hmem = GlobalHandleOfPtr(pszAppIniFile);
  169. GlobalUnlock(hmem);
  170. GlobalFree(hmem);
  171. pszAppIniFile = NULL;
  172. }
  173. hmem = GlobalAlloc(GMEM_MOVEABLE|GMEM_SHARE,lstrlen(pszFullPath)+1);
  174. if(NULL == hmem)
  175. {
  176. return FALSE;
  177. }
  178. pszAppIniFile = GlobalLock(hmem);
  179. lstrcpy(pszAppIniFile,pszFullPath);
  180. return TRUE;
  181. } // InitIniFile_FullPath()
  182. //--------------------------------------------------------------------------;
  183. //
  184. // void EndIniFile
  185. //
  186. // Description:
  187. // Frees memory allocated by InitIniFile()
  188. //
  189. // Arguments:
  190. // None.
  191. //
  192. // Return (void):
  193. //
  194. // History:
  195. // 01/26/94 Fwong To generalize .ini API's
  196. //
  197. //--------------------------------------------------------------------------;
  198. void FNGLOBAL EndIniFile
  199. (
  200. void
  201. )
  202. {
  203. HGLOBAL hmem;
  204. if(NULL != pszAppIniFile)
  205. {
  206. hmem = GlobalHandleOfPtr(pszAppIniFile);
  207. GlobalUnlock(hmem);
  208. GlobalFree(hmem);
  209. pszAppIniFile = NULL;
  210. }
  211. } // EndIniFile()
  212. //--------------------------------------------------------------------------;
  213. //
  214. // BOOL GetRegInfo
  215. //
  216. // Description:
  217. // Gets Appropriate version information from MMREG.INI
  218. //
  219. // Arguments:
  220. // DWORD dwEntry: Value of actual entry
  221. //
  222. // LPCTSTR pszSection: Section to look under
  223. //
  224. // LPTSTR pszName: Pointer to buffer to store name. If NULL no attempt
  225. // to copy will be made.
  226. //
  227. // UINT cbName: Size of buffer (in bytes) of pszName. If pszName is
  228. // NULL, this is ignored.
  229. //
  230. // LPTSTR pszDesc: Pointer to buffer for description. If NULL no
  231. // attempt to copy will be made. If pszName is NULL,
  232. // this parameter is ignored.
  233. //
  234. // UINT cbDesc: Size of buffer (in bytes) of pszDesc. If pszDesc is
  235. // NULL, this is ignored.
  236. //
  237. // Return (BOOL):
  238. // TRUE if value found, FALSE otherwise
  239. //
  240. // pszName and pszDesc are return strings.
  241. //
  242. // History:
  243. // 03/25/93 Fwong Created for checking Mid's and Pid's
  244. // 08/10/93 Fwong Updated to check current directory
  245. // 01/30/94 Fwong Added string sizes in parameters
  246. //
  247. //--------------------------------------------------------------------------;
  248. BOOL FNGLOBAL GetRegInfo
  249. (
  250. DWORD dwEntry,
  251. LPCTSTR pszSection,
  252. LPTSTR pszName,
  253. UINT cbName,
  254. LPTSTR pszDesc,
  255. UINT cbDesc
  256. )
  257. {
  258. DWORD cbBytesCopied,i;
  259. TCHAR szIniFile[MAXINISTR];
  260. TCHAR szEntry[11];
  261. TCHAR szFullString[MAXINISTR];
  262. wsprintf(szEntry,TEXT("%lu"),dwEntry);
  263. //
  264. // Getting full path...
  265. //
  266. lstrcpy(szIniFile,pszAppIniFile);
  267. //
  268. // Looking for '\\'
  269. //
  270. for(i=lstrlen(szIniFile)-1;'\\' != szIniFile[i];i--);
  271. szIniFile[i+1] = 0;
  272. lstrcat(szIniFile,gszMmregIni);
  273. //
  274. // Getting String
  275. //
  276. cbBytesCopied = GetPrivateProfileString(
  277. pszSection,
  278. szEntry,
  279. TEXT(""),
  280. szFullString,
  281. SIZEOF(szFullString),
  282. szIniFile);
  283. //
  284. // Checking for No Entry
  285. //
  286. if(cbBytesCopied!=(DWORD)GetPrivateProfileString(
  287. pszSection,
  288. szEntry,
  289. TEXT("a"),
  290. szFullString,
  291. SIZEOF(szFullString),
  292. szIniFile))
  293. {
  294. //
  295. // Not in application directory, trying Windows directory.
  296. //
  297. lstrcpy(szIniFile,gszMmregIni);
  298. cbBytesCopied = GetPrivateProfileString(
  299. pszSection,
  300. szEntry,
  301. TEXT(""),
  302. szFullString,
  303. SIZEOF(szFullString),
  304. szIniFile);
  305. if(cbBytesCopied!=(DWORD)GetPrivateProfileString(
  306. pszSection,
  307. szEntry,
  308. TEXT("a"),
  309. szFullString,
  310. SIZEOF(szFullString),
  311. szIniFile))
  312. {
  313. return (FALSE);
  314. }
  315. }
  316. if(NULL == pszName)
  317. {
  318. //
  319. // No return string just verifying that it exists.
  320. //
  321. return (TRUE);
  322. }
  323. //
  324. // Looking for comma
  325. //
  326. for(i=0;;i++)
  327. {
  328. //
  329. // If no comma is found szDesc is NULL string and
  330. // szName is szFullString
  331. //
  332. if(i==cbBytesCopied)
  333. {
  334. if(NULL != pszDesc)
  335. {
  336. pszDesc[0]=0;
  337. }
  338. lstrcpyn(pszName,szFullString,cbName);
  339. return (TRUE);
  340. }
  341. else
  342. {
  343. //
  344. // Stop Searching if comma is found
  345. //
  346. if(szFullString[i]==',')
  347. break;
  348. }
  349. }
  350. //
  351. // Copy up to comma [non-inclusive]
  352. //
  353. szFullString[i++]=0;
  354. lstrcpyn(pszName,szFullString,cbName);
  355. if(NULL == pszDesc)
  356. {
  357. //
  358. // No need to return pszDesc.
  359. //
  360. return (TRUE);
  361. }
  362. for(;;i++)
  363. {
  364. TCHAR c=szFullString[i];
  365. if(!c)
  366. {
  367. pszDesc[0]=0;
  368. return(TRUE);
  369. }
  370. else
  371. {
  372. if((c!=' ')&&(c!='\t'))
  373. {
  374. break;
  375. }
  376. }
  377. }
  378. //
  379. // Copying remainder of string
  380. //
  381. lstrcpyn(pszDesc,(LPTSTR)&(szFullString[i]),cbDesc);
  382. return (TRUE);
  383. } // GetRegInfo()
  384. //--------------------------------------------------------------------------;
  385. //
  386. // DWORD HexValue
  387. //
  388. // Description:
  389. // Returns the hexvalue of a particular hex digit.
  390. //
  391. // Arguments:
  392. // TCHAR chDigit: Hex digit in question.
  393. //
  394. // Return (DWORD):
  395. // The actual hex value, OR 16 if not valid digit.
  396. //
  397. // History:
  398. // 11/24/93 Fwong For other stuff to work.
  399. //
  400. //--------------------------------------------------------------------------;
  401. DWORD FNGLOBAL HexValue
  402. (
  403. TCHAR chDigit
  404. )
  405. {
  406. if(('0' <= chDigit) && ('9' >= chDigit))
  407. {
  408. return ((DWORD)(chDigit - '0'));
  409. }
  410. if(('a' <= chDigit) && ('f' >= chDigit))
  411. {
  412. return ((DWORD)(chDigit - 'a' + 10));
  413. }
  414. if(('A' <= chDigit) && ('F' >= chDigit))
  415. {
  416. return ((DWORD)(chDigit - 'A' + 10));
  417. }
  418. return (16);
  419. } // HexValue()
  420. //--------------------------------------------------------------------------;
  421. //
  422. // DWORD atoDW
  423. //
  424. // Description:
  425. // Given a string gives the numerical value.
  426. //
  427. // Arguments:
  428. // LPCSTR pszNumber: Pointer to string.
  429. //
  430. // DWORD dwDefault: Default value if error occurs.
  431. //
  432. // Return (DWORD):
  433. // Value of integer in string, or dwDefault if error occurs.
  434. //
  435. // History:
  436. // 08/23/94 Fwong Expanding GetIniDWORD
  437. //
  438. //--------------------------------------------------------------------------;
  439. DWORD FNGLOBAL atoDW
  440. (
  441. LPCTSTR pszNumber,
  442. DWORD dwDefault
  443. )
  444. {
  445. DWORD dwDigit;
  446. DWORD dwReturn;
  447. UINT uBase = 16;
  448. UINT u = 2;
  449. if(('0' != pszNumber[0]) || ('x' != pszNumber[1]))
  450. {
  451. if(10 <= HexValue(pszNumber[0]))
  452. {
  453. return dwDefault;
  454. }
  455. //
  456. // Note:
  457. // uBase = the base (10 for decimal, 16 for hex).
  458. // u = string offset to start decoding.
  459. //
  460. uBase = 10;
  461. u = 0;
  462. }
  463. for(dwReturn = 0L;u <= 9;u++)
  464. {
  465. dwDigit = HexValue(pszNumber[u]);
  466. if(uBase <= dwDigit)
  467. {
  468. //
  469. // Error!!
  470. //
  471. break;
  472. }
  473. dwDigit = uBase*dwReturn + dwDigit;
  474. if(dwReturn != (dwDigit/uBase))
  475. {
  476. //
  477. // OVERFLOWED!! Returning default.
  478. //
  479. return dwDefault;
  480. }
  481. dwReturn = dwDigit;
  482. }
  483. return dwReturn;
  484. } // atoDW()
  485. //--------------------------------------------------------------------------;
  486. //
  487. // DWORD GetIniBinSize
  488. //
  489. // Description:
  490. // Gets the size of a binary in the .ini file.
  491. //
  492. // Arguments:
  493. // LPCTSTR pszSection: Section for binary.
  494. //
  495. // LPCTSTR pszEntry: Name of binary.
  496. //
  497. // Return (DWORD):
  498. // Size (in bytes) of binary, or 0 if not found.
  499. //
  500. // History:
  501. // 01/07/94 Fwong Another thingy for binary stuff...
  502. //
  503. //--------------------------------------------------------------------------;
  504. DWORD FNGLOBAL GetIniBinSize
  505. (
  506. LPCTSTR pszSection,
  507. LPCTSTR pszEntry
  508. )
  509. {
  510. TCHAR szScrap[MAXINISTR];
  511. if(NULL == pszAppIniFile)
  512. {
  513. return 0L;
  514. }
  515. wsprintf(szScrap,TEXT("%s.size"),pszEntry);
  516. return GetIniDWORD(pszSection,szScrap,0);
  517. } // GetIniBinSize()
  518. //--------------------------------------------------------------------------;
  519. //
  520. // BOOL WriteIniString
  521. //
  522. // Description:
  523. // Writes a string into the .ini file for the application.
  524. //
  525. // Arguments:
  526. // LPCTSTR pszSection: Name of Section.
  527. //
  528. // LPCTSTR pszEntry: Name of Entry.
  529. //
  530. // LPCTSTR pszString: String to write.
  531. //
  532. // Return (BOOL):
  533. // Return value from WritePrivateProfileString().
  534. //
  535. // History:
  536. // 11/24/93 Fwong Adding .ini file support.
  537. //
  538. //--------------------------------------------------------------------------;
  539. BOOL FNGLOBAL WriteIniString
  540. (
  541. LPCTSTR pszSection,
  542. LPCTSTR pszEntry,
  543. LPCTSTR pszString
  544. )
  545. {
  546. if(NULL == pszAppIniFile)
  547. {
  548. return FALSE;
  549. }
  550. return WritePrivateProfileString(
  551. pszSection,
  552. pszEntry,
  553. pszString,
  554. pszAppIniFile);
  555. } // WriteIniString()
  556. //--------------------------------------------------------------------------;
  557. //
  558. // BOOL WriteIniDWORD
  559. //
  560. // Description:
  561. // Writes a DWORD to the .ini file for the app.
  562. //
  563. // Arguments:
  564. // LPCTSTR pszSection: Name of section.
  565. //
  566. // LPCTSTR pszEntry: Entry of section.
  567. //
  568. // DWORD dwValue: Value to write.
  569. //
  570. // Return (BOOL):
  571. // Return value from WriteIniString();
  572. //
  573. // History:
  574. // 11/24/93 Fwong
  575. //
  576. //--------------------------------------------------------------------------;
  577. BOOL FNGLOBAL WriteIniDWORD
  578. (
  579. LPCTSTR pszSection,
  580. LPCTSTR pszEntry,
  581. DWORD dwValue
  582. )
  583. {
  584. TCHAR szValue[12];
  585. if(NULL == pszAppIniFile)
  586. {
  587. return FALSE;
  588. }
  589. wsprintf(szValue,TEXT("0x%08lx"),dwValue);
  590. return WriteIniString(pszSection,pszEntry,szValue);
  591. } // WriteIniDWORD()
  592. //--------------------------------------------------------------------------;
  593. //
  594. // void WriteIniBin
  595. //
  596. // Description:
  597. // Writes binary information to the .ini file.
  598. //
  599. // Arguments:
  600. // LPCTSTR pszSection: Section name.
  601. //
  602. // LPCTSTR pszEntry: Entry name.
  603. //
  604. // LPVOID pvoid: Buffer to write.
  605. //
  606. // DWORD cbSize: Size of buffer in bytes.
  607. //
  608. // Return (void):
  609. //
  610. // History:
  611. // 01/07/94 Fwong Yet another random thing.
  612. //
  613. //--------------------------------------------------------------------------;
  614. void FNGLOBAL WriteIniBin
  615. (
  616. LPCTSTR pszSection,
  617. LPCTSTR pszEntry,
  618. LPVOID pvoid,
  619. DWORD cbSize
  620. )
  621. {
  622. TCHAR szEntry[MAXINISTR];
  623. TCHAR szScrap[MAXINISTR];
  624. LPBYTE pByte = pvoid;
  625. LPTSTR pszMiddle;
  626. UINT u;
  627. DWORD dw;
  628. if(NULL == pszAppIniFile)
  629. {
  630. return;
  631. }
  632. //
  633. // Clearing previous entry...
  634. //
  635. dw = GetIniBinSize(pszSection,pszEntry);
  636. dw = (dw / LINE_LIMIT) + ((dw % LINE_LIMIT)?1:0);
  637. for (;dw;dw--)
  638. {
  639. wsprintf(szEntry,TEXT("%s.%03lu"),pszEntry,dw-1);
  640. WriteIniString(pszSection,szEntry,NULL);
  641. }
  642. //
  643. // Writing new entry...
  644. //
  645. wsprintf(szEntry,TEXT("%s.size"),pszEntry);
  646. WriteIniDWORD(pszSection,szEntry,cbSize);
  647. for (dw=0,u=0;dw < cbSize;u++,dw++)
  648. {
  649. pszMiddle = &(szScrap[0]);
  650. //
  651. // Doing individual lines...
  652. //
  653. for (;dw < cbSize;dw++)
  654. {
  655. wsprintf(pszMiddle,TEXT("%02x"),pByte[0]);
  656. pszMiddle += 2;
  657. pByte++;
  658. if((LINE_LIMIT-1)==(dw % LINE_LIMIT))
  659. {
  660. //
  661. // Is it time for next line?!
  662. //
  663. break;
  664. }
  665. }
  666. wsprintf(szEntry,TEXT("%s.%03u"),pszEntry,u);
  667. WriteIniString(pszSection,szEntry,szScrap);
  668. }
  669. } // WriteIniBin()
  670. //--------------------------------------------------------------------------;
  671. //
  672. // int GetIniString
  673. //
  674. // Description:
  675. // Similar to GetProfileString, but for the .ini file for this app.
  676. //
  677. // Arguments:
  678. // LPCTSTR pszSection: Section name.
  679. //
  680. // LPCTSTR pszEntry: Entry name.
  681. //
  682. // LPTSTR pszBuffer: Return buffer.
  683. //
  684. // int cchReturnBuffer: Size of return buffer.
  685. //
  686. // Return (int):
  687. // Number of bytes returned in buffer.
  688. //
  689. // History:
  690. // 10/19/93 Fwong Adding .ini file for app.
  691. //
  692. //--------------------------------------------------------------------------;
  693. int FNGLOBAL GetIniString
  694. (
  695. LPCTSTR pszSection,
  696. LPCTSTR pszEntry,
  697. LPTSTR pszBuffer,
  698. int cchReturnBuffer
  699. )
  700. {
  701. DWORD cbBytesCopied;
  702. LPTSTR pszShortName;
  703. if(NULL == pszAppIniFile)
  704. {
  705. return 0;
  706. }
  707. cbBytesCopied = (DWORD)GetPrivateProfileString(
  708. pszSection,
  709. pszEntry,
  710. TEXT(""),
  711. pszBuffer,
  712. cchReturnBuffer,
  713. pszAppIniFile);
  714. //
  715. // Checking for No Entry
  716. //
  717. if(cbBytesCopied != (DWORD)GetPrivateProfileString(
  718. pszSection,
  719. pszEntry,
  720. TEXT("a"),
  721. pszBuffer,
  722. cchReturnBuffer,
  723. pszAppIniFile))
  724. {
  725. //
  726. // Not in application directory, trying Windows directory.
  727. //
  728. pszShortName = pszAppIniFile;
  729. pszShortName += lstrlen(pszAppIniFile);
  730. for(;'\\' != pszShortName[0];pszShortName--);
  731. pszShortName++;
  732. cbBytesCopied = (DWORD)GetPrivateProfileString(
  733. pszSection,
  734. pszEntry,
  735. TEXT(""),
  736. pszBuffer,
  737. cchReturnBuffer,
  738. pszShortName);
  739. if(cbBytesCopied != (DWORD)GetPrivateProfileString(
  740. pszSection,
  741. pszEntry,
  742. TEXT("a"),
  743. pszBuffer,
  744. cchReturnBuffer,
  745. pszShortName))
  746. {
  747. pszBuffer[0] = 0;
  748. return 0;
  749. }
  750. }
  751. return ((int)cbBytesCopied);
  752. } // GetIniString()
  753. //--------------------------------------------------------------------------;
  754. //
  755. // DWORD GetIniDWORD
  756. //
  757. // Description:
  758. // Gets A DWORD from the .ini file for this app.
  759. //
  760. // Arguments:
  761. // LPCTSTR pszSection: Section name.
  762. //
  763. // LPCTSTR pszEntry: Entry name.
  764. //
  765. // DWORD dwDefault: Default value.
  766. //
  767. // Return (DWORD):
  768. // Value from .ini file or dwDefault if error occurs.
  769. //
  770. // History:
  771. // 11/24/93 Fwong Adding .ini file for app.
  772. //
  773. //--------------------------------------------------------------------------;
  774. DWORD FNGLOBAL GetIniDWORD
  775. (
  776. LPCTSTR pszSection,
  777. LPCTSTR pszEntry,
  778. DWORD dwDefault
  779. )
  780. {
  781. TCHAR szValue[12];
  782. if(NULL == pszAppIniFile)
  783. {
  784. return dwDefault;
  785. }
  786. if(0 == GetIniString(pszSection,pszEntry,szValue,SIZEOF(szValue)))
  787. {
  788. return dwDefault;
  789. }
  790. return atoDW(szValue,dwDefault);
  791. } // GetIniDWORD()
  792. //--------------------------------------------------------------------------;
  793. //
  794. // BOOL GetIniBin
  795. //
  796. // Description:
  797. // Reads binary information to the .ini file.
  798. //
  799. // Arguments:
  800. // LPCTSTR pszSection: Section name.
  801. //
  802. // LPCTSTR pszEntry: Entry name.
  803. //
  804. // LPVOID pvoid: Buffer to read into.
  805. //
  806. // DWORD cbSize: Size of buffer in bytes.
  807. //
  808. // Return (BOOL):
  809. // TRUE if successful, FALSE otherwise.
  810. //
  811. // History:
  812. // 01/07/94 Fwong Yet another random thing.
  813. //
  814. //--------------------------------------------------------------------------;
  815. BOOL FNGLOBAL GetIniBin
  816. (
  817. LPCTSTR pszSection,
  818. LPCTSTR pszEntry,
  819. LPVOID pvoid,
  820. DWORD cbSize
  821. )
  822. {
  823. TCHAR szEntry[MAXINISTR];
  824. TCHAR szScrap[MAXINISTR];
  825. LPBYTE pByte = pvoid;
  826. LPTSTR pszMiddle;
  827. UINT u;
  828. DWORD dw,dwDigit1,dwDigit2;
  829. if(NULL == pszAppIniFile)
  830. {
  831. return FALSE;
  832. }
  833. if(IsBadWritePtr(pvoid,(UINT)cbSize))
  834. {
  835. //
  836. // Can't fit data into buffer...
  837. //
  838. return FALSE;
  839. }
  840. dw = GetIniBinSize(pszSection,pszEntry);
  841. if(dw > cbSize)
  842. {
  843. //
  844. // Binary (in .ini file) is larger than given size...
  845. //
  846. return FALSE;
  847. }
  848. cbSize = dw;
  849. for (dw=0,u=0;dw < cbSize;u++)
  850. {
  851. //
  852. // Doing individual lines...
  853. //
  854. wsprintf(szEntry,TEXT("%s.%03u"),pszEntry,u);
  855. GetIniString(pszSection,szEntry,szScrap,MAXINISTR);
  856. pszMiddle = &(szScrap[0]);
  857. for (;;dw++,pszMiddle += 2)
  858. {
  859. dwDigit1 = HexValue(*pszMiddle);
  860. if(16 == dwDigit1)
  861. {
  862. break;
  863. }
  864. dwDigit2 = HexValue(*(pszMiddle+1));
  865. if(16 == dwDigit2)
  866. {
  867. return FALSE;
  868. }
  869. dwDigit2 += (dwDigit1 * 16);
  870. pByte[dw] = (BYTE)dwDigit2;
  871. }
  872. }
  873. return TRUE;
  874. } // GetIniBin()