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.

2072 lines
43 KiB

  1. /*++
  2. Copyright (c) 1997 Microsoft Corporation
  3. Module Name:
  4. reg.c
  5. Abstract:
  6. Implements utilities to parse a registry key string, and also implements
  7. wrappers to the registry API. There are three groups of APIs in this
  8. source file: query functions, open and create functions, and registry
  9. string parsing functions.
  10. The query functions allow simplified querying, where the caller receives
  11. a MemAlloc'd pointer to the data and does not have to worry about managing
  12. the numerous parameters needed to do the query. The query functions
  13. also allow filtering of values that are not the expected type. All
  14. query functions have a version with 2 appended to the function name which
  15. allow the caller to specify an alternative allocator and deallocator.
  16. The open and create functions simplify the process of obtaining a key
  17. handle. They allow the caller to specify a key string as input and return
  18. the key handle as output.
  19. The registry string parsing functions are utilities that can be used when
  20. processing registry key strings. The functions extract the registry root
  21. from a string, convert it into a handle, convert a hive handle into a
  22. string, and so on.
  23. Author:
  24. Jim Schmidt (jimschm) 20-Mar-1997
  25. Revisions:
  26. jimschm 18-Sep-2000 Added cache
  27. ovidiut 22-Feb-1999 Added GetRegSubkeysCount
  28. calinn 23-Sep-1998 Fixed REG_SZ filtering
  29. jimschm 25-Mar-1998 Added CreateEncodedRegistryStringEx
  30. jimschm 21-Oct-1997 Added EnumFirstKeyInTree/EnumNextKeyInTree
  31. marcw 16-Jul-1997 Added CreateEncodedRegistryString/FreeEncodedRegistryString
  32. jimschm 22-Jun-1997 Added GetRegData
  33. --*/
  34. #include "pch.h"
  35. #include "regp.h"
  36. #ifdef DEBUG
  37. #undef RegCloseKey
  38. #endif
  39. HKEY g_Root = HKEY_ROOT;
  40. REGSAM g_OpenSam = KEY_ALL_ACCESS;
  41. REGSAM g_CreateSam = KEY_ALL_ACCESS;
  42. INT g_RegRefs;
  43. #define DBG_REG "Reg"
  44. //
  45. // Implementation
  46. //
  47. BOOL
  48. RegInitialize (
  49. VOID
  50. )
  51. {
  52. BOOL b = TRUE;
  53. MYASSERT (g_RegRefs >= 0);
  54. g_RegRefs++;
  55. if (g_RegRefs == 1) {
  56. RegInitializeCache (0);
  57. b = RegEnumInitialize ();
  58. }
  59. return b;
  60. }
  61. VOID
  62. RegTerminate (
  63. VOID
  64. )
  65. {
  66. MYASSERT (g_RegRefs > 0);
  67. g_RegRefs--;
  68. if (!g_RegRefs) {
  69. RegEnumTerminate ();
  70. RegTerminateCache ();
  71. }
  72. #ifdef DEBUG
  73. RegTrackTerminate();
  74. #endif
  75. }
  76. VOID
  77. SetRegRoot (
  78. IN HKEY Root
  79. )
  80. {
  81. g_Root = Root;
  82. }
  83. HKEY
  84. GetRegRoot (
  85. VOID
  86. )
  87. {
  88. return g_Root;
  89. }
  90. REGSAM
  91. SetRegOpenAccessMode (
  92. REGSAM Mode
  93. )
  94. {
  95. REGSAM OldMode;
  96. OldMode = g_OpenSam;
  97. g_OpenSam = Mode;
  98. return OldMode;
  99. }
  100. REGSAM
  101. GetRegOpenAccessMode (
  102. REGSAM Mode
  103. )
  104. {
  105. return g_OpenSam;
  106. }
  107. REGSAM
  108. SetRegCreateAccessMode (
  109. REGSAM Mode
  110. )
  111. {
  112. REGSAM OldMode;
  113. OldMode = g_CreateSam;
  114. g_CreateSam = Mode;
  115. return OldMode;
  116. }
  117. REGSAM
  118. GetRegCreateAccessMode (
  119. REGSAM Mode
  120. )
  121. {
  122. return g_CreateSam;
  123. }
  124. /*++
  125. Routine Description:
  126. OpenRegKeyStrA and OpenRegKeyStrW parse a text string that specifies a
  127. registry key into the hive and subkey, and then they open the subkey
  128. and return the handle.
  129. Arguments:
  130. RegKey - Specifies the complete path to the registry subkey, including
  131. the hive.
  132. Return Value:
  133. A non-NULL registry handle if successful, or NULL if either the subkey
  134. could not be opened or the string is malformed.
  135. --*/
  136. HKEY
  137. RealOpenRegKeyStrA (
  138. IN PCSTR RegKey
  139. DEBUG_TRACKING_PARAMS
  140. )
  141. {
  142. DWORD End;
  143. HKEY RootKey;
  144. HKEY Key;
  145. HKEY parentKey;
  146. PCSTR lastWack;
  147. //
  148. // Attempt to use cache
  149. //
  150. Key = RegGetKeyFromCacheA (RegKey, NULL, g_OpenSam, TRUE);
  151. if (Key) {
  152. TRACK_KEYA (Key, RegKey);
  153. return Key;
  154. }
  155. //
  156. // Attempt to use cache for parent
  157. //
  158. lastWack = _mbsrchr (RegKey, '\\');
  159. if (lastWack) {
  160. parentKey = RegGetKeyFromCacheA (RegKey, lastWack, g_OpenSam, FALSE);
  161. if (parentKey) {
  162. Key = OpenRegKeyWorkerA (parentKey, lastWack + 1 /* , */ DEBUG_TRACKING_ARGS);
  163. RegAddKeyToCacheA (RegKey, Key, g_OpenSam);
  164. return Key;
  165. }
  166. }
  167. //
  168. // Not in cache; use full api
  169. //
  170. DEBUGMSGA ((DBG_REG, "Opening %s", RegKey));
  171. RootKey = ConvertRootStringToKeyA (RegKey, &End);
  172. if (!RootKey) {
  173. return NULL;
  174. }
  175. if (!RegKey[End]) {
  176. OurRegOpenRootKeyA (RootKey, RegKey /* , */ DEBUG_TRACKING_ARGS);
  177. return RootKey;
  178. }
  179. Key = OpenRegKeyWorkerA (RootKey, &RegKey[End] /* , */ DEBUG_TRACKING_ARGS);
  180. if (Key) {
  181. RegAddKeyToCacheA (RegKey, Key, g_OpenSam);
  182. RegRecordParentInCacheA (RegKey, lastWack);
  183. }
  184. return Key;
  185. }
  186. HKEY
  187. RealOpenRegKeyStrW (
  188. IN PCWSTR RegKey
  189. DEBUG_TRACKING_PARAMS
  190. )
  191. {
  192. DWORD End;
  193. HKEY RootKey;
  194. HKEY Key;
  195. HKEY parentKey;
  196. PCWSTR lastWack;
  197. //
  198. // Attempt to use cache
  199. //
  200. Key = RegGetKeyFromCacheW (RegKey, NULL, g_OpenSam, TRUE);
  201. if (Key) {
  202. TRACK_KEYW (Key, RegKey);
  203. return Key;
  204. }
  205. //
  206. // Attempt to use cache for parent
  207. //
  208. lastWack = wcsrchr (RegKey, L'\\');
  209. if (lastWack) {
  210. parentKey = RegGetKeyFromCacheW (RegKey, lastWack, g_OpenSam, FALSE);
  211. if (parentKey) {
  212. Key = OpenRegKeyWorkerW (parentKey, lastWack + 1 /* , */ DEBUG_TRACKING_ARGS);
  213. RegAddKeyToCacheW (RegKey, Key, g_OpenSam);
  214. return Key;
  215. }
  216. }
  217. //
  218. // Not in cache; use full api
  219. //
  220. DEBUGMSGW ((DBG_REG, "Opening %s", RegKey));
  221. RootKey = ConvertRootStringToKeyW (RegKey, &End);
  222. if (!RootKey) {
  223. return NULL;
  224. }
  225. if (!RegKey[End]) {
  226. OurRegOpenRootKeyW (RootKey, RegKey /* , */ DEBUG_TRACKING_ARGS);
  227. return RootKey;
  228. }
  229. Key = OpenRegKeyWorkerW (RootKey, &RegKey[End] /* , */ DEBUG_TRACKING_ARGS);
  230. if (Key) {
  231. RegAddKeyToCacheW (RegKey, Key, g_OpenSam);
  232. RegRecordParentInCacheW (RegKey, lastWack);
  233. }
  234. return Key;
  235. }
  236. BOOL
  237. DeleteRegKeyA (
  238. IN HKEY Key,
  239. IN PCSTR SubKey
  240. )
  241. {
  242. DWORD rc;
  243. rc = RegDeleteKeyA (Key, SubKey);
  244. if (rc != ERROR_SUCCESS) {
  245. SetLastError (rc);
  246. return FALSE;
  247. }
  248. return TRUE;
  249. }
  250. BOOL
  251. DeleteRegKeyW (
  252. IN HKEY Key,
  253. IN PCWSTR SubKey
  254. )
  255. {
  256. DWORD rc;
  257. rc = RegDeleteKeyW (Key, SubKey);
  258. if (rc != ERROR_SUCCESS) {
  259. SetLastError (rc);
  260. return FALSE;
  261. }
  262. return TRUE;
  263. }
  264. BOOL
  265. DeleteRegKeyStrA (
  266. IN PCSTR RegKey
  267. )
  268. {
  269. DWORD End;
  270. HKEY RootKey;
  271. BOOL result = FALSE;
  272. RootKey = ConvertRootStringToKeyA (RegKey, &End);
  273. if (!RootKey) {
  274. SetLastError (ERROR_INVALID_PARAMETER);
  275. return FALSE;
  276. }
  277. if (!RegKey[End]) {
  278. SetLastError (ERROR_INVALID_PARAMETER);
  279. return FALSE;
  280. }
  281. result = DeleteRegKeyA (RootKey, &RegKey[End]);
  282. if (result) {
  283. // if we have this in the cache we need to
  284. // remove it from there so subsequent open
  285. // will fail.
  286. RegRemoveItemFromCacheA (RegKey);
  287. }
  288. return result;
  289. }
  290. BOOL
  291. DeleteRegKeyStrW (
  292. IN PCWSTR RegKey
  293. )
  294. {
  295. DWORD End;
  296. HKEY RootKey;
  297. BOOL result = FALSE;
  298. RootKey = ConvertRootStringToKeyW (RegKey, &End);
  299. if (!RootKey) {
  300. SetLastError (ERROR_INVALID_PARAMETER);
  301. return FALSE;
  302. }
  303. if (!RegKey[End]) {
  304. SetLastError (ERROR_INVALID_PARAMETER);
  305. return FALSE;
  306. }
  307. result = DeleteRegKeyW (RootKey, &RegKey[End]);
  308. if (result) {
  309. // if we have this in the cache we need to
  310. // remove it from there so subsequent open
  311. // will fail.
  312. RegRemoveItemFromCacheW (RegKey);
  313. }
  314. return result;
  315. }
  316. BOOL
  317. DeleteEmptyRegKeyStrA (
  318. IN PCSTR RegKey
  319. )
  320. {
  321. LONG rc;
  322. DWORD subKeys;
  323. DWORD values;
  324. HKEY key;
  325. key = OpenRegKeyStrA (RegKey);
  326. if (!key) {
  327. return TRUE;
  328. }
  329. rc = RegQueryInfoKey (key, NULL, NULL, NULL, &subKeys, NULL, NULL, &values, NULL, NULL, NULL, NULL);
  330. CloseRegKey (key);
  331. if (rc != ERROR_SUCCESS) {
  332. SetLastError (rc);
  333. return FALSE;
  334. }
  335. if (subKeys || values) {
  336. SetLastError (ERROR_ACCESS_DENIED);
  337. return FALSE;
  338. }
  339. return DeleteRegKeyStrA (RegKey);
  340. }
  341. BOOL
  342. DeleteEmptyRegKeyStrW (
  343. IN PCWSTR RegKey
  344. )
  345. {
  346. LONG rc;
  347. DWORD subKeys;
  348. DWORD values;
  349. HKEY key;
  350. key = OpenRegKeyStrW (RegKey);
  351. if (!key) {
  352. return TRUE;
  353. }
  354. rc = RegQueryInfoKey (key, NULL, NULL, NULL, &subKeys, NULL, NULL, &values, NULL, NULL, NULL, NULL);
  355. CloseRegKey (key);
  356. if (rc != ERROR_SUCCESS) {
  357. SetLastError (rc);
  358. return FALSE;
  359. }
  360. if (subKeys || values) {
  361. SetLastError (ERROR_ACCESS_DENIED);
  362. return FALSE;
  363. }
  364. return DeleteRegKeyStrW (RegKey);
  365. }
  366. PVOID
  367. MemAllocWrapper (
  368. IN DWORD Size
  369. )
  370. /*++
  371. Routine Description:
  372. pemAllocWrapper implements a default allocation routine. The APIs
  373. that have a "2" at the end allow the caller to supply an alternative
  374. allocator or deallocator. The routines without the "2" use this
  375. default allocator.
  376. Arguments:
  377. Size - Specifies the amount of memory (in bytes) to allocate
  378. Return Value:
  379. A pointer to a block of memory that can hold Size bytes, or NULL
  380. if allocation fails.
  381. --*/
  382. {
  383. return MemAlloc (g_hHeap, 0, Size);
  384. }
  385. VOID
  386. MemFreeWrapper (
  387. IN PCVOID Mem
  388. )
  389. /*++
  390. Routine Description:
  391. MemFreeWrapper implements a default deallocation routine.
  392. See MemAllocWrapper above.
  393. Arguments:
  394. Mem - Specifies the block of memory to free, and was allocated by the
  395. MemAllocWrapper function.
  396. Return Value:
  397. none
  398. --*/
  399. {
  400. MemFree (g_hHeap, 0, Mem);
  401. }
  402. /*++
  403. Routine Description:
  404. GetRegValueData2A and GetRegValueData2W query a registry value and
  405. return the data as a pointer. They use the specified Alloc and Free
  406. routines to allocate and free the memory as needed.
  407. A GetRegValueData macro is defined, and it uses the default allocators,
  408. simplifying the function parameters and allowing the caller to free
  409. the return value via MemFree.
  410. Arguments:
  411. hKey - Specifies the registry key that holds the specified value.
  412. Value - Specifies the value name to query.
  413. Alloc - Specifies the allocation routine, called to allocate a block of
  414. memory for the return data.
  415. FreeRoutine - Specifies the deallocation routine, called if an error is encountered
  416. during processing.
  417. Return Value:
  418. A pointer to the data retrieved, or NULL if the value does not exist or an
  419. error occurred. Call GetLastError to obtian the failure code.
  420. --*/
  421. PBYTE
  422. GetRegValueData2A (
  423. IN HKEY hKey,
  424. IN PCSTR Value,
  425. IN ALLOCATOR AllocRoutine,
  426. IN DEALLOCATOR FreeRoutine
  427. )
  428. {
  429. LONG rc;
  430. DWORD BufSize;
  431. PBYTE DataBuf;
  432. rc = RegQueryValueExA (hKey, Value, NULL, NULL, NULL, &BufSize);
  433. if (rc != ERROR_SUCCESS) {
  434. SetLastError ((DWORD)rc);
  435. return NULL;
  436. }
  437. DataBuf = (PBYTE) AllocRoutine (BufSize + sizeof (CHAR));
  438. rc = RegQueryValueExA (hKey, Value, NULL, NULL, DataBuf, &BufSize);
  439. if (rc == ERROR_SUCCESS) {
  440. *((PSTR) DataBuf + BufSize) = 0;
  441. return DataBuf;
  442. }
  443. FreeRoutine (DataBuf);
  444. SetLastError ((DWORD)rc);
  445. return NULL;
  446. }
  447. PBYTE
  448. GetRegValueData2W (
  449. IN HKEY hKey,
  450. IN PCWSTR Value,
  451. IN ALLOCATOR AllocRoutine,
  452. IN DEALLOCATOR FreeRoutine
  453. )
  454. {
  455. LONG rc;
  456. DWORD BufSize = 0;
  457. PBYTE DataBuf;
  458. rc = RegQueryValueExW (hKey, Value, NULL, NULL, NULL, &BufSize);
  459. if (rc != ERROR_SUCCESS) {
  460. SetLastError ((DWORD)rc);
  461. return NULL;
  462. }
  463. DataBuf = (PBYTE) AllocRoutine (BufSize + sizeof(WCHAR));
  464. rc = RegQueryValueExW (hKey, Value, NULL, NULL, DataBuf, &BufSize);
  465. if (rc == ERROR_SUCCESS) {
  466. *((PWSTR) (DataBuf + BufSize)) = 0;
  467. return DataBuf;
  468. }
  469. FreeRoutine (DataBuf);
  470. SetLastError ((DWORD)rc);
  471. return NULL;
  472. }
  473. /*++
  474. Routine Description:
  475. GetRegValueDataOfType2A and GetRegValueDataOfType2W are extensions of
  476. GetRegValueData. They only return a data pointer when the data stored
  477. in the registry value is the correct type.
  478. Arguments:
  479. hKey - Specifies the registry key to query
  480. Value - Specifies the value name to query
  481. MustBeType - Specifies the type of data (a REG_* constant). If the specified
  482. value has data but is a different type, NULL will be returned.
  483. AllocRoutine - Specifies the allocation routine, called to allocate the return data.
  484. FreeRoutine - Specifies the deallocation routine, called when an error is encountered.
  485. Return Value:
  486. If successful, returns a pointer to data that matches the specified type.
  487. If the data is a different type, the value name does not exist, or an
  488. error occurs during the query, NULL is returned, and the failure code
  489. can be obtained from GetLastError.
  490. --*/
  491. PBYTE
  492. GetRegValueDataOfType2A (
  493. IN HKEY hKey,
  494. IN PCSTR Value,
  495. IN DWORD MustBeType,
  496. IN ALLOCATOR AllocRoutine,
  497. IN DEALLOCATOR FreeRoutine
  498. )
  499. {
  500. LONG rc;
  501. DWORD BufSize = 0;
  502. PBYTE DataBuf;
  503. DWORD Type;
  504. rc = RegQueryValueExA (hKey, Value, NULL, &Type, NULL, &BufSize);
  505. if (rc != ERROR_SUCCESS) {
  506. SetLastError ((DWORD)rc);
  507. return NULL;
  508. }
  509. switch (MustBeType) {
  510. case REG_SZ:
  511. case REG_EXPAND_SZ:
  512. if (Type == REG_SZ) {
  513. break;
  514. }
  515. if (Type == REG_EXPAND_SZ) {
  516. break;
  517. }
  518. return NULL;
  519. default:
  520. if (Type == MustBeType) {
  521. break;
  522. }
  523. return NULL;
  524. }
  525. DataBuf = (PBYTE) AllocRoutine (BufSize + sizeof (WORD));
  526. rc = RegQueryValueExA (hKey, Value, NULL, NULL, DataBuf, &BufSize);
  527. if (rc == ERROR_SUCCESS) {
  528. *((PWORD) (DataBuf + BufSize)) = 0;
  529. return DataBuf;
  530. }
  531. MYASSERT (FALSE); //lint !e506
  532. FreeRoutine (DataBuf);
  533. SetLastError ((DWORD)rc);
  534. return NULL;
  535. }
  536. PBYTE
  537. GetRegValueDataOfType2W (
  538. IN HKEY hKey,
  539. IN PCWSTR Value,
  540. IN DWORD MustBeType,
  541. IN ALLOCATOR AllocRoutine,
  542. IN DEALLOCATOR FreeRoutine
  543. )
  544. {
  545. LONG rc;
  546. DWORD BufSize;
  547. PBYTE DataBuf;
  548. DWORD Type;
  549. rc = RegQueryValueExW (hKey, Value, NULL, &Type, NULL, &BufSize);
  550. if (rc != ERROR_SUCCESS) {
  551. SetLastError ((DWORD)rc);
  552. return NULL;
  553. }
  554. switch (MustBeType) {
  555. case REG_SZ:
  556. case REG_EXPAND_SZ:
  557. if (Type == REG_SZ) break;
  558. if (Type == REG_EXPAND_SZ) break;
  559. return NULL;
  560. case REG_DWORD:
  561. case REG_DWORD_BIG_ENDIAN:
  562. if (Type == REG_DWORD) break;
  563. if (Type == REG_DWORD_BIG_ENDIAN) break;
  564. return NULL;
  565. default:
  566. if (Type == MustBeType) break;
  567. return NULL;
  568. }
  569. DataBuf = (PBYTE) AllocRoutine (BufSize + sizeof(WCHAR));
  570. rc = RegQueryValueExW (hKey, Value, NULL, NULL, DataBuf, &BufSize);
  571. if (rc == ERROR_SUCCESS) {
  572. *((PWSTR) (DataBuf + BufSize)) = 0;
  573. return DataBuf;
  574. }
  575. MYASSERT (FALSE); //lint !e506
  576. FreeRoutine (DataBuf);
  577. SetLastError ((DWORD)rc);
  578. return NULL;
  579. }
  580. BOOL
  581. GetRegValueTypeAndSizeA (
  582. IN HKEY Key,
  583. IN PCSTR ValueName,
  584. OUT PDWORD OutType, OPTIONAL
  585. OUT PDWORD OutSize OPTIONAL
  586. )
  587. {
  588. LONG rc;
  589. DWORD Type;
  590. DWORD Size = 0;
  591. rc = RegQueryValueExA (Key, ValueName, NULL, &Type, NULL, &Size);
  592. if (rc == ERROR_SUCCESS) {
  593. if (OutType) {
  594. *OutType = Type;
  595. }
  596. if (OutSize) {
  597. *OutSize = Size;
  598. }
  599. return TRUE;
  600. }
  601. return FALSE;
  602. }
  603. BOOL
  604. GetRegValueTypeAndSizeW (
  605. IN HKEY Key,
  606. IN PCWSTR ValueName,
  607. OUT PDWORD OutType, OPTIONAL
  608. OUT PDWORD OutSize OPTIONAL
  609. )
  610. {
  611. LONG rc;
  612. DWORD Type;
  613. DWORD Size;
  614. rc = RegQueryValueExW (Key, ValueName, NULL, &Type, NULL, &Size);
  615. if (rc == ERROR_SUCCESS) {
  616. if (OutType) {
  617. *OutType = Type;
  618. }
  619. if (OutSize) {
  620. *OutSize = Size;
  621. }
  622. return TRUE;
  623. }
  624. return FALSE;
  625. }
  626. /*++
  627. Routine Description:
  628. GetRegKeyData2A and GetRegKeyData2W return default data associated
  629. with a registry key. They open the specified subkey, query the value,
  630. close the subkey and return the data.
  631. Arguments:
  632. Parent - Specifies the key that contains SubKey.
  633. SubKey - Specifies the name of the subkey to obtain the default value for.
  634. AllocRoutine - Specifies the allocation routine, called to allocate a block of
  635. memory for the registry data.
  636. FreeRoutine - Specifies the deallocation routine, called to free the block of
  637. data if an error occurs.
  638. Return Value:
  639. A pointer to the block of data obtained from the subkey's default value,
  640. or NULL if the subkey does not exist or an error was encountered. Call
  641. GetLastError for a failure code.
  642. --*/
  643. PBYTE
  644. GetRegKeyData2A (
  645. IN HKEY Parent,
  646. IN PCSTR SubKey,
  647. IN ALLOCATOR AllocRoutine,
  648. IN DEALLOCATOR FreeRoutine
  649. )
  650. {
  651. HKEY SubKeyHandle;
  652. PBYTE Data;
  653. SubKeyHandle = OpenRegKeyA (Parent, SubKey);
  654. if (!SubKeyHandle) {
  655. return NULL;
  656. }
  657. Data = GetRegValueData2A (SubKeyHandle, "", AllocRoutine, FreeRoutine);
  658. CloseRegKey (SubKeyHandle);
  659. return Data;
  660. }
  661. PBYTE
  662. GetRegKeyData2W (
  663. IN HKEY Parent,
  664. IN PCWSTR SubKey,
  665. IN ALLOCATOR AllocRoutine,
  666. IN DEALLOCATOR FreeRoutine
  667. )
  668. {
  669. HKEY SubKeyHandle;
  670. PBYTE Data;
  671. SubKeyHandle = OpenRegKeyW (Parent, SubKey);
  672. if (!SubKeyHandle) {
  673. return NULL;
  674. }
  675. Data = GetRegValueData2W (SubKeyHandle, L"", AllocRoutine, FreeRoutine);
  676. CloseRegKey (SubKeyHandle);
  677. return Data;
  678. }
  679. /*++
  680. Routine Description:
  681. GetRegData2A and GetRegData2W open a registry key, query a value,
  682. close the registry key and return the value.
  683. Arguments:
  684. KeyString - Specifies the registry key to open
  685. ValueName - Specifies the value to query
  686. AllocRoutine - Specifies the allocation routine, used to allocate a block of
  687. memory to hold the value data
  688. FreeRoutine - Specifies the deallocation routine, used to free the block of
  689. memory when an error is encountered.
  690. Return Value:
  691. A pointer to the registry data retrieved, or NULL if the key or value
  692. does not exist, or if an error occurs. Call GetLastError for a failure code.
  693. --*/
  694. PBYTE
  695. GetRegData2A (
  696. IN PCSTR KeyString,
  697. IN PCSTR ValueName,
  698. IN ALLOCATOR AllocRoutine,
  699. IN DEALLOCATOR FreeRoutine
  700. )
  701. {
  702. HKEY Key;
  703. PBYTE Data;
  704. Key = OpenRegKeyStrA (KeyString);
  705. if (!Key) {
  706. return NULL;
  707. }
  708. Data = GetRegValueData2A (Key, ValueName, AllocRoutine, FreeRoutine);
  709. CloseRegKey (Key);
  710. return Data;
  711. }
  712. PBYTE
  713. GetRegData2W (
  714. IN PCWSTR KeyString,
  715. IN PCWSTR ValueName,
  716. IN ALLOCATOR AllocRoutine,
  717. IN DEALLOCATOR FreeRoutine
  718. )
  719. {
  720. HKEY Key;
  721. PBYTE Data;
  722. Key = OpenRegKeyStrW (KeyString);
  723. if (!Key) {
  724. return NULL;
  725. }
  726. Data = GetRegValueData2W (Key, ValueName, AllocRoutine, FreeRoutine);
  727. CloseRegKey (Key);
  728. return Data;
  729. }
  730. BOOL
  731. GetRegSubkeysCount (
  732. IN HKEY ParentKey,
  733. OUT PDWORD SubKeyCount, OPTIONAL
  734. OUT PDWORD MaxSubKeyLen OPTIONAL
  735. )
  736. /*++
  737. Routine Description:
  738. GetRegSubkeysCount retrieves the number of subkeys of a given parent key.
  739. Arguments:
  740. ParentKey - Specifies a handle to the parent registry key.
  741. SubKeyCount - Receives the number of subkeys
  742. MaxSubKeyLen - Receives the length, in chars, of the longest subkey string
  743. Return Value:
  744. TRUE if the count was retrieved successfully, FALSE otherwise.
  745. In this case, call GetLastError for a failure code.
  746. --*/
  747. {
  748. LONG rc;
  749. rc = RegQueryInfoKey (
  750. ParentKey,
  751. NULL,
  752. NULL,
  753. NULL,
  754. SubKeyCount,
  755. MaxSubKeyLen,
  756. NULL,
  757. NULL,
  758. NULL,
  759. NULL,
  760. NULL,
  761. NULL
  762. );
  763. if (rc != ERROR_SUCCESS) {
  764. return FALSE;
  765. }
  766. return TRUE;
  767. }
  768. /*++
  769. Routine Description:
  770. CreateRegKeyA and CreateRegKeyW create a subkey if it does not
  771. exist already, or open a subkey if it already exists.
  772. Arguments:
  773. ParentKey - Specifies a handle to the parent registry key to contain
  774. the new key.
  775. NewKeyName - Specifies the name of the subkey to create or open.
  776. Return Value:
  777. The handle to an open registry key upon success, or NULL if an
  778. error occurred. Call GetLastError for a failure code.
  779. --*/
  780. HKEY
  781. pCreateRegKeyWorkerA (
  782. IN HKEY ParentKey,
  783. IN PCSTR NewKeyName
  784. DEBUG_TRACKING_PARAMS
  785. )
  786. {
  787. LONG rc;
  788. HKEY SubKey;
  789. DWORD DontCare;
  790. rc = OurRegCreateKeyExA (
  791. ParentKey,
  792. NewKeyName,
  793. 0,
  794. NULL,
  795. 0,
  796. g_CreateSam,
  797. NULL,
  798. &SubKey,
  799. &DontCare
  800. DEBUG_TRACKING_ARGS
  801. );
  802. if (rc != ERROR_SUCCESS) {
  803. SetLastError ((DWORD)rc);
  804. return NULL;
  805. }
  806. return SubKey;
  807. }
  808. HKEY
  809. RealCreateRegKeyA (
  810. IN HKEY ParentKey,
  811. IN PCSTR NewKeyName
  812. DEBUG_TRACKING_PARAMS
  813. )
  814. {
  815. HKEY result;
  816. result = pCreateRegKeyWorkerA (ParentKey, NewKeyName /* , */ DEBUG_TRACKING_ARGS);
  817. RegAddKeyToCacheA ("", result, g_CreateSam);
  818. return result;
  819. }
  820. HKEY
  821. pCreateRegKeyWorkerW (
  822. IN HKEY ParentKey,
  823. IN PCWSTR NewKeyName
  824. DEBUG_TRACKING_PARAMS
  825. )
  826. {
  827. LONG rc;
  828. HKEY SubKey;
  829. DWORD DontCare;
  830. rc = OurRegCreateKeyExW (
  831. ParentKey,
  832. NewKeyName,
  833. 0,
  834. NULL,
  835. 0,
  836. g_CreateSam,
  837. NULL,
  838. &SubKey,
  839. &DontCare
  840. DEBUG_TRACKING_ARGS
  841. );
  842. if (rc != ERROR_SUCCESS) {
  843. SetLastError ((DWORD)rc);
  844. return NULL;
  845. }
  846. return SubKey;
  847. }
  848. HKEY
  849. RealCreateRegKeyW (
  850. IN HKEY ParentKey,
  851. IN PCWSTR NewKeyName
  852. DEBUG_TRACKING_PARAMS
  853. )
  854. {
  855. HKEY result;
  856. result = pCreateRegKeyWorkerW (ParentKey, NewKeyName /* , */ DEBUG_TRACKING_ARGS);
  857. RegAddKeyToCacheW (L"", result, g_CreateSam);
  858. return result;
  859. }
  860. /*++
  861. Routine Description:
  862. CreateRegKeyStrA and CreateRegKeyStrW create a subkey if it does not
  863. exist already, or open a subkey if it already exists.
  864. Arguments:
  865. NewKeyName - Specifies the full path to the key to create or open.
  866. Return Value:
  867. The handle to an open registry key upon success, or NULL if an
  868. error occurred. Call GetLastError for a failure code.
  869. --*/
  870. HKEY
  871. RealCreateRegKeyStrA (
  872. IN PCSTR NewKeyName
  873. DEBUG_TRACKING_PARAMS
  874. )
  875. {
  876. LONG rc;
  877. DWORD DontCare;
  878. CHAR RegKey[MAX_REGISTRY_KEYA];
  879. PCSTR Start;
  880. PCSTR End;
  881. HKEY Parent, NewKey;
  882. BOOL CloseParent = FALSE;
  883. DWORD EndPos;
  884. PCSTR lastWack;
  885. HKEY parentKey;
  886. //
  887. // Attempt to use cache
  888. //
  889. NewKey = RegGetKeyFromCacheA (NewKeyName, NULL, g_CreateSam, TRUE);
  890. if (NewKey) {
  891. TRACK_KEYA (NewKey, NewKeyName);
  892. return NewKey;
  893. }
  894. //
  895. // Attempt to use cache for parent
  896. //
  897. lastWack = _mbsrchr (NewKeyName, '\\');
  898. if (lastWack) {
  899. parentKey = RegGetKeyFromCacheA (NewKeyName, lastWack, g_CreateSam, FALSE);
  900. if (parentKey) {
  901. NewKey = pCreateRegKeyWorkerA (parentKey, lastWack + 1 /* , */ DEBUG_TRACKING_ARGS);
  902. RegAddKeyToCacheA (NewKeyName, NewKey, g_CreateSam);
  903. return NewKey;
  904. }
  905. }
  906. //
  907. // Get the root
  908. //
  909. Parent = ConvertRootStringToKeyA (NewKeyName, &EndPos);
  910. if (!Parent) {
  911. return NULL;
  912. }
  913. Start = &NewKeyName[EndPos];
  914. if (!(*Start)) {
  915. OurRegOpenRootKeyA (Parent, NewKeyName/* , */ DEBUG_TRACKING_ARGS);
  916. return Parent;
  917. }
  918. //
  919. // Create each node until entire key exists
  920. //
  921. NewKey = NULL;
  922. do {
  923. //
  924. // Find end of this node
  925. //
  926. End = _mbschr (Start, '\\');
  927. if (!End) {
  928. End = GetEndOfStringA (Start);
  929. }
  930. StringCopyABA (RegKey, Start, End);
  931. //
  932. // Try to open the key (unless it's the last in the string)
  933. //
  934. if (*End) { //lint !e613
  935. rc = OurRegOpenKeyExA (
  936. Parent,
  937. RegKey,
  938. 0,
  939. KEY_READ|KEY_CREATE_SUB_KEY,
  940. &NewKey
  941. DEBUG_TRACKING_ARGS
  942. );
  943. if (rc != ERROR_SUCCESS) {
  944. NewKey = NULL;
  945. }
  946. } else {
  947. NewKey = NULL;
  948. }
  949. //
  950. // If open failed, create the key
  951. //
  952. if (NewKey) {
  953. rc = ERROR_SUCCESS;
  954. } else {
  955. rc = OurRegCreateKeyExA (
  956. Parent,
  957. RegKey,
  958. 0,
  959. NULL,
  960. 0,
  961. g_CreateSam,
  962. NULL,
  963. &NewKey,
  964. &DontCare
  965. DEBUG_TRACKING_ARGS
  966. );
  967. }
  968. if (CloseParent) {
  969. CloseRegKey (Parent);
  970. }
  971. if (rc != ERROR_SUCCESS) {
  972. SetLastError ((DWORD)rc);
  973. return NULL;
  974. }
  975. Parent = NewKey;
  976. CloseParent = TRUE;
  977. //
  978. // Go to next node
  979. //
  980. Start = End;
  981. if (*Start) { //lint !e613
  982. Start = _mbsinc (Start);
  983. }
  984. } while (*Start); //lint !e613
  985. if (Parent) {
  986. RegAddKeyToCacheA (NewKeyName, Parent, g_CreateSam);
  987. RegRecordParentInCacheA (NewKeyName, lastWack);
  988. }
  989. return Parent;
  990. }
  991. HKEY
  992. RealCreateRegKeyStrW (
  993. IN PCWSTR NewKeyName
  994. DEBUG_TRACKING_PARAMS
  995. )
  996. {
  997. LONG rc;
  998. DWORD DontCare;
  999. WCHAR RegKey[MAX_REGISTRY_KEYW];
  1000. PCWSTR Start;
  1001. PCWSTR End;
  1002. HKEY Parent, NewKey;
  1003. BOOL CloseParent = FALSE;
  1004. DWORD EndPos;
  1005. PCWSTR lastWack;
  1006. HKEY parentKey;
  1007. //
  1008. // Attempt to use cache
  1009. //
  1010. NewKey = RegGetKeyFromCacheW (NewKeyName, NULL, g_CreateSam, TRUE);
  1011. if (NewKey) {
  1012. TRACK_KEYW (NewKey, NewKeyName);
  1013. return NewKey;
  1014. }
  1015. //
  1016. // Attempt to use cache for parent
  1017. //
  1018. lastWack = wcsrchr (NewKeyName, L'\\');
  1019. if (lastWack) {
  1020. parentKey = RegGetKeyFromCacheW (NewKeyName, lastWack, g_CreateSam, FALSE);
  1021. if (parentKey) {
  1022. NewKey = pCreateRegKeyWorkerW (parentKey, lastWack + 1 /* , */ DEBUG_TRACKING_ARGS);
  1023. RegAddKeyToCacheW (NewKeyName, NewKey, g_CreateSam);
  1024. return NewKey;
  1025. }
  1026. }
  1027. //
  1028. // Get the root
  1029. //
  1030. Parent = ConvertRootStringToKeyW (NewKeyName, &EndPos);
  1031. if (!Parent) {
  1032. return NULL;
  1033. }
  1034. Start = &NewKeyName[EndPos];
  1035. if (!(*Start)) {
  1036. OurRegOpenRootKeyW (Parent, NewKeyName/* , */ DEBUG_TRACKING_ARGS);
  1037. return Parent;
  1038. }
  1039. //
  1040. // Create each node until entire key exists
  1041. //
  1042. NewKey = NULL;
  1043. do {
  1044. //
  1045. // Find end of this node
  1046. //
  1047. End = wcschr (Start, '\\');
  1048. if (!End) {
  1049. End = GetEndOfStringW (Start);
  1050. }
  1051. StringCopyABW (RegKey, Start, End);
  1052. //
  1053. // Try to open the key (unless it's the last in the string)
  1054. //
  1055. if (*End) {
  1056. rc = OurRegOpenKeyExW (
  1057. Parent,
  1058. RegKey,
  1059. 0,
  1060. KEY_READ|KEY_CREATE_SUB_KEY,
  1061. &NewKey
  1062. DEBUG_TRACKING_ARGS
  1063. );
  1064. if (rc != ERROR_SUCCESS) {
  1065. NewKey = NULL;
  1066. }
  1067. } else {
  1068. NewKey = NULL;
  1069. }
  1070. //
  1071. // If open failed, create the key
  1072. //
  1073. if (NewKey) {
  1074. rc = ERROR_SUCCESS;
  1075. } else {
  1076. rc = OurRegCreateKeyExW (
  1077. Parent,
  1078. RegKey,
  1079. 0,
  1080. NULL,
  1081. 0,
  1082. g_CreateSam,
  1083. NULL,
  1084. &NewKey,
  1085. &DontCare
  1086. DEBUG_TRACKING_ARGS
  1087. );
  1088. }
  1089. if (CloseParent) {
  1090. CloseRegKey (Parent);
  1091. }
  1092. if (rc != ERROR_SUCCESS) {
  1093. SetLastError ((DWORD)rc);
  1094. return NULL;
  1095. }
  1096. Parent = NewKey;
  1097. CloseParent = TRUE;
  1098. //
  1099. // Go to next node
  1100. //
  1101. Start = End;
  1102. if (*Start) {
  1103. Start++;
  1104. }
  1105. } while (*Start);
  1106. if (Parent) {
  1107. RegAddKeyToCacheW (NewKeyName, Parent, g_CreateSam);
  1108. RegRecordParentInCacheW (NewKeyName, lastWack);
  1109. }
  1110. return Parent;
  1111. }
  1112. /*++
  1113. Routine Description:
  1114. OpenRegKeyA and OpenRegKeyW open a subkey.
  1115. Arguments:
  1116. ParentKey - Specifies a handle to the parent registry key to contain
  1117. the subkey.
  1118. KeyToOpen - Specifies the name of the subkey to open.
  1119. Return Value:
  1120. The handle to an open registry key upon success, or NULL if an
  1121. error occurred. Call GetLastError for a failure code.
  1122. --*/
  1123. HKEY
  1124. OpenRegKeyWorkerA (
  1125. IN HKEY ParentKey,
  1126. IN PCSTR KeyToOpen OPTIONAL
  1127. DEBUG_TRACKING_PARAMS
  1128. )
  1129. {
  1130. HKEY SubKey;
  1131. LONG rc;
  1132. rc = OurRegOpenKeyExA (
  1133. ParentKey,
  1134. KeyToOpen,
  1135. 0,
  1136. g_OpenSam,
  1137. &SubKey
  1138. DEBUG_TRACKING_ARGS
  1139. );
  1140. if (rc != ERROR_SUCCESS) {
  1141. SetLastError ((DWORD)rc);
  1142. return NULL;
  1143. }
  1144. return SubKey;
  1145. }
  1146. HKEY
  1147. RealOpenRegKeyA (
  1148. IN HKEY ParentKey,
  1149. IN PCSTR KeyToOpen OPTIONAL
  1150. DEBUG_TRACKING_PARAMS
  1151. )
  1152. {
  1153. HKEY result;
  1154. result = OpenRegKeyWorkerA (ParentKey, KeyToOpen /* , */ DEBUG_TRACKING_ARGS);
  1155. RegAddKeyToCacheA ("", result, g_OpenSam);
  1156. return result;
  1157. }
  1158. HKEY
  1159. OpenRegKeyWorkerW (
  1160. IN HKEY ParentKey,
  1161. IN PCWSTR KeyToOpen
  1162. DEBUG_TRACKING_PARAMS
  1163. )
  1164. {
  1165. LONG rc;
  1166. HKEY SubKey;
  1167. rc = OurRegOpenKeyExW (
  1168. ParentKey,
  1169. KeyToOpen,
  1170. 0,
  1171. g_OpenSam,
  1172. &SubKey
  1173. DEBUG_TRACKING_ARGS
  1174. );
  1175. if (rc != ERROR_SUCCESS) {
  1176. SetLastError ((DWORD)rc);
  1177. return NULL;
  1178. }
  1179. return SubKey;
  1180. }
  1181. HKEY
  1182. RealOpenRegKeyW (
  1183. IN HKEY ParentKey,
  1184. IN PCWSTR KeyToOpen OPTIONAL
  1185. DEBUG_TRACKING_PARAMS
  1186. )
  1187. {
  1188. HKEY result;
  1189. result = OpenRegKeyWorkerW (ParentKey, KeyToOpen /* , */ DEBUG_TRACKING_ARGS);
  1190. RegAddKeyToCacheW (L"", result, g_OpenSam);
  1191. return result;
  1192. }
  1193. LONG
  1194. CloseRegKeyWorker (
  1195. IN HKEY Key
  1196. )
  1197. {
  1198. LONG rc = ERROR_INVALID_HANDLE;
  1199. if (!Key) {
  1200. return ERROR_SUCCESS;
  1201. }
  1202. if (GetOffsetOfRootKey (Key)) {
  1203. return ERROR_SUCCESS;
  1204. }
  1205. __try {
  1206. rc = RegCloseKey (Key);
  1207. }
  1208. __except (TRUE) {
  1209. DEBUGMSG ((DBG_WHOOPS, "RegCloseKey threw an exception!"));
  1210. }
  1211. MYASSERT (rc == ERROR_SUCCESS);
  1212. return rc;
  1213. }
  1214. LONG
  1215. RealCloseRegKey (
  1216. IN HKEY Key
  1217. )
  1218. /*++
  1219. Routine Description:
  1220. RealCloseRegKey closes the reg handle supplied, unless the handle is
  1221. a pre-defined Win32 handle. The CloseRegKey macro resolves directly
  1222. to this function in the free build, and to OurCloseRegKey in the
  1223. checked build.
  1224. Arguments:
  1225. Key - Specifies the reg handle to close
  1226. Return Value:
  1227. A standard Win32 error code indicating outcome.
  1228. --*/
  1229. {
  1230. if (RegDecrementRefCount (Key)) {
  1231. //
  1232. // Key is in the cache; don't call CloseRegKeyWorker. This will
  1233. // be done by the cache code.
  1234. //
  1235. return ERROR_SUCCESS;
  1236. }
  1237. return CloseRegKeyWorker (Key);
  1238. }
  1239. /*++
  1240. Routine Description:
  1241. GetOffsetOfRootString returns a non-zero offset to the g_RegRoots table
  1242. below. The offset can be used with GetRootStringFromOffset and
  1243. GetRootKeyFromOffset.
  1244. Arguments:
  1245. RootString - A pointer to a string containing the path to a registry key
  1246. LengthPtr - A pointer to a variable that receives the length of the
  1247. registry root, including the joining backslash if it exists.
  1248. Return Value:
  1249. A non-zero offset to the g_RegRoots table, or zero if RootString does not
  1250. contain a registry root.
  1251. --*/
  1252. typedef struct {
  1253. PCSTR RootText;
  1254. PCWSTR WideRootText;
  1255. UINT TextLength;
  1256. HKEY RootKey;
  1257. } REGISTRYROOT, *PREGISTRYROOT;
  1258. static
  1259. REGISTRYROOT g_RegRoots[] = {
  1260. "HKR", L"HKR", 3, HKEY_ROOT,
  1261. "HKEY_ROOT", L"HKEY_ROOT", 9, HKEY_ROOT,
  1262. "HKLM", L"HKLM", 4, HKEY_LOCAL_MACHINE,
  1263. "HKEY_LOCAL_MACHINE", L"HKEY_LOCAL_MACHINE", 18, HKEY_LOCAL_MACHINE,
  1264. "HKU", L"HKU", 3, HKEY_USERS,
  1265. "HKEY_USERS", L"HKEY_USERS", 10, HKEY_USERS,
  1266. "HKCU", L"HKCU", 4, HKEY_CURRENT_USER,
  1267. "HKEY_CURRENT_USER", L"HKEY_CURRENT_USER", 17, HKEY_CURRENT_USER,
  1268. "HKCC", L"HKCC", 4, HKEY_CURRENT_CONFIG,
  1269. "HKEY_CURRENT_CONFIG", L"HKEY_CURRENT_CONFIG", 19, HKEY_CURRENT_CONFIG,
  1270. "HKCR", L"HKCR", 4, HKEY_CLASSES_ROOT,
  1271. "HKEY_CLASSES_ROOT", L"HKEY_CLASSES_ROOT", 17, HKEY_CLASSES_ROOT,
  1272. "HKDD", L"HKDD", 4, HKEY_DYN_DATA,
  1273. "HKEY_DYN_DATA", L"HKEY_DYN_DATA", 13, HKEY_DYN_DATA,
  1274. NULL, NULL, 0, NULL
  1275. };
  1276. #define REGROOTS 14
  1277. INT
  1278. GetOffsetOfRootStringA (
  1279. IN PCSTR RootString,
  1280. OUT PDWORD LengthPtr OPTIONAL
  1281. )
  1282. {
  1283. int i;
  1284. MBCHAR c;
  1285. for (i = 0 ; g_RegRoots[i].RootText ; i++) {
  1286. if (StringIMatchCharCountA (
  1287. RootString,
  1288. g_RegRoots[i].RootText,
  1289. g_RegRoots[i].TextLength
  1290. )) {
  1291. c = _mbsgetc (RootString, g_RegRoots[i].TextLength);
  1292. if (c && c != '\\') {
  1293. continue;
  1294. }
  1295. if (LengthPtr) {
  1296. *LengthPtr = g_RegRoots[i].TextLength;
  1297. if (c) {
  1298. *LengthPtr += 1;
  1299. }
  1300. }
  1301. return i + 1;
  1302. }
  1303. }
  1304. return 0;
  1305. }
  1306. INT
  1307. GetOffsetOfRootStringW (
  1308. IN PCWSTR RootString,
  1309. OUT PDWORD LengthPtr OPTIONAL
  1310. )
  1311. {
  1312. int i;
  1313. WCHAR c;
  1314. for (i = 0 ; g_RegRoots[i].RootText ; i++) {
  1315. if (!_wcsnicmp (RootString, g_RegRoots[i].WideRootText,
  1316. g_RegRoots[i].TextLength)
  1317. ) {
  1318. c = _wcsgetc (RootString, g_RegRoots[i].TextLength);
  1319. if (c && c != L'\\') {
  1320. continue;
  1321. }
  1322. if (LengthPtr) {
  1323. *LengthPtr = g_RegRoots[i].TextLength;
  1324. if (c) {
  1325. *LengthPtr += 1;
  1326. }
  1327. }
  1328. return i + 1;
  1329. }
  1330. }
  1331. return 0;
  1332. }
  1333. /*++
  1334. Routine Description:
  1335. GetOffsetOfRootKey returns a non-zero offset to the g_RegRoots table
  1336. corresponding to the root that matches the supplied HKEY. This offset
  1337. can be used with GetRootStringFromOffset and GetRootKeyFromOffset.
  1338. Arguments:
  1339. RootKey - Supplies the handle to locate in g_RegRoots table
  1340. Return Value:
  1341. A non-zero offset to the g_RegRoots table, or zero if the handle is not
  1342. a registry root.
  1343. --*/
  1344. INT
  1345. GetOffsetOfRootKey (
  1346. IN HKEY RootKey
  1347. )
  1348. {
  1349. INT i;
  1350. if (RootKey == g_Root) {
  1351. return 1;
  1352. }
  1353. for (i = 0 ; g_RegRoots[i].RootText ; i++) {
  1354. if (g_RegRoots[i].RootKey == RootKey) {
  1355. return i + 1;
  1356. }
  1357. }
  1358. return 0;
  1359. }
  1360. /*++
  1361. Routine Description:
  1362. GetRootStringFromOffset and GetRootKeyFromOffset return a pointer to a
  1363. static string or HKEY, respectively. If the offset supplied is invalid,
  1364. these functions return NULL.
  1365. Arguments:
  1366. i - The offset as returned by GetOffsetOfRootString or GetOffsetOfRootKey
  1367. Return Value:
  1368. A pointer to a static string/HKEY, or NULL if offset is invalid
  1369. --*/
  1370. PCSTR
  1371. GetRootStringFromOffsetA (
  1372. IN INT i
  1373. )
  1374. {
  1375. if (i < 1 || i > REGROOTS) {
  1376. return NULL;
  1377. }
  1378. return g_RegRoots[i - 1].RootText;
  1379. }
  1380. PCWSTR
  1381. GetRootStringFromOffsetW (
  1382. IN INT i
  1383. )
  1384. {
  1385. if (i < 1 || i > REGROOTS) {
  1386. return NULL;
  1387. }
  1388. return g_RegRoots[i - 1].WideRootText;
  1389. }
  1390. HKEY
  1391. GetRootKeyFromOffset (
  1392. IN INT i
  1393. )
  1394. {
  1395. HKEY Ret;
  1396. if (i < 1 || i > REGROOTS) {
  1397. return NULL;
  1398. }
  1399. Ret = g_RegRoots[i - 1].RootKey;
  1400. if (Ret == HKEY_ROOT) {
  1401. Ret = g_Root;
  1402. }
  1403. return Ret;
  1404. }
  1405. /*++
  1406. Routine Description:
  1407. ConvertRootStringToKey converts a registry key path's root to an HKEY.
  1408. Arguments:
  1409. RegPath - A pointer to a registry string that has a root at the begining
  1410. LengthPtr - An optional pointer to a variable that receives the length of
  1411. the root, including the joining backslash if it exists.
  1412. Return Value:
  1413. A handle to the registry key, or NULL if RegPath does not have a root
  1414. --*/
  1415. HKEY
  1416. ConvertRootStringToKeyA (
  1417. PCSTR RegPath,
  1418. PDWORD LengthPtr OPTIONAL
  1419. )
  1420. {
  1421. return GetRootKeyFromOffset (GetOffsetOfRootStringA (RegPath, LengthPtr));
  1422. }
  1423. HKEY
  1424. ConvertRootStringToKeyW (
  1425. PCWSTR RegPath,
  1426. PDWORD LengthPtr OPTIONAL
  1427. )
  1428. {
  1429. return GetRootKeyFromOffset (GetOffsetOfRootStringW (RegPath, LengthPtr));
  1430. }
  1431. /*++
  1432. Routine Description:
  1433. ConvertKeyToRootString converts a root HKEY to a registry root string
  1434. Arguments:
  1435. RegRoot - A handle to a registry root
  1436. Return Value:
  1437. A pointer to a static string, or NULL if RegRoot is not a valid registry
  1438. root handle
  1439. --*/
  1440. PCSTR
  1441. ConvertKeyToRootStringA (
  1442. HKEY RegRoot
  1443. )
  1444. {
  1445. return GetRootStringFromOffsetA (GetOffsetOfRootKey (RegRoot));
  1446. }
  1447. PCWSTR
  1448. ConvertKeyToRootStringW (
  1449. HKEY RegRoot
  1450. )
  1451. {
  1452. return GetRootStringFromOffsetW (GetOffsetOfRootKey (RegRoot));
  1453. }
  1454. /*++
  1455. Routine Description:
  1456. CreateEncodedRegistryStringEx is used to create a registry string in the format commonly
  1457. expected by w95upg reg routines. This format is:
  1458. EncodedKey\[EncodedValue]
  1459. Encoding is used to safely represent "special" characters
  1460. (such as MBS chars and certain punctuation marks.)
  1461. The [EncodedValue] part will exist only if Value is non null.
  1462. Arguments:
  1463. Key - Contains an unencoded registry key.
  1464. Value - Optionally contains an unencoded registry value.
  1465. Tree - Specifies that the registry key refers to the entire key
  1466. Return Value:
  1467. Returns a pointer to the encoded registry string, or NULL if there was an error.
  1468. --*/
  1469. PCSTR
  1470. CreateEncodedRegistryStringExA (
  1471. IN PCSTR Key,
  1472. IN PCSTR Value, OPTIONAL
  1473. IN BOOL Tree
  1474. )
  1475. {
  1476. PSTR rEncodedString = NULL;
  1477. DWORD requiredSize;
  1478. PSTR end;
  1479. //
  1480. // Determine required size and allocate buffer large enough to hold
  1481. // the encoded string.
  1482. //
  1483. requiredSize = SizeOfStringA(Key)*6 + (Value ? SizeOfStringA(Value)*6 : 0) + 10;
  1484. rEncodedString = AllocPathStringA(requiredSize);
  1485. //
  1486. // Encode the key portion of the string.
  1487. //
  1488. EncodeRuleCharsA(rEncodedString, Key);
  1489. //
  1490. // Finally, if a value exists, append it in encoded form. If a value does not exist,
  1491. // then add an '*' to the line.
  1492. //
  1493. if (Value) {
  1494. StringCopyA (AppendWackA (rEncodedString), "[");
  1495. end = GetEndOfStringA (rEncodedString);
  1496. EncodeRuleCharsA(end, Value);
  1497. StringCatA(end, "]");
  1498. } else if (Tree) {
  1499. StringCopyA (AppendWackA (rEncodedString), "*");
  1500. }
  1501. return rEncodedString;
  1502. }
  1503. PCWSTR
  1504. CreateEncodedRegistryStringExW (
  1505. IN PCWSTR Key,
  1506. IN PCWSTR Value, OPTIONAL
  1507. IN BOOL Tree
  1508. )
  1509. {
  1510. PWSTR rEncodedString = NULL;
  1511. DWORD requiredSize;
  1512. PWSTR end;
  1513. //
  1514. // Determine required size and allocate buffer large enough to hold
  1515. // the encoded string.
  1516. //
  1517. requiredSize = SizeOfStringW(Key)*6 + (Value ? SizeOfStringW(Value)*6 : 0) + 10;
  1518. rEncodedString = AllocPathStringW(requiredSize);
  1519. if (!rEncodedString) {
  1520. return NULL;
  1521. }
  1522. //
  1523. // Encode the key portion of the string.
  1524. //
  1525. EncodeRuleCharsW(rEncodedString, Key);
  1526. //
  1527. // Finally, if a value exists, append it in encoded form.
  1528. // If a value doesn't exist, add na '*' to the line.
  1529. //
  1530. if (Value) {
  1531. StringCopyW (AppendWackW (rEncodedString), L"[");
  1532. end = GetEndOfStringW (rEncodedString);
  1533. if (!end) {
  1534. // we should never get here
  1535. return NULL;
  1536. }
  1537. EncodeRuleCharsW(end, Value);
  1538. StringCatW(end, L"]");
  1539. } else if (Tree) {
  1540. StringCopyW (AppendWackW (rEncodedString), L"*");
  1541. }
  1542. return rEncodedString;
  1543. }
  1544. /*++
  1545. Routine Description:
  1546. FreeEncodedRegistryString frees the memory allocated by a call to CreateEncodedRegistryString.
  1547. Arguments:
  1548. None.
  1549. Return Value:
  1550. None.
  1551. --*/
  1552. VOID
  1553. FreeEncodedRegistryStringA (
  1554. IN OUT PCSTR RegString
  1555. )
  1556. {
  1557. FreePathStringA(RegString);
  1558. }
  1559. VOID
  1560. FreeEncodedRegistryStringW (
  1561. IN OUT PCWSTR RegString
  1562. )
  1563. {
  1564. FreePathStringW(RegString);
  1565. }