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

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