Leaked source code of windows server 2003
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.

1601 lines
37 KiB

  1. /*++
  2. Copyright (c) 1994 Microsoft Corporation
  3. Module Name:
  4. registry.cxx
  5. Abstract:
  6. Functions to read/write registry parameters
  7. Contents:
  8. OpenInternetSettingsKey
  9. CloseInternetSettingsKey
  10. InternetGetComputerName
  11. InternetDeleteRegistryValue
  12. InternetReadRegistryDword
  13. InternetWriteRegistryDword
  14. InternetReadRegistryString
  15. InternetWriteRegistryString
  16. InternetReadRegistryBinary
  17. (InternetReadRegistryDwordKey)
  18. (InternetReadRegistryStringKey)
  19. (InternetReadRegistryBinaryKey)
  20. (InternetGetPrivateProfileString)
  21. (ReadRegistryOemString)
  22. (WriteRegistryDword)
  23. ReadRegistryDword
  24. Author:
  25. Richard L Firth (rfirth) 20-Mar-1995
  26. Environment:
  27. Win32(s) user-level DLL
  28. Revision History:
  29. 20-Mar-1995 rfirth
  30. Created
  31. --*/
  32. #include <wininetp.h>
  33. char vszDelimiters[] = ";, ";
  34. //
  35. // manifests
  36. //
  37. #define INTERNET_CLIENT_KEY "Internet Settings"
  38. #define SYSTEM_INI_FILE_NAME "SYSTEM.INI"
  39. #define NETWORK_SECTION_NAME "Network"
  40. #define COMPUTER_NAME_VALUE "ComputerName"
  41. #define PROFILE_INT_BUFFER_LENGTH 128
  42. #define MIME_TO_FILE_EXTENSION_KEY "MIME\\Database\\Content Type\\"
  43. #define EXTENSION_VALUE "Extension"
  44. //
  45. // macros
  46. //
  47. #define INTERNET_SETTINGS_KEY "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"
  48. #define INTERNET_CACHE_SETTINGS_KEY "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\5.0\\Cache"
  49. //
  50. // private prototypes
  51. //
  52. PRIVATE
  53. DWORD
  54. InternetReadRegistryDwordKey(
  55. IN HKEY ParameterKey,
  56. IN LPCSTR ParameterName,
  57. OUT LPDWORD ParameterValue,
  58. IN LPCSTR keyToReadFrom
  59. );
  60. PRIVATE
  61. DWORD
  62. InternetReadRegistryStringKey(
  63. IN HKEY ParameterKey,
  64. IN LPCSTR ParameterName,
  65. OUT LPSTR ParameterValue,
  66. IN OUT LPDWORD ParameterLength,
  67. IN LPCSTR keyToReadFrom
  68. );
  69. //PRIVATE
  70. //DWORD
  71. //InternetReadRegistryBinaryKey(
  72. // IN HKEY ParameterKey,
  73. // IN LPCSTR ParameterName,
  74. // OUT LPBYTE ParameterValue,
  75. // IN OUT LPDWORD ParameterLength
  76. // );
  77. //
  78. //PRIVATE
  79. //DWORD
  80. //InternetGetPrivateProfileString(
  81. // IN LPSTR IniFileName,
  82. // IN LPSTR SectionName,
  83. // IN LPCSTR ParameterName,
  84. // OUT LPSTR ParameterValue,
  85. // IN OUT LPDWORD ParameterLength
  86. // );
  87. PRIVATE
  88. DWORD
  89. ReadRegistryOemString(
  90. IN HKEY Key,
  91. IN LPCSTR ParameterName,
  92. OUT LPSTR String,
  93. IN OUT LPDWORD Length
  94. );
  95. PRIVATE
  96. DWORD
  97. WriteRegistryDword(
  98. IN HKEY Key,
  99. IN LPCSTR ParameterName,
  100. IN DWORD ParameterValue
  101. );
  102. //
  103. // private data
  104. //
  105. PRIVATE HKEY hKeyInternetSettings = NULL;
  106. //
  107. // functions
  108. //
  109. DWORD
  110. OpenInternetSettingsKey(
  111. VOID
  112. )
  113. /*++
  114. Routine Description:
  115. Opens registry key for Internet Settings branch
  116. Arguments:
  117. None.
  118. Return Value:
  119. DWORD
  120. Success - ERROR_SUCCESS
  121. Failure - ERROR_NOT_ENOUGH_MEMORY
  122. --*/
  123. {
  124. DEBUG_ENTER((DBG_REGISTRY,
  125. Dword,
  126. "OpenInternetSettingsKey",
  127. NULL
  128. ));
  129. DWORD error = ERROR_SUCCESS;
  130. if (!GeneralInitCritSec.Lock())
  131. {
  132. error = ERROR_NOT_ENOUGH_MEMORY;
  133. goto quit;
  134. }
  135. if (hKeyInternetSettings == NULL) {
  136. DWORD dwDisposition;
  137. REGCREATEKEYEX(HKEY_LOCAL_MACHINE,
  138. INTERNET_SETTINGS_KEY,
  139. 0, // reserved
  140. NULL, // class
  141. 0, // options
  142. KEY_READ | KEY_WRITE,
  143. NULL, // security attributes
  144. &hKeyInternetSettings,
  145. &dwDisposition
  146. );
  147. }
  148. GeneralInitCritSec.Unlock();
  149. quit:
  150. DEBUG_LEAVE(error);
  151. return error;
  152. }
  153. DWORD
  154. CloseInternetSettingsKey(
  155. VOID
  156. )
  157. /*++
  158. Routine Description:
  159. Closes Internet Settings registry key
  160. Arguments:
  161. None.
  162. Return Value:
  163. DWORD
  164. Success - ERROR_SUCCESS
  165. Failure -
  166. --*/
  167. {
  168. DEBUG_ENTER((DBG_REGISTRY,
  169. Dword,
  170. "CloseInternetSettingsKey",
  171. NULL
  172. ));
  173. DWORD error = ERROR_SUCCESS;
  174. if (hKeyInternetSettings != NULL) {
  175. error = REGCLOSEKEY(hKeyInternetSettings);
  176. hKeyInternetSettings = NULL;
  177. }
  178. DEBUG_LEAVE(error);
  179. return error;
  180. }
  181. PUBLIC
  182. DWORD
  183. InternetDeleteRegistryValue(
  184. IN LPSTR ParameterName
  185. )
  186. /*++
  187. Routine Description:
  188. Delets an entry from a the Internet Client registry key if the platform
  189. is NT/Win95.
  190. Arguments:
  191. ParameterName - name of the parameter to retrieve (e.g. AccessType)
  192. Return Value:
  193. DWORD
  194. Success - ERROR_SUCCESS
  195. Failure - ERROR_PATH_NOT_FOUND
  196. --*/
  197. {
  198. DWORD error;
  199. DEBUG_ENTER((DBG_REGISTRY,
  200. Dword,
  201. "InternetDeleteRegistryValue",
  202. "%q",
  203. ParameterName
  204. ));
  205. HKEY clientKey;
  206. //
  207. // open the registry key containing the Internet client values (this is
  208. // in the same place on NT and Win95)
  209. //
  210. error = REGOPENKEYEX(HKEY_CURRENT_USER,
  211. INTERNET_SETTINGS_KEY,
  212. 0, // reserved
  213. KEY_ALL_ACCESS,
  214. &clientKey
  215. );
  216. if (error == ERROR_SUCCESS) {
  217. error = RegDeleteValue(clientKey,
  218. ParameterName
  219. );
  220. REGCLOSEKEY(clientKey);
  221. }
  222. DEBUG_LEAVE(error);
  223. return error;
  224. }
  225. DWORD
  226. InternetReadRegistryDword(
  227. IN LPCSTR ParameterName,
  228. OUT LPDWORD ParameterValue
  229. )
  230. /*++
  231. Routine Description:
  232. Reads a single DWORD from a the Internet Client registry key if the platform
  233. is NT/Win95, else reads the value from SYSTEM.INI if we are running on Win32s
  234. Arguments:
  235. ParameterName - name of the parameter to retrieve (e.g. AccessType)
  236. ParameterValue - pointer to place to store retrieved value
  237. Return Value:
  238. DWORD
  239. Success - ERROR_SUCCESS
  240. Failure - ERROR_PATH_NOT_FOUND
  241. --*/
  242. {
  243. DEBUG_ENTER((DBG_REGISTRY,
  244. Dword,
  245. "InternetReadRegistryDword",
  246. "%q, %x",
  247. ParameterName,
  248. ParameterValue
  249. ));
  250. DWORD error = InternetReadRegistryDwordKey(HKEY_LOCAL_MACHINE,
  251. ParameterName,
  252. ParameterValue
  253. );
  254. DEBUG_LEAVE(error);
  255. return error;
  256. }
  257. #ifdef WININET6
  258. DWORD
  259. InternetIDEWriteRegistryDword(
  260. IN LPCSTR ParameterName,
  261. IN DWORD ParameterValue
  262. )
  263. /*++
  264. Routine Description:
  265. Writes a single DWORD from to the Internet Client registry key if the platform
  266. is NT/Win95, otherwise it fails.
  267. Arguments:
  268. ParameterName - name of the parameter to retrieve (e.g. AccessType)
  269. ParameterValue - value to store in registry.
  270. Return Value:
  271. DWORD
  272. Success - ERROR_SUCCESS
  273. Failure - ERROR_PATH_NOT_FOUND
  274. --*/
  275. {
  276. DEBUG_ENTER((DBG_REGISTRY,
  277. Dword,
  278. "InternetIDEWriteRegistryDword",
  279. "%q, %x",
  280. ParameterName,
  281. ParameterValue
  282. ));
  283. DWORD error = GlobalIdentity
  284. ? WriteIDRegDword(ParameterName, ParameterValue)
  285. : InternetWriteRegistryDword(ParameterName, ParameterValue);
  286. DEBUG_LEAVE(error);
  287. return error;
  288. }
  289. DWORD
  290. InternetIDEReadRegistryDword(
  291. IN LPCSTR ParameterName,
  292. OUT LPDWORD ParameterValue
  293. )
  294. /*++
  295. Routine Description:
  296. If we're in an identity-mode, we'll read from the special location.
  297. Otherwise, read from the old location.
  298. Arguments:
  299. ParameterName - name of the parameter to retrieve (e.g. AccessType)
  300. ParameterValue - pointer to place to store retrieved value
  301. Return Value:
  302. DWORD
  303. Success - ERROR_SUCCESS
  304. Failure - ERROR_PATH_NOT_FOUND
  305. --*/
  306. {
  307. DEBUG_ENTER((DBG_REGISTRY,
  308. Dword,
  309. "InternetIDEReadRegistryDword",
  310. "%q, %x",
  311. ParameterName,
  312. ParameterValue
  313. ));
  314. DWORD error = GlobalIdentity
  315. ? ReadIDRegDword(ParameterName, ParameterValue)
  316. : InternetReadRegistryDwordKey(HKEY_CURRENT_USER, ParameterName, ParameterValue);
  317. DEBUG_LEAVE(error);
  318. return error;
  319. }
  320. #endif
  321. DWORD
  322. InternetCacheReadRegistryDword(
  323. IN LPCSTR ParameterName,
  324. OUT LPDWORD ParameterValue
  325. )
  326. /*++
  327. Routine Description:
  328. Reads a single DWORD from a the Internet Client registry key if the platform
  329. is NT/Win95, else reads the value from SYSTEM.INI if we are running on Win32s
  330. Arguments:
  331. ParameterName - name of the parameter to retrieve (e.g. AccessType)
  332. ParameterValue - pointer to place to store retrieved value
  333. Return Value:
  334. DWORD
  335. Success - ERROR_SUCCESS
  336. Failure - ERROR_PATH_NOT_FOUND
  337. --*/
  338. {
  339. DEBUG_ENTER((DBG_REGISTRY,
  340. Dword,
  341. "InternetCacheReadRegistryDword",
  342. "%q, %x",
  343. ParameterName,
  344. ParameterValue
  345. ));
  346. DWORD error = ERROR_SUCCESS;
  347. HKEY clientKey;
  348. error = REGOPENKEYEX(HKEY_CURRENT_USER,
  349. INTERNET_CACHE_SETTINGS_KEY,
  350. 0, // reserved
  351. KEY_QUERY_VALUE,
  352. &clientKey
  353. );
  354. if (error == ERROR_SUCCESS) {
  355. error = ReadRegistryDword(clientKey,
  356. ParameterName,
  357. ParameterValue
  358. );
  359. REGCLOSEKEY(clientKey);
  360. }
  361. DEBUG_LEAVE(error);
  362. return error;
  363. }
  364. DWORD
  365. InternetWriteRegistryDword(
  366. IN LPCSTR ParameterName,
  367. IN DWORD ParameterValue
  368. )
  369. /*++
  370. Routine Description:
  371. Writes a single DWORD from to the Internet Client registry key if the platform
  372. is NT/Win95, otherwise it fails.
  373. Arguments:
  374. ParameterName - name of the parameter to retrieve (e.g. AccessType)
  375. ParameterValue - value to store in registry.
  376. Return Value:
  377. DWORD
  378. Success - ERROR_SUCCESS
  379. Failure - ERROR_PATH_NOT_FOUND
  380. --*/
  381. {
  382. DEBUG_ENTER((DBG_REGISTRY,
  383. Dword,
  384. "InternetWriteRegistryDword",
  385. "%q, %x",
  386. ParameterName,
  387. ParameterValue
  388. ));
  389. DWORD error;
  390. if (hKeyInternetSettings != NULL) {
  391. error = WriteRegistryDword(hKeyInternetSettings,
  392. ParameterName,
  393. ParameterValue
  394. );
  395. } else {
  396. error = ERROR_SUCCESS;
  397. }
  398. DEBUG_PRINT(REGISTRY,
  399. INFO,
  400. ("InternetWriteRegistryDword(%q): value = %d (%#x)\n",
  401. ParameterName,
  402. ParameterValue,
  403. ParameterValue
  404. ));
  405. DEBUG_LEAVE(error);
  406. return error;
  407. }
  408. DWORD
  409. InternetReadRegistryString(
  410. IN LPCSTR ParameterName,
  411. OUT LPSTR ParameterValue,
  412. IN OUT LPDWORD ParameterLength
  413. )
  414. /*++
  415. Routine Description:
  416. Reads a string from the Internet Client registry key on NT/Win95, or reads
  417. the corresponding value from SYSTEM.INI on a Win32s platform
  418. Arguments:
  419. ParameterName - name of value parameter within key (e.g. EmailName)
  420. ParameterValue - pointer to string buffer for returned string
  421. ParameterLength - IN: number of bytes in ParameterValue
  422. OUT: number of bytes in string (excluding trailing '\0')
  423. Return Value:
  424. DWORD
  425. Success - ERROR_SUCCESS
  426. Failure - ERROR_PATH_NOT_FOUND
  427. --*/
  428. {
  429. DEBUG_ENTER((DBG_REGISTRY,
  430. Dword,
  431. "InternetReadRegistryString",
  432. "%q, %x, %x [%d]",
  433. ParameterName,
  434. ParameterValue,
  435. ParameterLength,
  436. *ParameterLength
  437. ));
  438. DWORD error = InternetReadRegistryStringKey(HKEY_CURRENT_USER,
  439. ParameterName,
  440. ParameterValue,
  441. ParameterLength
  442. );
  443. DEBUG_LEAVE(error);
  444. return error;
  445. }
  446. //
  447. //DWORD
  448. //InternetWriteRegistryString(
  449. // IN LPCSTR ParameterName,
  450. // IN LPSTR ParameterValue
  451. // )
  452. //
  453. ///*++
  454. //
  455. //Routine Description:
  456. //
  457. // Writes a string to the Internet Client registry key on NT/Win95, or writes
  458. // the corresponding value to SYSTEM.INI on Win32s platform
  459. //
  460. //Arguments:
  461. //
  462. // ParameterName - name of value parameter within key (e.g. EmailName)
  463. //
  464. // ParameterValue - pointer to string to write
  465. //
  466. //Return Value:
  467. //
  468. // DWORD
  469. // Success - ERROR_SUCCESS
  470. //
  471. // Failure -
  472. //
  473. //--*/
  474. //
  475. //{
  476. // DEBUG_ENTER((DBG_REGISTRY,
  477. // Dword,
  478. // "InternetWriteRegistryString",
  479. // "%.40q, %.80q",
  480. // ParameterName,
  481. // ParameterValue
  482. // ));
  483. //
  484. // DWORD error;
  485. //
  486. // if (IsPlatformWin32s()) {
  487. //
  488. // BOOL ok;
  489. //
  490. // ok = WritePrivateProfileString(INTERNET_CLIENT_KEY,
  491. // ParameterName,
  492. // ParameterValue,
  493. // SYSTEM_INI_FILE_NAME
  494. // );
  495. // error = ok ? ERROR_SUCCESS : GetLastError();
  496. // } else {
  497. //
  498. // //
  499. // // BUGBUG - currently, nothing needs to write to registry if NT or Win95
  500. // //
  501. //
  502. // INET_ASSERT(FALSE);
  503. //
  504. // }
  505. //
  506. // DEBUG_LEAVE(error);
  507. //
  508. // return error;
  509. //}
  510. //
  511. //
  512. //DWORD
  513. //InternetReadRegistryBinary(
  514. // IN LPCSTR ParameterName,
  515. // OUT LPBYTE ParameterValue,
  516. // IN OUT LPDWORD ParameterLength
  517. // )
  518. //
  519. ///*++
  520. //
  521. //Routine Description:
  522. //
  523. // Reads a binary value from the Internet Client registry key on NT/Win95, or
  524. // reads the corresponding value from SYSTEM.INI on a Win32s platform
  525. //
  526. //Arguments:
  527. //
  528. // ParameterName - name of value parameter within key (e.g. EmailName)
  529. //
  530. // ParameterValue - pointer to buffer for returned data
  531. //
  532. // ParameterLength - IN: number of bytes in ParameterValue
  533. // OUT: number of bytes in buffer, or required size
  534. //
  535. //Return Value:
  536. //
  537. // DWORD
  538. // Success - ERROR_SUCCESS
  539. //
  540. // Failure - ERROR_PATH_NOT_FOUND
  541. // The parameter wasn't found
  542. //
  543. // ERROR_MORE_DATA
  544. // The buffer isn't large enough
  545. //
  546. //--*/
  547. //
  548. //{
  549. // DEBUG_ENTER((DBG_REGISTRY,
  550. // Dword,
  551. // "InternetReadRegistryBinary",
  552. // "%q, %#x, %#x [%d]",
  553. // ParameterName,
  554. // ParameterValue,
  555. // ParameterLength,
  556. // *ParameterLength
  557. // ));
  558. //
  559. // DWORD error;
  560. //
  561. // error = InternetReadRegistryBinaryKey(HKEY_CURRENT_USER,
  562. // ParameterName,
  563. // ParameterValue,
  564. // ParameterLength
  565. // );
  566. //
  567. // DEBUG_LEAVE(error);
  568. //
  569. // return error;
  570. //}
  571. //
  572. // private functions
  573. //
  574. PUBLIC
  575. DWORD
  576. InternetReadRegistryDwordKey(
  577. IN HKEY ParameterKey,
  578. IN LPCSTR ParameterName,
  579. OUT LPDWORD ParameterValue,
  580. IN LPCSTR keyToReadFrom
  581. )
  582. /*++
  583. Routine Description:
  584. Reads a single DWORD from a the Internet Client registry key if the platform
  585. is NT/Win95, else reads the value from SYSTEM.INI if we are running on Win32s.
  586. Does not modify the *ParameterValue if the registry variable cannot be read
  587. Arguments:
  588. ParameterKey - root registry key (e.g. HKEY_CURRENT_USER)
  589. ParameterName - name of the parameter to retrieve (e.g. AccessType)
  590. ParameterValue - pointer to place to store retrieved value
  591. Return Value:
  592. DWORD
  593. Success - ERROR_SUCCESS
  594. Failure - ERROR_PATH_NOT_FOUND
  595. --*/
  596. {
  597. DEBUG_ENTER((DBG_REGISTRY,
  598. Dword,
  599. "InternetReadRegistryDwordKey",
  600. "%s, %q, %x",
  601. (ParameterKey == HKEY_LOCAL_MACHINE)
  602. ? "HKEY_LOCAL_MACHINE"
  603. : (ParameterKey == HKEY_CURRENT_USER)
  604. ? "HKEY_CURRENT_USER"
  605. : "???",
  606. ParameterName,
  607. ParameterValue
  608. ));
  609. DWORD error = ERROR_SUCCESS;
  610. HKEY clientKey = hKeyInternetSettings;
  611. if (ParameterKey != HKEY_CURRENT_USER) {
  612. error = REGOPENKEYEX(ParameterKey,
  613. keyToReadFrom,
  614. 0, // reserved
  615. KEY_QUERY_VALUE,
  616. &clientKey
  617. );
  618. } else if (clientKey == NULL) {
  619. error = ERROR_PATH_NOT_FOUND;
  620. }
  621. if (error == ERROR_SUCCESS) {
  622. error = ReadRegistryDword(clientKey,
  623. ParameterName,
  624. ParameterValue
  625. );
  626. if (clientKey != hKeyInternetSettings) {
  627. REGCLOSEKEY(clientKey);
  628. }
  629. }
  630. DEBUG_PRINT(REGISTRY,
  631. INFO,
  632. ("InternetReadRegistryDwordKey(%q): value = %d (%#x)\n",
  633. ParameterName,
  634. *ParameterValue,
  635. *ParameterValue
  636. ));
  637. DEBUG_LEAVE(error);
  638. return error;
  639. }
  640. PRIVATE
  641. DWORD
  642. InternetReadRegistryStringKey(
  643. IN HKEY ParameterKey,
  644. IN LPCSTR ParameterName,
  645. OUT LPSTR ParameterValue,
  646. IN OUT LPDWORD ParameterLength,
  647. IN LPCSTR keyToReadFrom
  648. )
  649. /*++
  650. Routine Description:
  651. Reads a string from the Internet Client registry key on NT/Win95, or reads
  652. the corresponding value from SYSTEM.INI on a Win32s platform
  653. Arguments:
  654. ParameterKey - root registry key (e.g. HKEY_LOCAL_MACHINE)
  655. ParameterName - name of value parameter within key (e.g. EmailName)
  656. ParameterValue - pointer to string buffer for returned string
  657. ParameterLength - IN: number of bytes in ParameterValue
  658. OUT: number of bytes in string (excluding trailing '\0')
  659. Return Value:
  660. DWORD
  661. Success - ERROR_SUCCESS
  662. Failure - ERROR_PATH_NOT_FOUND
  663. --*/
  664. {
  665. DEBUG_ENTER((DBG_REGISTRY,
  666. Dword,
  667. "InternetReadRegistryStringKey",
  668. "%s (%x), %q, %x, %x [%d]",
  669. (ParameterKey == HKEY_LOCAL_MACHINE)
  670. ? "HKEY_LOCAL_MACHINE"
  671. : (ParameterKey == HKEY_CURRENT_USER)
  672. ? "HKEY_CURRENT_USER"
  673. : "???",
  674. ParameterKey,
  675. ParameterName,
  676. ParameterValue,
  677. ParameterLength,
  678. *ParameterLength
  679. ));
  680. //
  681. // zero-terminate the string
  682. //
  683. if (*ParameterLength > 0) {
  684. *ParameterValue = '\0';
  685. }
  686. DWORD error = ERROR_SUCCESS;
  687. HKEY clientKey = hKeyInternetSettings;
  688. if (ParameterKey != HKEY_CURRENT_USER) {
  689. error = REGOPENKEYEX(ParameterKey,
  690. keyToReadFrom,
  691. 0, // reserved
  692. KEY_QUERY_VALUE,
  693. &clientKey
  694. );
  695. } else if (clientKey == NULL) {
  696. error = ERROR_PATH_NOT_FOUND;
  697. }
  698. if (error == ERROR_SUCCESS) {
  699. error = ReadRegistryOemString(clientKey,
  700. ParameterName,
  701. ParameterValue,
  702. ParameterLength
  703. );
  704. if (clientKey != hKeyInternetSettings) {
  705. REGCLOSEKEY(clientKey);
  706. }
  707. }
  708. DEBUG_PRINT(REGISTRY,
  709. INFO,
  710. ("InternetReadRegistryStringKey(%q): value = %q\n",
  711. ParameterName,
  712. ParameterValue
  713. ));
  714. DEBUG_LEAVE(error);
  715. return error;
  716. }
  717. //
  718. //PRIVATE
  719. //DWORD
  720. //InternetReadRegistryBinaryKey(
  721. // IN HKEY ParameterKey,
  722. // IN LPCSTR ParameterName,
  723. // OUT LPBYTE ParameterValue,
  724. // IN OUT LPDWORD ParameterLength
  725. // )
  726. //
  727. ///*++
  728. //
  729. //Routine Description:
  730. //
  731. // Reads a binary value from the Internet Client registry key on NT/Win95, or
  732. // reads the corresponding value from SYSTEM.INI on a Win32s platform
  733. //
  734. //Arguments:
  735. //
  736. // ParameterKey - root registry key (e.g. HKEY_LOCAL_MACHINE)
  737. //
  738. // ParameterName - name of value parameter within key (e.g. EmailName)
  739. //
  740. // ParameterValue - pointer to buffer for returned data
  741. //
  742. // ParameterLength - IN: number of bytes in ParameterValue
  743. // OUT: number of bytes in buffer, or required size
  744. //
  745. //Return Value:
  746. //
  747. // DWORD
  748. // Success - ERROR_SUCCESS
  749. //
  750. // Failure - ERROR_PATH_NOT_FOUND
  751. // The parameter wasn't found
  752. //
  753. // ERROR_MORE_DATA
  754. // The buffer isn't large enough
  755. //
  756. //--*/
  757. //
  758. //{
  759. // DEBUG_ENTER((DBG_REGISTRY,
  760. // Dword,
  761. // "InternetReadRegistryBinaryKey",
  762. // "%s (%x), %q, %#x, %#x [%d]",
  763. // (ParameterKey == HKEY_LOCAL_MACHINE)
  764. // ? "HKEY_LOCAL_MACHINE"
  765. // : (ParameterKey == HKEY_CURRENT_USER)
  766. // ? "HKEY_CURRENT_USER"
  767. // : "???",
  768. // ParameterKey,
  769. // ParameterName,
  770. // ParameterValue,
  771. // ParameterLength,
  772. // *ParameterLength
  773. // ));
  774. //
  775. // DWORD error;
  776. // HKEY clientKey;
  777. //
  778. // //
  779. // // open the registry key containing the Internet client values (this is
  780. // // in the same place on NT and Win95)
  781. // //
  782. //
  783. // error = REGOPENKEYEX(ParameterKey,
  784. // INTERNET_SETTINGS_KEY,
  785. // 0, // reserved
  786. // KEY_QUERY_VALUE,
  787. // &clientKey
  788. // );
  789. //
  790. // if (error == ERROR_SUCCESS) {
  791. //
  792. // DWORD valueType;
  793. //
  794. // error = RegQueryValueEx(clientKey,
  795. // ParameterName,
  796. // NULL, // reserved
  797. // &valueType,
  798. // ParameterValue,
  799. // ParameterLength
  800. // );
  801. // REGCLOSEKEY(clientKey);
  802. // }
  803. //
  804. // DEBUG_PRINT(REGISTRY,
  805. // INFO,
  806. // ("InternetReadRegistryBinaryKey(%q): length = %d\n",
  807. // ParameterName,
  808. // *ParameterLength
  809. // ));
  810. //
  811. // DEBUG_LEAVE(error);
  812. //
  813. // return error;
  814. //}
  815. //
  816. //
  817. //PRIVATE
  818. //DWORD
  819. //InternetGetPrivateProfileString(
  820. // IN LPSTR IniFileName,
  821. // IN LPSTR SectionName,
  822. // IN LPCSTR ParameterName,
  823. // OUT LPSTR ParameterValue,
  824. // IN OUT LPDWORD ParameterLength
  825. // )
  826. //
  827. ///*++
  828. //
  829. //Routine Description:
  830. //
  831. // Reads an string out of an INI file. Mainly just for Win32s
  832. //
  833. //Arguments:
  834. //
  835. // IniFileName - name of INI file to read
  836. //
  837. // SectionName - name of section in INI file to read
  838. //
  839. // ParameterName - name of entry in section to read
  840. //
  841. // ParameterValue - returned string
  842. //
  843. // ParameterLength - IN: Length of ParameterValue
  844. // OUT: Number of characters in ParameterValue, excluding
  845. // terminating NUL
  846. //
  847. //Return Value:
  848. //
  849. // DWORD
  850. // Success - ERROR_SUCCESS
  851. //
  852. // Failure - ERROR_PATH_NOT_FOUND
  853. // ERROR_FILE_NOT_FOUND
  854. //
  855. //--*/
  856. //
  857. //{
  858. // DWORD error;
  859. // DWORD nChars;
  860. //
  861. // nChars = GetPrivateProfileString(SectionName,
  862. // ParameterName,
  863. // "", // lpszDefault
  864. // ParameterValue,
  865. // *ParameterLength,
  866. // IniFileName
  867. // );
  868. // if (nChars > 0) {
  869. // *ParameterLength = nChars;
  870. // error = ERROR_SUCCESS;
  871. // } else {
  872. // error = ERROR_PATH_NOT_FOUND;
  873. // }
  874. // return error;
  875. //}
  876. PRIVATE
  877. DWORD
  878. ReadRegistryOemString(
  879. IN HKEY Key,
  880. IN LPCSTR ParameterName,
  881. OUT LPSTR String,
  882. IN OUT LPDWORD Length
  883. )
  884. /*++
  885. Routine Description:
  886. Reads a string out of the registry as an OEM string
  887. Arguments:
  888. Key - open registry key where to read value from
  889. ParameterName - name of registry value to read
  890. String - place to put it
  891. Length - IN: length of String buffer in characters
  892. OUT: length of String in characters, as if returned from
  893. strlen()
  894. Return Value:
  895. DWORD
  896. Success - ERROR_SUCCESS
  897. Failure - ERROR_FILE_NOT_FOUND
  898. Couldn't find the parameter
  899. ERROR_PATH_NOT_FOUND
  900. Couldn't find the parameter
  901. --*/
  902. {
  903. DEBUG_ENTER((DBG_REGISTRY,
  904. Dword,
  905. "ReadRegistryOemString",
  906. "%#x, %q, %#x, %#x [%d]",
  907. Key,
  908. ParameterName,
  909. String,
  910. Length,
  911. *Length
  912. ));
  913. LONG error;
  914. DWORD valueType;
  915. DWORD valueLength;
  916. //
  917. // first, get the length of the string
  918. //
  919. valueLength = *Length;
  920. error = RegQueryValueEx(Key,
  921. ParameterName,
  922. NULL, // reserved
  923. &valueType,
  924. (LPBYTE)String,
  925. &valueLength
  926. );
  927. if (error != ERROR_SUCCESS) {
  928. goto quit;
  929. }
  930. //
  931. // we only support REG_SZ (single string) values in this function
  932. //
  933. if (valueType != REG_SZ) {
  934. error = ERROR_PATH_NOT_FOUND;
  935. goto quit;
  936. }
  937. //
  938. // if 1 or 0 chars returned then the string is empty
  939. //
  940. if (valueLength <= sizeof(char)) {
  941. error = ERROR_PATH_NOT_FOUND;
  942. goto quit;
  943. }
  944. //
  945. // convert the ANSI string to OEM character set in place. According to Win
  946. // help, this always succeeds
  947. //
  948. CharToOem(String, String);
  949. //
  950. // return the length as if returned from strlen() (i.e. drop the '\0')
  951. //
  952. *Length = valueLength - sizeof(char);
  953. DEBUG_PRINT(REGISTRY,
  954. INFO,
  955. ("ReadRegistryOemString(%q) returning %q (%d chars)\n",
  956. ParameterName,
  957. String,
  958. *Length
  959. ));
  960. quit:
  961. DEBUG_LEAVE(error);
  962. return error;
  963. }
  964. DWORD
  965. ReadRegistryDword(
  966. IN HKEY Key,
  967. IN LPCSTR ParameterName,
  968. OUT LPDWORD ParameterValue
  969. )
  970. /*++
  971. Routine Description:
  972. Reads a DWORD parameter from the registry
  973. Won't modify *ParameterValue unless a valid value is read from the registry
  974. Arguments:
  975. Key - handle of open registry key where parameter resides
  976. ParameterName - name of DWORD parameter to read
  977. ParameterValue - returned DWORD parameter read from registry
  978. Return Value:
  979. DWORD
  980. Success - ERROR_SUCCESS
  981. Failure - ERROR_PATH_NOT_FOUND
  982. One of the following occurred:
  983. - the parameter is not in the specified registry key
  984. - the parameter is the wrong type
  985. - the parameter is the wrong size
  986. --*/
  987. {
  988. DEBUG_ENTER((DBG_REGISTRY,
  989. Dword,
  990. "ReadRegistryDword",
  991. "%x, %q, %x",
  992. Key,
  993. ParameterName,
  994. ParameterValue
  995. ));
  996. DWORD error;
  997. DWORD valueLength;
  998. DWORD valueType;
  999. DWORD value;
  1000. valueLength = sizeof(*ParameterValue);
  1001. error = (DWORD)RegQueryValueEx(Key,
  1002. ParameterName,
  1003. NULL, // reserved
  1004. &valueType,
  1005. (LPBYTE)&value,
  1006. &valueLength
  1007. );
  1008. //
  1009. // if the size or type aren't correct then return an error, else only if
  1010. // success was returned do we modify *ParameterValue
  1011. //
  1012. if (error == ERROR_SUCCESS) {
  1013. if (((valueType != REG_DWORD)
  1014. && (valueType != REG_BINARY))
  1015. || (valueLength != sizeof(DWORD))) {
  1016. DEBUG_PRINT(REGISTRY,
  1017. ERROR,
  1018. ("valueType = %d, valueLength = %d\n",
  1019. valueType,
  1020. valueLength
  1021. ));
  1022. error = ERROR_PATH_NOT_FOUND;
  1023. } else {
  1024. *ParameterValue = value;
  1025. }
  1026. }
  1027. DEBUG_LEAVE(error);
  1028. return error;
  1029. }
  1030. PRIVATE
  1031. DWORD
  1032. WriteRegistryDword(
  1033. IN HKEY Key,
  1034. IN LPCSTR ParameterName,
  1035. IN DWORD ParameterValue
  1036. )
  1037. /*++
  1038. Routine Description:
  1039. Writes a DWORD parameter from the registry
  1040. Will write ParameterValue to the key.
  1041. Arguments:
  1042. Key - handle of open registry key where parameter resides
  1043. ParameterName - name of DWORD parameter to write
  1044. ParameterValue - DWORD parameter to write from registry
  1045. Return Value:
  1046. DWORD
  1047. Success - ERROR_SUCCESS
  1048. Failure - ERROR_PATH_NOT_FOUND
  1049. One of the following occurred:
  1050. - the parameter is not in the specified registry key
  1051. - the parameter is the wrong type
  1052. - the parameter is the wrong size
  1053. --*/
  1054. {
  1055. DEBUG_ENTER((DBG_REGISTRY,
  1056. Dword,
  1057. "WriteRegistryDword",
  1058. "%x, %q, %x",
  1059. Key,
  1060. ParameterName,
  1061. ParameterValue
  1062. ));
  1063. DWORD error;
  1064. DWORD valueLength;
  1065. DWORD valueType;
  1066. DWORD value;
  1067. valueLength = sizeof(ParameterValue);
  1068. valueType = REG_DWORD;
  1069. value = ParameterValue;
  1070. error = (DWORD)RegSetValueEx(Key,
  1071. ParameterName,
  1072. NULL, // reserved
  1073. valueType,
  1074. (LPBYTE)&value,
  1075. valueLength
  1076. );
  1077. DEBUG_PRINT(REGISTRY,
  1078. INFO,
  1079. ("added: valueType = %d, valueLength = %d\n",
  1080. valueType,
  1081. valueLength
  1082. ));
  1083. DEBUG_LEAVE(error);
  1084. return error;
  1085. }
  1086. #if INET_DEBUG
  1087. typedef struct {
  1088. LIST_ENTRY entry;
  1089. HKEY hkey;
  1090. char * file;
  1091. int line;
  1092. char name[1];
  1093. } DBGREGKEYINFO;
  1094. SERIALIZED_LIST DbgRegKeyList;
  1095. VOID DbgRegKey_Init(VOID) {
  1096. InitializeSerializedList(&DbgRegKeyList);
  1097. }
  1098. VOID DbgRegKey_Terminate(VOID) {
  1099. TerminateSerializedList(&DbgRegKeyList);
  1100. }
  1101. void regkey_add(const char * name, HKEY hkey, char * file, int line) {
  1102. if (!name) {
  1103. name = "";
  1104. }
  1105. int len = lstrlen(name);
  1106. DBGREGKEYINFO * p = (DBGREGKEYINFO *)ALLOCATE_FIXED_MEMORY(sizeof(DBGREGKEYINFO) + len);
  1107. if (p) {
  1108. //dprintf("Wininet.DbgRegKey: adding %q\n", name);
  1109. memcpy(p->name, name, len + 1);
  1110. p->line = line;
  1111. p->file = file;
  1112. p->hkey = hkey;
  1113. InsertAtHeadOfSerializedList(&DbgRegKeyList, &p->entry);
  1114. }
  1115. }
  1116. void regkey_remove(HKEY hkey) {
  1117. if (LockSerializedList(&DbgRegKeyList))
  1118. {
  1119. DBGREGKEYINFO * p = (DBGREGKEYINFO *)HeadOfSerializedList(&DbgRegKeyList);
  1120. while (p != (DBGREGKEYINFO *)SlSelf(&DbgRegKeyList)) {
  1121. if (p->hkey == hkey) {
  1122. RemoveFromSerializedList(&DbgRegKeyList, (PLIST_ENTRY)p);
  1123. //dprintf("Wininet.DbgRegKey: removing %q\n", p->name);
  1124. FREE_MEMORY(p);
  1125. break;
  1126. }
  1127. p = (DBGREGKEYINFO *)p->entry.Flink;
  1128. }
  1129. UnlockSerializedList(&DbgRegKeyList);
  1130. }
  1131. }
  1132. char * regkey_name(HKEY hkey, const char * subname) {
  1133. switch ((INT_PTR)hkey) {
  1134. case (INT_PTR)HKEY_CLASSES_ROOT:
  1135. return NEW_STRING("HKEY_CLASSES_ROOT");
  1136. case (INT_PTR)HKEY_CURRENT_USER:
  1137. return NEW_STRING("HKEY_CURRENT_USER");
  1138. case (INT_PTR)HKEY_LOCAL_MACHINE:
  1139. return NEW_STRING("HKEY_LOCAL_MACHINE");
  1140. case (INT_PTR)HKEY_USERS:
  1141. return NEW_STRING("HKEY_USERS");
  1142. case (INT_PTR)HKEY_PERFORMANCE_DATA:
  1143. return NEW_STRING("HKEY_PERFORMANCE_DATA");
  1144. case (INT_PTR)HKEY_CURRENT_CONFIG:
  1145. return NEW_STRING("HKEY_CURRENT_CONFIG");
  1146. case (INT_PTR)HKEY_DYN_DATA:
  1147. return NEW_STRING("HKEY_DYN_DATA");
  1148. }
  1149. char * name = NULL;
  1150. if (LockSerializedList(&DbgRegKeyList))
  1151. {
  1152. DBGREGKEYINFO * p = (DBGREGKEYINFO *)HeadOfSerializedList(&DbgRegKeyList);
  1153. while (p != (DBGREGKEYINFO *)SlSelf(&DbgRegKeyList)) {
  1154. if (p->hkey == hkey) {
  1155. int len = lstrlen(p->name);
  1156. int slen = lstrlen(subname);
  1157. name = (char *)ALLOCATE_FIXED_MEMORY(len + 1 + slen + 1);
  1158. if (name) {
  1159. memcpy(name, p->name, len);
  1160. name[len] = '\\';
  1161. memcpy(name + len + 1, subname, slen + 1);
  1162. }
  1163. break;
  1164. }
  1165. p = (DBGREGKEYINFO *)p->entry.Flink;
  1166. }
  1167. UnlockSerializedList(&DbgRegKeyList);
  1168. }
  1169. return name;
  1170. }
  1171. void regkey_freename(char * name) {
  1172. if (name) {
  1173. FREE_MEMORY(name);
  1174. }
  1175. }
  1176. LONG
  1177. DbgRegOpenKey(
  1178. IN HKEY hKey,
  1179. IN LPCTSTR lpszSubKey,
  1180. OUT PHKEY phkResult,
  1181. char * file,
  1182. int line
  1183. )
  1184. {
  1185. char * keyname = regkey_name(hKey, lpszSubKey);
  1186. LONG rc = RegOpenKey(hKey, lpszSubKey, phkResult);
  1187. if (rc == 0) {
  1188. regkey_add(keyname, *phkResult, file, line);
  1189. }
  1190. regkey_freename(keyname);
  1191. return rc;
  1192. }
  1193. LONG
  1194. DbgRegOpenKeyEx(
  1195. IN HKEY hKey,
  1196. IN LPCSTR lpSubKey,
  1197. IN DWORD ulOptions,
  1198. IN REGSAM samDesired,
  1199. OUT PHKEY phkResult,
  1200. char * file,
  1201. int line
  1202. )
  1203. {
  1204. char * keyname = regkey_name(hKey, lpSubKey);
  1205. LONG rc = RegOpenKeyEx(hKey, lpSubKey, ulOptions, samDesired, phkResult);
  1206. if (rc == 0) {
  1207. regkey_add(keyname, *phkResult, file, line);
  1208. }
  1209. regkey_freename(keyname);
  1210. return rc;
  1211. }
  1212. LONG
  1213. DbgRegCreateKeyEx(
  1214. IN HKEY hKey,
  1215. IN LPCSTR lpSubKey,
  1216. IN DWORD Reserved,
  1217. IN LPSTR lpClass,
  1218. IN DWORD dwOptions,
  1219. IN REGSAM samDesired,
  1220. IN LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  1221. OUT PHKEY phkResult,
  1222. OUT LPDWORD lpdwDisposition,
  1223. char * file,
  1224. int line
  1225. )
  1226. {
  1227. char * keyname = regkey_name(hKey, lpSubKey);
  1228. LONG rc = RegCreateKeyEx(hKey, lpSubKey, Reserved, lpClass, dwOptions, samDesired, lpSecurityAttributes, phkResult, lpdwDisposition);
  1229. if (rc == 0) {
  1230. regkey_add(keyname, *phkResult, file, line);
  1231. }
  1232. regkey_freename(keyname);
  1233. return rc;
  1234. }
  1235. LONG
  1236. DbgRegCloseKey(
  1237. IN HKEY hKey
  1238. )
  1239. {
  1240. LONG rc = RegCloseKey(hKey);
  1241. if (rc == 0) {
  1242. regkey_remove(hKey);
  1243. }
  1244. return rc;
  1245. }
  1246. #endif // INET_DEBUG