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.

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