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.

556 lines
13 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1997.
  5. //
  6. // File: N C R E G . H
  7. //
  8. // Contents: Common routines for dealing with the registry.
  9. //
  10. // Notes:
  11. //
  12. // Author: danielwe 24 Mar 1997
  13. //
  14. //----------------------------------------------------------------------------
  15. #pragma once
  16. #ifndef _NCREG_H_
  17. #define _NCREG_H_
  18. #include "ncstring.h"
  19. // constants for HrRegQueryStringAsUlong
  20. const int c_nBase10 = 10;
  21. const int c_nBase16 = 16;
  22. const int c_cchMaxRegKeyLengthWithNull = 257;
  23. const DWORD KEY_READ_WRITE_DELETE = KEY_READ | KEY_WRITE | DELETE;
  24. const DWORD KEY_READ_WRITE = KEY_READ | KEY_WRITE;
  25. HRESULT
  26. HrRegAddStringToMultiSz (
  27. IN PCWSTR pszAddString,
  28. IN HKEY hkeyRoot,
  29. IN PCWSTR pszKeySubPath,
  30. IN PCWSTR pszValueName,
  31. IN DWORD dwFlags,
  32. IN DWORD dwIndex);
  33. HRESULT
  34. HrRegAddStringToSz (
  35. IN PCWSTR pszAddString,
  36. IN HKEY hkeyRoot,
  37. IN PCWSTR pszKeySubPath,
  38. IN PCWSTR pszValueName,
  39. IN WCHAR chDelimiter,
  40. IN DWORD dwFlags,
  41. IN DWORD dwStringIndex);
  42. HRESULT
  43. HrRegCopyHive (
  44. IN HKEY hkeySrc,
  45. IN HKEY hkeyDst);
  46. HRESULT
  47. HrRegCreateKeyEx (
  48. IN HKEY hkey,
  49. IN PCWSTR pszSubkey,
  50. IN DWORD dwOptions,
  51. IN REGSAM samDesired,
  52. IN LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  53. OUT PHKEY phkResult,
  54. OUT LPDWORD pdwDisposition);
  55. HRESULT
  56. HrRegDeleteKey (
  57. IN HKEY hkey,
  58. IN PCWSTR pszSubkey);
  59. HRESULT
  60. HrRegDeleteKeyTree (
  61. IN HKEY hkeyParent,
  62. IN PCWSTR pszRemoveKey);
  63. HRESULT
  64. HrRegDeleteValue (
  65. IN HKEY hkey,
  66. IN PCWSTR pszValueName);
  67. HRESULT
  68. HrRegEnumKey (
  69. IN HKEY hkey,
  70. IN DWORD dwIndex,
  71. OUT PWSTR pszSubkeyName,
  72. IN DWORD cchSubkeyName);
  73. HRESULT
  74. HrRegEnumKeyEx (
  75. IN HKEY hkey,
  76. IN DWORD dwIndex,
  77. OUT PWSTR pszSubkeyName,
  78. OUT LPDWORD pcchSubkeyName,
  79. OUT PWSTR pszClass,
  80. OUT LPDWORD pcchClass,
  81. OUT FILETIME* pftLastWriteTime);
  82. HRESULT
  83. HrRegEnumValue (
  84. HKEY hkey,
  85. DWORD dwIndex,
  86. PWSTR pszValueName,
  87. LPDWORD pcbValueName,
  88. LPDWORD pdwType,
  89. LPBYTE pbData,
  90. LPDWORD pcbData);
  91. HRESULT
  92. HrRegOpenKeyEx (
  93. HKEY hkey,
  94. PCWSTR pszSubkey,
  95. REGSAM samDesired,
  96. PHKEY phkResult);
  97. HRESULT
  98. HrRegOpenKeyBestAccess (
  99. HKEY hkey,
  100. PCWSTR pszSubkey,
  101. PHKEY phkResult);
  102. HRESULT
  103. HrRegDuplicateKeyEx (
  104. HKEY hkey,
  105. REGSAM samDesired,
  106. PHKEY phkResult);
  107. HRESULT
  108. HrRegQueryBinaryWithAlloc (
  109. HKEY hkey,
  110. PCWSTR pszValueName,
  111. LPBYTE* ppbValue,
  112. DWORD* pcbValue);
  113. HRESULT
  114. HrRegQueryDword (
  115. HKEY hkey,
  116. PCWSTR pszValueName,
  117. LPDWORD pdwValue);
  118. HRESULT
  119. HrRegQueryExpandString (
  120. HKEY hkey,
  121. PCWSTR pszValueName,
  122. tstring* pstrValue);
  123. HRESULT
  124. HrRegQueryInfoKey (
  125. HKEY hkey,
  126. PWSTR pszClass,
  127. LPDWORD pcbClass,
  128. LPDWORD pcSubKeys,
  129. LPDWORD pcbMaxSubKeyLen,
  130. LPDWORD pcbMaxClassLen,
  131. LPDWORD pcValues,
  132. LPDWORD pcbMaxValueNameLen,
  133. LPDWORD pcbMaxValueLen,
  134. LPDWORD pcbSecurityDescriptor,
  135. PFILETIME pftLastWriteTime);
  136. HRESULT
  137. HrRegQueryStringAsUlong (
  138. HKEY hkey,
  139. PCWSTR pszValueName,
  140. int nBase,
  141. ULONG* pulValue);
  142. HRESULT
  143. HrRegQueryTypeString (
  144. HKEY hkey,
  145. PCWSTR pszValueName,
  146. DWORD dwType,
  147. tstring* pstr);
  148. HRESULT
  149. HrRegQueryTypeSzBuffer (
  150. HKEY hkey,
  151. PCWSTR pszValueName,
  152. DWORD dwType,
  153. PWSTR pszData,
  154. DWORD* pcbData);
  155. HRESULT
  156. HrRegQueryValueEx (
  157. HKEY hkey,
  158. PCWSTR pszValueName,
  159. LPDWORD pdwType,
  160. LPBYTE pbData,
  161. LPDWORD pcbData);
  162. HRESULT
  163. HrRegQueryValueWithAlloc (
  164. HKEY hkey,
  165. PCWSTR pszValueName,
  166. LPDWORD pdwType,
  167. LPBYTE* ppbBuffer,
  168. LPDWORD pdwSize);
  169. HRESULT HrRegGetKeySecurity (
  170. HKEY hKey,
  171. SECURITY_INFORMATION SecurityInformation,
  172. PSECURITY_DESCRIPTOR pSecurityDescriptor,
  173. LPDWORD lpcbSecurityDescriptor
  174. );
  175. HRESULT HrRegSetKeySecurity (
  176. HKEY hKey,
  177. SECURITY_INFORMATION SecurityInformation,
  178. PSECURITY_DESCRIPTOR pSecurityDescriptor
  179. );
  180. template<class T>
  181. HRESULT HrRegQueryColString( HKEY hkey, PCWSTR pszValueName, T* pcolstr );
  182. inline
  183. HRESULT
  184. HrRegQueryString (
  185. HKEY hkey,
  186. PCWSTR pszValueName,
  187. tstring* pstr)
  188. {
  189. return HrRegQueryTypeString (hkey, pszValueName, REG_SZ, pstr);
  190. }
  191. HRESULT
  192. HrRegQueryTypeWithAlloc (
  193. HKEY hkey,
  194. PCWSTR pszValueName,
  195. DWORD dwType,
  196. LPBYTE* ppbValue,
  197. DWORD* pcbValue);
  198. inline
  199. HRESULT
  200. HrRegQueryBinaryWithAlloc (
  201. HKEY hkey,
  202. PCWSTR pszValueName,
  203. LPBYTE* ppbValue,
  204. DWORD* pcbValue)
  205. {
  206. return HrRegQueryTypeWithAlloc (hkey, pszValueName, REG_BINARY,
  207. ppbValue, pcbValue);
  208. }
  209. inline
  210. HRESULT
  211. HrRegQueryMultiSzWithAlloc (
  212. HKEY hkey,
  213. PCWSTR pszValueName,
  214. PWSTR* pszValue)
  215. {
  216. return HrRegQueryTypeWithAlloc (hkey, pszValueName, REG_MULTI_SZ,
  217. (LPBYTE*)pszValue, NULL);
  218. }
  219. inline
  220. HRESULT
  221. HrRegQuerySzWithAlloc (
  222. HKEY hkey,
  223. PCWSTR pszValueName,
  224. PWSTR* pszValue)
  225. {
  226. return HrRegQueryTypeWithAlloc (hkey, pszValueName, REG_SZ,
  227. (LPBYTE*)pszValue, NULL);
  228. }
  229. inline
  230. HRESULT
  231. HrRegQueryExpandSzBuffer (
  232. HKEY hkey,
  233. PCWSTR pszValueName,
  234. PWSTR pszData,
  235. DWORD* pcbData)
  236. {
  237. return HrRegQueryTypeSzBuffer (hkey, pszValueName, REG_EXPAND_SZ,
  238. pszData, pcbData);
  239. }
  240. inline
  241. HRESULT
  242. HrRegQuerySzBuffer (
  243. HKEY hkey,
  244. PCWSTR pszValueName,
  245. PWSTR pszData,
  246. DWORD* pcbData)
  247. {
  248. return HrRegQueryTypeSzBuffer (hkey, pszValueName, REG_SZ,
  249. pszData, pcbData);
  250. }
  251. HRESULT HrRegSaveKey(HKEY hkey, PCWSTR szFileName,
  252. LPSECURITY_ATTRIBUTES psa);
  253. HRESULT HrRegSetValueEx (HKEY hkey,
  254. PCWSTR szValueName,
  255. DWORD dwType,
  256. const BYTE *pbData,
  257. DWORD cbData);
  258. HRESULT HrRegRemoveStringFromSz( PCWSTR pszRemoveString,
  259. HKEY hkeyRoot,
  260. PCWSTR pszKeySubPath,
  261. PCWSTR pszValueName,
  262. WCHAR chDelimiter,
  263. DWORD dwFlags );
  264. HRESULT HrRegRemoveStringFromMultiSz (PCWSTR pszRemoveString,
  265. HKEY hkeyRoot,
  266. PCWSTR pszKeySubPath,
  267. PCWSTR pszValueName,
  268. DWORD dwFlags);
  269. HRESULT HrRegRestoreKey(HKEY hkey, PCWSTR pszFileName, DWORD dwFlags);
  270. HRESULT HrRegOpenAdapterKey(
  271. PCWSTR pszComponentName,
  272. BOOL fCreate,
  273. HKEY* phkey);
  274. //+---------------------------------------------------------------------------
  275. //
  276. // Function: HrRegSetColString
  277. //
  278. // Purpose: Sets a multi-sz in the registry using the collection of strings
  279. //
  280. // Arguments:
  281. // hkey [in] The registry key.
  282. // pszValueName [in] The name of the value to set.
  283. // colstr [in] The collection of tstrings to set.
  284. //
  285. // Returns: S_OK or an HRESULT_FROM_WIN32 error code.
  286. //
  287. // Author: mikemi 30 Apr 1997
  288. //
  289. // Notes:
  290. //
  291. //---------------------------------------------------------------------------
  292. template<class T>
  293. HRESULT HrRegSetColString(HKEY hkey, PCWSTR pszValueName, const T& colstr);
  294. HRESULT
  295. HrRegSetBool (
  296. IN HKEY hkey,
  297. IN PCWSTR pszValueName,
  298. IN BOOL fValue);
  299. HRESULT
  300. HrRegSetDword (
  301. IN HKEY hkey,
  302. IN PCWSTR pszValueName,
  303. IN DWORD dwValue);
  304. HRESULT
  305. HrRegSetGuidAsSz (
  306. IN HKEY hkey,
  307. IN PCWSTR pszValueName,
  308. IN const GUID& guid);
  309. //+---------------------------------------------------------------------------
  310. //
  311. // Function: HrRegSetMultiSz
  312. //
  313. // Purpose: Sets a multi-sz in the registry. Assures that its type and
  314. // size are correct. Easier to read than HrRegSetValueEx
  315. // with 5 parameters. Type safe (no LPBYTE stuff).
  316. //
  317. // Arguments:
  318. // hkey [in] The registry key.
  319. // pszValueName [in] The name of the value to set.
  320. // pszValue [in] The multi-sz to set.
  321. //
  322. // Returns: S_OK or an HRESULT_FROM_WIN32 error code.
  323. //
  324. // Author: shaunco 1 Apr 1997
  325. //
  326. // Notes:
  327. //
  328. inline HRESULT HrRegSetMultiSz (HKEY hkey, PCWSTR pszValueName, PCWSTR pszValue)
  329. {
  330. return HrRegSetValueEx (
  331. hkey,
  332. pszValueName,
  333. REG_MULTI_SZ,
  334. (LPBYTE)pszValue,
  335. (CchOfMultiSzAndTermSafe (pszValue) * sizeof(WCHAR)));
  336. }
  337. //+---------------------------------------------------------------------------
  338. //
  339. // Function: HrRegSetSz, HrRegSetString
  340. //
  341. // Purpose: Sets a string in the registry. Assures that its type and
  342. // size are correct. Easier to read than HrRegSetValueEx
  343. // with 5 parameters. Type safe (no LPBYTE stuff).
  344. //
  345. // Arguments:
  346. // hkey [in] The registry key.
  347. // pszValueName [in] The name of the value to set.
  348. // pszValue, str [in] The string to set.
  349. //
  350. // Returns: S_OK or an HRESULT_FROM_WIN32 error code.
  351. //
  352. // Author: shaunco 1 Apr 1997
  353. //
  354. // Notes:
  355. //
  356. inline HRESULT HrRegSetSz (HKEY hkey, PCWSTR pszValueName, PCWSTR pszValue)
  357. {
  358. return HrRegSetValueEx (hkey, pszValueName, REG_SZ,
  359. (LPBYTE)pszValue,
  360. CbOfSzAndTermSafe (pszValue));
  361. }
  362. inline HRESULT HrRegSetString (HKEY hkey, PCWSTR pszValueName, const tstring& str)
  363. {
  364. return HrRegSetSz (hkey, pszValueName, str.c_str());
  365. }
  366. //+---------------------------------------------------------------------------
  367. //
  368. // Function: HrRegSetBinary
  369. //
  370. // Purpose: Sets a binary value into the registry. Assures the type is
  371. // correct.
  372. //
  373. // Arguments:
  374. // hkey [in] The registry key.
  375. // pszValueName [in] The name of the value to set.
  376. // pbData [in] Buffer containing binary data to write.
  377. // cbData [in] Size of buffer in bytes.
  378. //
  379. // Returns: S_OK or an HRESULT_FROM_WIN32 error code.
  380. //
  381. // Author: danielwe 16 Apr 1997
  382. //
  383. // Notes:
  384. //
  385. inline HRESULT HrRegSetBinary(HKEY hkey, PCWSTR pszValueName,
  386. const BYTE *pbData, DWORD cbData)
  387. {
  388. return HrRegSetValueEx(hkey, pszValueName, REG_BINARY,
  389. pbData, cbData);
  390. }
  391. //+---------------------------------------------------------------------------
  392. //
  393. // Function: HrRegSetSzAsUlong
  394. //
  395. // Purpose: Writes the given ulong to the given registry value as a
  396. // REG_SZ.
  397. //
  398. // Arguments:
  399. // hkey [in] See Win32 docs.
  400. // pszValueName [in] See Win32 docs.
  401. // ulValue [in] The value to write as a string
  402. // nBase [in] The radix to convert the ulong from
  403. //
  404. // Returns: S_OK or an HRESULT_FROM_WIN32 error code.
  405. //
  406. // Author: billbe 14 Jun 1997
  407. //
  408. // Notes:
  409. //
  410. inline
  411. HRESULT
  412. HrRegSetSzAsUlong (
  413. IN HKEY hkey,
  414. IN PCWSTR pszValueName,
  415. IN ULONG ulValue,
  416. IN int nBase)
  417. {
  418. WCHAR szBuffer[20];
  419. // convert the value to a string using the specified base
  420. _ultow(ulValue, szBuffer, nBase);
  421. return HrRegSetSz(hkey, pszValueName, szBuffer);
  422. }
  423. //
  424. // Data structures
  425. //
  426. const HKEY HKLM_SVCS = (HKEY)(INT_PTR)(int)(0x80000007);
  427. const DWORD REG_MIN = REG_QWORD;
  428. const DWORD REG_CREATE = (REG_MIN + 1);
  429. const DWORD REG_BOOL = (REG_MIN + 2);
  430. const DWORD REG_IP = (REG_MIN + 3);
  431. const DWORD REG_FILE = (REG_MIN + 4);
  432. const DWORD REG_HEX = (REG_MIN + 5);
  433. struct VALUETABLE
  434. {
  435. // Name of value key
  436. PCWSTR pszValueName;
  437. // Data and offset location
  438. DWORD dwType;
  439. INT cbOffset;
  440. // Default values
  441. BYTE* pbDefault;
  442. };
  443. struct REGBATCH
  444. {
  445. // Location of the registry entry
  446. HKEY hkey;
  447. PCWSTR pszSubkey;
  448. PCWSTR pszValueName;
  449. // Data and offset location
  450. DWORD dwType;
  451. INT cbOffset;
  452. // Default values
  453. BYTE* pbDefault;
  454. };
  455. VOID
  456. RegReadValues (
  457. IN INT crvc,
  458. IN const REGBATCH* arb,
  459. OUT const BYTE* pbUserData,
  460. IN REGSAM samDesired);
  461. HRESULT
  462. HrRegWriteValues (
  463. IN INT crvc,
  464. IN const REGBATCH* arb,
  465. IN const BYTE* pbUserData,
  466. IN DWORD dwOptions,
  467. IN REGSAM samDesired);
  468. HRESULT
  469. HrRegWriteValueTable(
  470. IN HKEY hkeyRoot,
  471. IN INT cvt,
  472. IN const VALUETABLE* avt,
  473. IN const BYTE* pbUserData,
  474. IN DWORD dwOptions,
  475. IN REGSAM samDesired);
  476. VOID
  477. RegSafeCloseKey (
  478. IN HKEY hkey);
  479. #endif // _NCREG_H_