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.

1320 lines
30 KiB

  1. /*++
  2. Module Name:
  3. registry.cxx
  4. Abstract:
  5. Functions to read/write registry parameters
  6. Author:
  7. Venkatraman Kudallur (venkatk)
  8. Revision History:
  9. 3-10-2000 venkatk
  10. Created
  11. --*/
  12. #ifdef ENABLE_DEBUG
  13. #include <urlint.h>
  14. #include "registry.h"
  15. //
  16. // manifests
  17. //
  18. #define INTERNET_CLIENT_KEY "Internet Settings"
  19. #define SYSTEM_INI_FILE_NAME "SYSTEM.INI"
  20. #define NETWORK_SECTION_NAME "Network"
  21. #define COMPUTER_NAME_VALUE "ComputerName"
  22. #define PROFILE_INT_BUFFER_LENGTH 128
  23. #define MIME_TO_FILE_EXTENSION_KEY "MIME\\Database\\Content Type\\"
  24. #define EXTENSION_VALUE "Extension"
  25. //
  26. // macros
  27. //
  28. #define INTERNET_SETTINGS_KEY "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"
  29. #define INTERNET_CACHE_SETTINGS_KEY "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\5.0\\Cache"
  30. //from macros.h
  31. #define PRIVATE
  32. #define PUBLIC
  33. #define ARRAY_ELEMENTS(array) \
  34. (sizeof(array)/sizeof(array[0]))
  35. //from defaults.h
  36. #define DEFAULT_EMAIL_NAME "user@domain"
  37. //from wininet.w
  38. #define ERROR_INTERNET_BAD_REGISTRY_PARAMETER 12022
  39. PRIVATE
  40. BOOL
  41. IsPlatformWinNT()
  42. {
  43. OSVERSIONINFO osVersionInfo;
  44. BOOL fRet = FALSE;
  45. osVersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  46. if (GetVersionEx(&osVersionInfo))
  47. fRet = (VER_PLATFORM_WIN32_NT == osVersionInfo.dwPlatformId);
  48. return fRet;
  49. }
  50. //
  51. // private prototypes
  52. //
  53. PRIVATE
  54. DWORD
  55. InternetReadRegistryDwordKey(
  56. IN HKEY ParameterKey,
  57. IN LPCSTR ParameterName,
  58. OUT LPDWORD ParameterValue
  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. );
  68. PRIVATE
  69. DWORD
  70. ReadRegistryOemString(
  71. IN HKEY Key,
  72. IN LPCSTR ParameterName,
  73. OUT LPSTR String,
  74. IN OUT LPDWORD Length
  75. );
  76. PRIVATE
  77. DWORD
  78. WriteRegistryDword(
  79. IN HKEY Key,
  80. IN LPCSTR ParameterName,
  81. IN DWORD ParameterValue
  82. );
  83. //
  84. // private data
  85. //
  86. PRIVATE HKEY hKeyInternetSettings = NULL;
  87. //
  88. // functions
  89. //
  90. DWORD
  91. OpenInternetSettingsKey(
  92. VOID
  93. )
  94. /*++
  95. Routine Description:
  96. Opens registry key for Internet Settings branch
  97. Arguments:
  98. None.
  99. Return Value:
  100. DWORD
  101. Success - ERROR_SUCCESS
  102. Failure -
  103. --*/
  104. {
  105. if (hKeyInternetSettings == NULL) {
  106. DWORD dwDisposition;
  107. REGCREATEKEYEX(HKEY_CURRENT_USER,
  108. INTERNET_SETTINGS_KEY,
  109. 0, // reserved
  110. NULL, // class
  111. 0, // options
  112. KEY_READ | KEY_WRITE,
  113. NULL, // security attributes
  114. &hKeyInternetSettings,
  115. &dwDisposition
  116. );
  117. }
  118. return ERROR_SUCCESS;
  119. }
  120. DWORD
  121. CloseInternetSettingsKey(
  122. VOID
  123. )
  124. /*++
  125. Routine Description:
  126. Closes Internet Settings registry key
  127. Arguments:
  128. None.
  129. Return Value:
  130. DWORD
  131. Success - ERROR_SUCCESS
  132. Failure -
  133. --*/
  134. {
  135. DWORD error = ERROR_SUCCESS;
  136. if (hKeyInternetSettings != NULL) {
  137. error = REGCLOSEKEY(hKeyInternetSettings);
  138. hKeyInternetSettings = NULL;
  139. }
  140. return error;
  141. }
  142. DWORD
  143. GetMyEmailName(
  144. OUT LPSTR EmailName,
  145. IN OUT LPDWORD Length
  146. )
  147. /*++
  148. Routine Description:
  149. Retrieve the user's email name from the appropriate place in the registry
  150. Arguments:
  151. EmailName - place to store email name
  152. Length - IN: length of EmailName
  153. OUT: returned length of EmailName (in characters, minus
  154. trailing NUL)
  155. Return Value:
  156. DWORD
  157. Success - ERROR_SUCCESS
  158. Failure - ERROR_FILE_NOT_FOUND
  159. ERROR_PATH_NOT_FOUND
  160. --*/
  161. {
  162. DEBUG_ENTER((DBG_REGISTRY,
  163. Dword,
  164. "GetMyEmailName",
  165. "%#x, %#x [%d]",
  166. EmailName,
  167. Length,
  168. *Length
  169. ));
  170. DWORD error;
  171. //
  172. // for the EmailName, we first try HKEY_CURRENT_USER. If that fails then we
  173. // try the same branch of the HKEY_LOCAL_MACHINE tree. If that fails,
  174. // invent something
  175. //
  176. static HKEY KeysToTry[2] = {HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE};
  177. int i;
  178. //
  179. // in the event we cannot find EmailName in both HKEY_CURRENT_USER and
  180. // HKEY_LOCAL_MACHINE trees, then we return this default
  181. //
  182. static char DefaultEmailName[] = DEFAULT_EMAIL_NAME;
  183. for (i = 0; i < ARRAY_ELEMENTS(KeysToTry); ++i) {
  184. error = InternetReadRegistryStringKey(KeysToTry[i],
  185. "EmailName",
  186. EmailName,
  187. Length
  188. );
  189. if (error == ERROR_SUCCESS) {
  190. break;
  191. }
  192. }
  193. if (error != ERROR_SUCCESS) {
  194. if (IsPlatformWinNT()) {
  195. //
  196. // only NT supports GetUserName()
  197. //
  198. if (GetUserName(EmailName, Length)) {
  199. //
  200. // we return the length as if the result from strlen/wcslen
  201. //
  202. *Length -= sizeof(char);
  203. DEBUG_PRINT(REGISTRY,
  204. INFO,
  205. ("GetUserName() returns %q\n",
  206. EmailName
  207. ));
  208. error = ERROR_SUCCESS;
  209. } else {
  210. //
  211. // BUGBUG - what's the required length?
  212. //
  213. error = GetLastError();
  214. }
  215. } else {
  216. //
  217. // Win95 & Win32s: have to do something different
  218. //
  219. }
  220. //
  221. // if we still don't have an email name, we use an internal default
  222. //
  223. if (error != ERROR_SUCCESS) {
  224. DEBUG_PRINT(REGISTRY,
  225. ERROR,
  226. ("Cannot find EmailName: using default (%s)\n",
  227. DefaultEmailName
  228. ));
  229. if (*Length >= sizeof(DEFAULT_EMAIL_NAME)) {
  230. memcpy(EmailName, DefaultEmailName, sizeof(DEFAULT_EMAIL_NAME));
  231. //
  232. // success - returned length as if from strlen()
  233. //
  234. *Length = sizeof(DEFAULT_EMAIL_NAME) - 1;
  235. error = ERROR_SUCCESS;
  236. } else {
  237. //
  238. // failure - returned length is the required buffer size
  239. //
  240. *Length = sizeof(DEFAULT_EMAIL_NAME);
  241. error = ERROR_INSUFFICIENT_BUFFER;
  242. }
  243. }
  244. }
  245. DEBUG_LEAVE(error);
  246. return error;
  247. }
  248. PUBLIC
  249. DWORD
  250. InternetDeleteRegistryValue(
  251. IN LPSTR ParameterName
  252. )
  253. /*++
  254. Routine Description:
  255. Delets an entry from a the Internet Client registry key if the platform
  256. is NT/Win95.
  257. Arguments:
  258. ParameterName - name of the parameter to retrieve (e.g. AccessType)
  259. Return Value:
  260. DWORD
  261. Success - ERROR_SUCCESS
  262. Failure - ERROR_PATH_NOT_FOUND
  263. --*/
  264. {
  265. DWORD error;
  266. DEBUG_ENTER((DBG_REGISTRY,
  267. Dword,
  268. "InternetDeleteRegistryValue",
  269. "%q",
  270. ParameterName
  271. ));
  272. HKEY clientKey;
  273. //
  274. // open the registry key containing the Internet client values (this is
  275. // in the same place on NT and Win95)
  276. //
  277. error = REGOPENKEYEX(HKEY_CURRENT_USER,
  278. INTERNET_SETTINGS_KEY,
  279. 0, // reserved
  280. KEY_ALL_ACCESS,
  281. &clientKey
  282. );
  283. if (error == ERROR_SUCCESS) {
  284. error = RegDeleteValue(clientKey,
  285. ParameterName
  286. );
  287. REGCLOSEKEY(clientKey);
  288. }
  289. DEBUG_LEAVE(error);
  290. return error;
  291. }
  292. DWORD
  293. InternetReadRegistryDword(
  294. IN LPCSTR ParameterName,
  295. OUT LPDWORD ParameterValue
  296. )
  297. /*++
  298. Routine Description:
  299. Reads a single DWORD from a the Internet Client registry key if the platform
  300. is NT/Win95, else reads the value from SYSTEM.INI if we are running on Win32s
  301. Arguments:
  302. ParameterName - name of the parameter to retrieve (e.g. AccessType)
  303. ParameterValue - pointer to place to store retrieved value
  304. Return Value:
  305. DWORD
  306. Success - ERROR_SUCCESS
  307. Failure - ERROR_PATH_NOT_FOUND
  308. --*/
  309. {
  310. DEBUG_ENTER((DBG_REGISTRY,
  311. Dword,
  312. "InternetReadRegistryDword",
  313. "%q, %x",
  314. ParameterName,
  315. ParameterValue
  316. ));
  317. DWORD error = InternetReadRegistryDwordKey(HKEY_CURRENT_USER,
  318. ParameterName,
  319. ParameterValue
  320. );
  321. DEBUG_LEAVE(error);
  322. return error;
  323. }
  324. DWORD
  325. InternetCacheReadRegistryDword(
  326. IN LPCSTR ParameterName,
  327. OUT LPDWORD ParameterValue
  328. )
  329. /*++
  330. Routine Description:
  331. Reads a single DWORD from a the Internet Client registry key if the platform
  332. is NT/Win95, else reads the value from SYSTEM.INI if we are running on Win32s
  333. Arguments:
  334. ParameterName - name of the parameter to retrieve (e.g. AccessType)
  335. ParameterValue - pointer to place to store retrieved value
  336. Return Value:
  337. DWORD
  338. Success - ERROR_SUCCESS
  339. Failure - ERROR_PATH_NOT_FOUND
  340. --*/
  341. {
  342. DEBUG_ENTER((DBG_REGISTRY,
  343. Dword,
  344. "InternetCacheReadRegistryDword",
  345. "%q, %x",
  346. ParameterName,
  347. ParameterValue
  348. ));
  349. DWORD error = ERROR_SUCCESS;
  350. HKEY clientKey;
  351. error = REGOPENKEYEX(HKEY_CURRENT_USER,
  352. INTERNET_CACHE_SETTINGS_KEY,
  353. 0, // reserved
  354. KEY_QUERY_VALUE,
  355. &clientKey
  356. );
  357. if (error == ERROR_SUCCESS) {
  358. error = ReadRegistryDword(clientKey,
  359. ParameterName,
  360. ParameterValue
  361. );
  362. REGCLOSEKEY(clientKey);
  363. }
  364. DEBUG_LEAVE(error);
  365. return error;
  366. }
  367. DWORD
  368. InternetWriteRegistryDword(
  369. IN LPCSTR ParameterName,
  370. IN DWORD ParameterValue
  371. )
  372. /*++
  373. Routine Description:
  374. Writes a single DWORD from to the Internet Client registry key if the platform
  375. is NT/Win95, otherwise it fails.
  376. Arguments:
  377. ParameterName - name of the parameter to retrieve (e.g. AccessType)
  378. ParameterValue - value to store in registry.
  379. Return Value:
  380. DWORD
  381. Success - ERROR_SUCCESS
  382. Failure - ERROR_PATH_NOT_FOUND
  383. --*/
  384. {
  385. DEBUG_ENTER((DBG_REGISTRY,
  386. Dword,
  387. "InternetWriteRegistryDword",
  388. "%q, %x",
  389. ParameterName,
  390. ParameterValue
  391. ));
  392. DWORD error;
  393. if (hKeyInternetSettings != NULL) {
  394. error = WriteRegistryDword(hKeyInternetSettings,
  395. ParameterName,
  396. ParameterValue
  397. );
  398. } else {
  399. error = ERROR_SUCCESS;
  400. }
  401. DEBUG_PRINT(REGISTRY,
  402. INFO,
  403. ("InternetWriteRegistryDword(%q): value = %d (%#x)\n",
  404. ParameterName,
  405. ParameterValue,
  406. ParameterValue
  407. ));
  408. DEBUG_LEAVE(error);
  409. return error;
  410. }
  411. DWORD
  412. InternetReadRegistryString(
  413. IN LPCSTR ParameterName,
  414. OUT LPSTR ParameterValue,
  415. IN OUT LPDWORD ParameterLength
  416. )
  417. /*++
  418. Routine Description:
  419. Reads a string from the Internet Client registry key on NT/Win95, or reads
  420. the corresponding value from SYSTEM.INI on a Win32s platform
  421. Arguments:
  422. ParameterName - name of value parameter within key (e.g. EmailName)
  423. ParameterValue - pointer to string buffer for returned string
  424. ParameterLength - IN: number of bytes in ParameterValue
  425. OUT: number of bytes in string (excluding trailing '\0')
  426. Return Value:
  427. DWORD
  428. Success - ERROR_SUCCESS
  429. Failure - ERROR_PATH_NOT_FOUND
  430. --*/
  431. {
  432. DEBUG_ENTER((DBG_REGISTRY,
  433. Dword,
  434. "InternetReadRegistryString",
  435. "%q, %x, %x [%d]",
  436. ParameterName,
  437. ParameterValue,
  438. ParameterLength,
  439. *ParameterLength
  440. ));
  441. DWORD error = InternetReadRegistryStringKey(HKEY_CURRENT_USER,
  442. ParameterName,
  443. ParameterValue,
  444. ParameterLength
  445. );
  446. DEBUG_LEAVE(error);
  447. return error;
  448. }
  449. PUBLIC
  450. DWORD
  451. InternetReadRegistryDwordKey(
  452. IN HKEY ParameterKey,
  453. IN LPCSTR ParameterName,
  454. OUT LPDWORD ParameterValue
  455. )
  456. /*++
  457. Routine Description:
  458. Reads a single DWORD from a the Internet Client registry key if the platform
  459. is NT/Win95, else reads the value from SYSTEM.INI if we are running on Win32s.
  460. Does not modify the *ParameterValue if the registry variable cannot be read
  461. Arguments:
  462. ParameterKey - root registry key (e.g. HKEY_CURRENT_USER)
  463. ParameterName - name of the parameter to retrieve (e.g. AccessType)
  464. ParameterValue - pointer to place to store retrieved value
  465. Return Value:
  466. DWORD
  467. Success - ERROR_SUCCESS
  468. Failure - ERROR_PATH_NOT_FOUND
  469. --*/
  470. {
  471. DEBUG_ENTER((DBG_REGISTRY,
  472. Dword,
  473. "InternetReadRegistryDwordKey",
  474. "%s, %q, %x",
  475. (ParameterKey == HKEY_LOCAL_MACHINE)
  476. ? "HKEY_LOCAL_MACHINE"
  477. : (ParameterKey == HKEY_CURRENT_USER)
  478. ? "HKEY_CURRENT_USER"
  479. : "???",
  480. ParameterName,
  481. ParameterValue
  482. ));
  483. DWORD error = ERROR_SUCCESS;
  484. HKEY clientKey = hKeyInternetSettings;
  485. if (ParameterKey != HKEY_CURRENT_USER) {
  486. error = REGOPENKEYEX(ParameterKey,
  487. INTERNET_SETTINGS_KEY,
  488. 0, // reserved
  489. KEY_QUERY_VALUE,
  490. &clientKey
  491. );
  492. } else if (clientKey == NULL) {
  493. error = ERROR_PATH_NOT_FOUND;
  494. }
  495. if (error == ERROR_SUCCESS) {
  496. error = ReadRegistryDword(clientKey,
  497. ParameterName,
  498. ParameterValue
  499. );
  500. if (clientKey != hKeyInternetSettings) {
  501. REGCLOSEKEY(clientKey);
  502. }
  503. }
  504. DEBUG_PRINT(REGISTRY,
  505. INFO,
  506. ("InternetReadRegistryDwordKey(%q): value = %d (%#x)\n",
  507. ParameterName,
  508. *ParameterValue,
  509. *ParameterValue
  510. ));
  511. DEBUG_LEAVE(error);
  512. return error;
  513. }
  514. PRIVATE
  515. DWORD
  516. InternetReadRegistryStringKey(
  517. IN HKEY ParameterKey,
  518. IN LPCSTR ParameterName,
  519. OUT LPSTR ParameterValue,
  520. IN OUT LPDWORD ParameterLength
  521. )
  522. /*++
  523. Routine Description:
  524. Reads a string from the Internet Client registry key on NT/Win95, or reads
  525. the corresponding value from SYSTEM.INI on a Win32s platform
  526. Arguments:
  527. ParameterKey - root registry key (e.g. HKEY_LOCAL_MACHINE)
  528. ParameterName - name of value parameter within key (e.g. EmailName)
  529. ParameterValue - pointer to string buffer for returned string
  530. ParameterLength - IN: number of bytes in ParameterValue
  531. OUT: number of bytes in string (excluding trailing '\0')
  532. Return Value:
  533. DWORD
  534. Success - ERROR_SUCCESS
  535. Failure - ERROR_PATH_NOT_FOUND
  536. --*/
  537. {
  538. DEBUG_ENTER((DBG_REGISTRY,
  539. Dword,
  540. "InternetReadRegistryStringKey",
  541. "%s (%x), %q, %x, %x [%d]",
  542. (ParameterKey == HKEY_LOCAL_MACHINE)
  543. ? "HKEY_LOCAL_MACHINE"
  544. : (ParameterKey == HKEY_CURRENT_USER)
  545. ? "HKEY_CURRENT_USER"
  546. : "???",
  547. ParameterKey,
  548. ParameterName,
  549. ParameterValue,
  550. ParameterLength,
  551. *ParameterLength
  552. ));
  553. //
  554. // zero-terminate the string
  555. //
  556. if (*ParameterLength > 0) {
  557. *ParameterValue = '\0';
  558. }
  559. DWORD error = ERROR_SUCCESS;
  560. HKEY clientKey = hKeyInternetSettings;
  561. if (ParameterKey != HKEY_CURRENT_USER) {
  562. error = REGOPENKEYEX(ParameterKey,
  563. INTERNET_SETTINGS_KEY,
  564. 0, // reserved
  565. KEY_QUERY_VALUE,
  566. &clientKey
  567. );
  568. } else if (clientKey == NULL) {
  569. error = ERROR_PATH_NOT_FOUND;
  570. }
  571. if (error == ERROR_SUCCESS) {
  572. error = ReadRegistryOemString(clientKey,
  573. ParameterName,
  574. ParameterValue,
  575. ParameterLength
  576. );
  577. if (clientKey != hKeyInternetSettings) {
  578. REGCLOSEKEY(clientKey);
  579. }
  580. }
  581. DEBUG_PRINT(REGISTRY,
  582. INFO,
  583. ("InternetReadRegistryStringKey(%q): value = %q\n",
  584. ParameterName,
  585. ParameterValue
  586. ));
  587. DEBUG_LEAVE(error);
  588. return error;
  589. }
  590. PRIVATE
  591. DWORD
  592. ReadRegistryOemString(
  593. IN HKEY Key,
  594. IN LPCSTR ParameterName,
  595. OUT LPSTR String,
  596. IN OUT LPDWORD Length
  597. )
  598. /*++
  599. Routine Description:
  600. Reads a string out of the registry as an OEM string
  601. Arguments:
  602. Key - open registry key where to read value from
  603. ParameterName - name of registry value to read
  604. String - place to put it
  605. Length - IN: length of String buffer in characters
  606. OUT: length of String in characters, as if returned from
  607. strlen()
  608. Return Value:
  609. DWORD
  610. Success - ERROR_SUCCESS
  611. Failure - ERROR_FILE_NOT_FOUND
  612. Couldn't find the parameter
  613. ERROR_PATH_NOT_FOUND
  614. Couldn't find the parameter
  615. ERROR_INTERNET_BAD_REGISTRY_PARAMETER
  616. Inconsistent registry contents
  617. --*/
  618. {
  619. DEBUG_ENTER((DBG_REGISTRY,
  620. Dword,
  621. "ReadRegistryOemString",
  622. "%#x, %q, %#x, %#x [%d]",
  623. Key,
  624. ParameterName,
  625. String,
  626. Length,
  627. *Length
  628. ));
  629. LONG error;
  630. DWORD valueType;
  631. LPSTR str;
  632. DWORD valueLength;
  633. //
  634. // first, get the length of the string
  635. //
  636. valueLength = *Length;
  637. error = RegQueryValueEx(Key,
  638. ParameterName,
  639. NULL, // reserved
  640. &valueType,
  641. (LPBYTE)String,
  642. &valueLength
  643. );
  644. if (error != ERROR_SUCCESS) {
  645. goto quit;
  646. }
  647. //
  648. // we only support REG_SZ (single string) values in this function
  649. //
  650. if (valueType != REG_SZ) {
  651. error = ERROR_INTERNET_BAD_REGISTRY_PARAMETER;
  652. goto quit;
  653. }
  654. //
  655. // if 1 or 0 chars returned then the string is empty
  656. //
  657. if (valueLength <= sizeof(char)) {
  658. error = ERROR_PATH_NOT_FOUND;
  659. goto quit;
  660. }
  661. //
  662. // convert the ANSI string to OEM character set in place. According to Win
  663. // help, this always succeeds
  664. //
  665. CharToOem(String, String);
  666. //
  667. // return the length as if returned from strlen() (i.e. drop the '\0')
  668. //
  669. *Length = valueLength - sizeof(char);
  670. DEBUG_PRINT(REGISTRY,
  671. INFO,
  672. ("ReadRegistryOemString(%q) returning %q (%d chars)\n",
  673. ParameterName,
  674. String,
  675. *Length
  676. ));
  677. quit:
  678. DEBUG_LEAVE(error);
  679. return error;
  680. }
  681. DWORD
  682. ReadRegistryDword(
  683. IN HKEY Key,
  684. IN LPCSTR ParameterName,
  685. OUT LPDWORD ParameterValue
  686. )
  687. /*++
  688. Routine Description:
  689. Reads a DWORD parameter from the registry
  690. Won't modify *ParameterValue unless a valid value is read from the registry
  691. Arguments:
  692. Key - handle of open registry key where parameter resides
  693. ParameterName - name of DWORD parameter to read
  694. ParameterValue - returned DWORD parameter read from registry
  695. Return Value:
  696. DWORD
  697. Success - ERROR_SUCCESS
  698. Failure - ERROR_PATH_NOT_FOUND
  699. One of the following occurred:
  700. - the parameter is not in the specified registry key
  701. - the parameter is the wrong type
  702. - the parameter is the wrong size
  703. --*/
  704. {
  705. DEBUG_ENTER((DBG_REGISTRY,
  706. Dword,
  707. "ReadRegistryDword",
  708. "%x, %q, %x",
  709. Key,
  710. ParameterName,
  711. ParameterValue
  712. ));
  713. DWORD error;
  714. DWORD valueLength;
  715. DWORD valueType;
  716. DWORD value;
  717. valueLength = sizeof(*ParameterValue);
  718. error = (DWORD)RegQueryValueEx(Key,
  719. ParameterName,
  720. NULL, // reserved
  721. &valueType,
  722. (LPBYTE)&value,
  723. &valueLength
  724. );
  725. //
  726. // if the size or type aren't correct then return an error, else only if
  727. // success was returned do we modify *ParameterValue
  728. //
  729. if (error == ERROR_SUCCESS) {
  730. if (((valueType != REG_DWORD)
  731. && (valueType != REG_BINARY))
  732. || (valueLength != sizeof(DWORD))) {
  733. DEBUG_PRINT(REGISTRY,
  734. ERROR,
  735. ("valueType = %d, valueLength = %d\n",
  736. valueType,
  737. valueLength
  738. ));
  739. error = ERROR_PATH_NOT_FOUND;
  740. } else {
  741. *ParameterValue = value;
  742. }
  743. }
  744. DEBUG_LEAVE(error);
  745. return error;
  746. }
  747. PRIVATE
  748. DWORD
  749. WriteRegistryDword(
  750. IN HKEY Key,
  751. IN LPCSTR ParameterName,
  752. IN DWORD ParameterValue
  753. )
  754. /*++
  755. Routine Description:
  756. Writes a DWORD parameter from the registry
  757. Will write ParameterValue to the key.
  758. Arguments:
  759. Key - handle of open registry key where parameter resides
  760. ParameterName - name of DWORD parameter to write
  761. ParameterValue - DWORD parameter to write from registry
  762. Return Value:
  763. DWORD
  764. Success - ERROR_SUCCESS
  765. Failure - ERROR_PATH_NOT_FOUND
  766. One of the following occurred:
  767. - the parameter is not in the specified registry key
  768. - the parameter is the wrong type
  769. - the parameter is the wrong size
  770. --*/
  771. {
  772. DEBUG_ENTER((DBG_REGISTRY,
  773. Dword,
  774. "WriteRegistryDword",
  775. "%x, %q, %x",
  776. Key,
  777. ParameterName,
  778. ParameterValue
  779. ));
  780. DWORD error;
  781. DWORD valueLength;
  782. DWORD valueType;
  783. DWORD value;
  784. valueLength = sizeof(ParameterValue);
  785. valueType = REG_DWORD;
  786. value = ParameterValue;
  787. error = (DWORD)RegSetValueEx(Key,
  788. ParameterName,
  789. NULL, // reserved
  790. valueType,
  791. (LPBYTE)&value,
  792. valueLength
  793. );
  794. DEBUG_PRINT(REGISTRY,
  795. INFO,
  796. ("added: valueType = %d, valueLength = %d\n",
  797. valueType,
  798. valueLength
  799. ));
  800. DEBUG_LEAVE(error);
  801. return error;
  802. }
  803. #if INET_DEBUG
  804. typedef struct {
  805. LIST_ENTRY entry;
  806. HKEY hkey;
  807. char * file;
  808. int line;
  809. char name[1];
  810. } DBGREGKEYINFO;
  811. #if 0
  812. SERIALIZED_LIST DbgRegKeyList;
  813. #endif
  814. VOID DbgRegKey_Init(VOID) {
  815. #if 0
  816. InitializeSerializedList(&DbgRegKeyList);
  817. #endif
  818. }
  819. VOID DbgRegKey_Terminate(VOID) {
  820. #if 0
  821. TerminateSerializedList(&DbgRegKeyList);
  822. #endif
  823. }
  824. void regkey_add(const char * name, HKEY hkey, char * file, int line) {
  825. #if 0
  826. if (!name) {
  827. name = "";
  828. }
  829. int len = lstrlen(name);
  830. DBGREGKEYINFO * p = (DBGREGKEYINFO *)ALLOCATE_FIXED_MEMORY(sizeof(DBGREGKEYINFO) + len);
  831. if (p) {
  832. memcpy(p->name, name, len + 1);
  833. p->line = line;
  834. p->file = file;
  835. p->hkey = hkey;
  836. InsertAtHeadOfSerializedList(&DbgRegKeyList, &p->entry);
  837. }
  838. #endif
  839. }
  840. void regkey_remove(HKEY hkey) {
  841. #if 0
  842. LockSerializedList(&DbgRegKeyList);
  843. DBGREGKEYINFO * p = (DBGREGKEYINFO *)HeadOfSerializedList(&DbgRegKeyList);
  844. while (p != (DBGREGKEYINFO *)SlSelf(&DbgRegKeyList)) {
  845. if (p->hkey == hkey) {
  846. RemoveFromSerializedList(&DbgRegKeyList, (PLIST_ENTRY)p);
  847. FREE_MEMORY(p);
  848. break;
  849. }
  850. p = (DBGREGKEYINFO *)p->entry.Flink;
  851. }
  852. UnlockSerializedList(&DbgRegKeyList);
  853. #endif
  854. }
  855. #undef NEW_STRING
  856. #define NEW_STRING(str) (str)
  857. char * regkey_name(HKEY hkey, const char * subname) {
  858. switch ((INT_PTR)hkey) {
  859. case (INT_PTR)HKEY_CLASSES_ROOT:
  860. return NEW_STRING("HKEY_CLASSES_ROOT");
  861. case (INT_PTR)HKEY_CURRENT_USER:
  862. return NEW_STRING("HKEY_CURRENT_USER");
  863. case (INT_PTR)HKEY_LOCAL_MACHINE:
  864. return NEW_STRING("HKEY_LOCAL_MACHINE");
  865. case (INT_PTR)HKEY_USERS:
  866. return NEW_STRING("HKEY_USERS");
  867. case (INT_PTR)HKEY_PERFORMANCE_DATA:
  868. return NEW_STRING("HKEY_PERFORMANCE_DATA");
  869. case (INT_PTR)HKEY_CURRENT_CONFIG:
  870. return NEW_STRING("HKEY_CURRENT_CONFIG");
  871. case (INT_PTR)HKEY_DYN_DATA:
  872. return NEW_STRING("HKEY_DYN_DATA");
  873. }
  874. char * name = NULL;
  875. #if 0
  876. LockSerializedList(&DbgRegKeyList);
  877. DBGREGKEYINFO * p = (DBGREGKEYINFO *)HeadOfSerializedList(&DbgRegKeyList);
  878. while (p != (DBGREGKEYINFO *)SlSelf(&DbgRegKeyList)) {
  879. if (p->hkey == hkey) {
  880. int len = lstrlen(p->name);
  881. int slen = lstrlen(subname);
  882. name = (char *)ALLOCATE_FIXED_MEMORY(len + 1 + slen + 1);
  883. if (name) {
  884. memcpy(name, p->name, len);
  885. name[len] = '\\';
  886. memcpy(name + len + 1, subname, slen + 1);
  887. }
  888. break;
  889. }
  890. p = (DBGREGKEYINFO *)p->entry.Flink;
  891. }
  892. UnlockSerializedList(&DbgRegKeyList);
  893. #endif
  894. return name;
  895. }
  896. void regkey_freename(char * name) {
  897. #if 0
  898. if (name) {
  899. FREE_MEMORY(name);
  900. }
  901. #endif
  902. }
  903. LONG
  904. DbgRegOpenKey(
  905. IN HKEY hKey,
  906. IN LPCTSTR lpszSubKey,
  907. OUT PHKEY phkResult,
  908. char * file,
  909. int line
  910. )
  911. {
  912. char * keyname = regkey_name(hKey, lpszSubKey);
  913. LONG rc = RegOpenKey(hKey, lpszSubKey, phkResult);
  914. if (rc == 0) {
  915. regkey_add(keyname, *phkResult, file, line);
  916. }
  917. regkey_freename(keyname);
  918. return rc;
  919. }
  920. LONG
  921. DbgRegOpenKeyEx(
  922. IN HKEY hKey,
  923. IN LPCSTR lpSubKey,
  924. IN DWORD ulOptions,
  925. IN REGSAM samDesired,
  926. OUT PHKEY phkResult,
  927. char * file,
  928. int line
  929. )
  930. {
  931. char * keyname = regkey_name(hKey, lpSubKey);
  932. LONG rc = RegOpenKeyEx(hKey, lpSubKey, ulOptions, samDesired, phkResult);
  933. if (rc == 0) {
  934. regkey_add(keyname, *phkResult, file, line);
  935. }
  936. regkey_freename(keyname);
  937. return rc;
  938. }
  939. LONG
  940. DbgRegCreateKeyEx(
  941. IN HKEY hKey,
  942. IN LPCSTR lpSubKey,
  943. IN DWORD Reserved,
  944. IN LPSTR lpClass,
  945. IN DWORD dwOptions,
  946. IN REGSAM samDesired,
  947. IN LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  948. OUT PHKEY phkResult,
  949. OUT LPDWORD lpdwDisposition,
  950. char * file,
  951. int line
  952. )
  953. {
  954. char * keyname = regkey_name(hKey, lpSubKey);
  955. LONG rc = RegCreateKeyEx(hKey, lpSubKey, Reserved, lpClass, dwOptions, samDesired, lpSecurityAttributes, phkResult, lpdwDisposition);
  956. if (rc == 0) {
  957. regkey_add(keyname, *phkResult, file, line);
  958. }
  959. regkey_freename(keyname);
  960. return rc;
  961. }
  962. LONG
  963. DbgRegCloseKey(
  964. IN HKEY hKey
  965. )
  966. {
  967. LONG rc = RegCloseKey(hKey);
  968. if (rc == 0) {
  969. regkey_remove(hKey);
  970. }
  971. return rc;
  972. }
  973. #endif // INET_DEBUG
  974. #endif // ENABLE_DEBUG