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.

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