Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

562 lines
14 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. inline
  181. HRESULT
  182. HrRegQueryString (
  183. HKEY hkey,
  184. PCWSTR pszValueName,
  185. tstring* pstr)
  186. {
  187. return HrRegQueryTypeString (hkey, pszValueName, REG_SZ, pstr);
  188. }
  189. HRESULT
  190. HrRegQueryTypeWithAlloc (
  191. HKEY hkey,
  192. PCWSTR pszValueName,
  193. DWORD dwType,
  194. LPBYTE* ppbValue,
  195. DWORD* pcbValue);
  196. inline
  197. HRESULT
  198. HrRegQueryBinaryWithAlloc (
  199. HKEY hkey,
  200. PCWSTR pszValueName,
  201. LPBYTE* ppbValue,
  202. DWORD* pcbValue)
  203. {
  204. return HrRegQueryTypeWithAlloc (hkey, pszValueName, REG_BINARY,
  205. ppbValue, pcbValue);
  206. }
  207. inline
  208. HRESULT
  209. HrRegQueryMultiSzWithAlloc (
  210. HKEY hkey,
  211. PCWSTR pszValueName,
  212. PWSTR* pszValue)
  213. {
  214. return HrRegQueryTypeWithAlloc (hkey, pszValueName, REG_MULTI_SZ,
  215. (LPBYTE*)pszValue, NULL);
  216. }
  217. inline
  218. HRESULT
  219. HrRegQuerySzWithAlloc (
  220. HKEY hkey,
  221. PCWSTR pszValueName,
  222. PWSTR* pszValue)
  223. {
  224. return HrRegQueryTypeWithAlloc (hkey, pszValueName, REG_SZ,
  225. (LPBYTE*)pszValue, NULL);
  226. }
  227. inline
  228. HRESULT
  229. HrRegQueryExpandSzBuffer (
  230. HKEY hkey,
  231. PCWSTR pszValueName,
  232. PWSTR pszData,
  233. DWORD* pcbData)
  234. {
  235. return HrRegQueryTypeSzBuffer (hkey, pszValueName, REG_EXPAND_SZ,
  236. pszData, pcbData);
  237. }
  238. inline
  239. HRESULT
  240. HrRegQuerySzBuffer (
  241. HKEY hkey,
  242. PCWSTR pszValueName,
  243. PWSTR pszData,
  244. DWORD* pcbData)
  245. {
  246. return HrRegQueryTypeSzBuffer (hkey, pszValueName, REG_SZ,
  247. pszData, pcbData);
  248. }
  249. HRESULT HrRegSaveKey(HKEY hkey, PCWSTR szFileName,
  250. LPSECURITY_ATTRIBUTES psa);
  251. HRESULT HrRegSetValueEx (HKEY hkey,
  252. PCWSTR szValueName,
  253. DWORD dwType,
  254. const BYTE *pbData,
  255. DWORD cbData);
  256. HRESULT HrRegRemoveStringFromSz( PCWSTR pszRemoveString,
  257. HKEY hkeyRoot,
  258. PCWSTR pszKeySubPath,
  259. PCWSTR pszValueName,
  260. WCHAR chDelimiter,
  261. DWORD dwFlags );
  262. HRESULT HrRegRemoveStringFromMultiSz (PCWSTR pszRemoveString,
  263. HKEY hkeyRoot,
  264. PCWSTR pszKeySubPath,
  265. PCWSTR pszValueName,
  266. DWORD dwFlags);
  267. HRESULT HrRegRestoreKey(HKEY hkey, PCWSTR pszFileName, DWORD dwFlags);
  268. HRESULT HrRegOpenAdapterKey(
  269. PCWSTR pszComponentName,
  270. BOOL fCreate,
  271. HKEY* phkey);
  272. //+---------------------------------------------------------------------------
  273. //
  274. // Function: HrRegSetColString
  275. //
  276. // Purpose: Sets a multi-sz in the registry using the collection of strings
  277. //
  278. // Arguments:
  279. // hkey [in] The registry key.
  280. // pszValueName [in] The name of the value to set.
  281. // colstr [in] The collection of tstrings to set.
  282. //
  283. // Returns: S_OK or an HRESULT_FROM_WIN32 error code.
  284. //
  285. // Author: mikemi 30 Apr 1997
  286. //
  287. // Notes:
  288. //
  289. //---------------------------------------------------------------------------
  290. // template<class T>
  291. // HRESULT HrRegSetColString(HKEY hkey, PCWSTR pszValueName, const T& colstr);
  292. // template<class T>
  293. // HRESULT HrRegQueryColString( HKEY hkey, PCWSTR pszValueName, T* pcolstr );
  294. HRESULT HrRegSetColString(IN HKEY hkey, IN PCWSTR pszValueName, IN const list<tstring*>& colstr);
  295. HRESULT HrRegSetColString(IN HKEY hkey, IN PCWSTR pszValueName, IN const vector<tstring*>& colstr);
  296. HRESULT HrRegQueryColString(IN HKEY hkey, IN PCWSTR pszValueName, OUT list<tstring*>* pcolstr );
  297. HRESULT HrRegQueryColString(IN HKEY hkey, IN PCWSTR pszValueName, OUT vector<tstring*>* pcolstr );
  298. HRESULT
  299. HrRegSetBool (
  300. IN HKEY hkey,
  301. IN PCWSTR pszValueName,
  302. IN BOOL fValue);
  303. HRESULT
  304. HrRegSetDword (
  305. IN HKEY hkey,
  306. IN PCWSTR pszValueName,
  307. IN DWORD dwValue);
  308. HRESULT
  309. HrRegSetGuidAsSz (
  310. IN HKEY hkey,
  311. IN PCWSTR pszValueName,
  312. IN const GUID& guid);
  313. //+---------------------------------------------------------------------------
  314. //
  315. // Function: HrRegSetMultiSz
  316. //
  317. // Purpose: Sets a multi-sz in the registry. Assures that its type and
  318. // size are correct. Easier to read than HrRegSetValueEx
  319. // with 5 parameters. Type safe (no LPBYTE stuff).
  320. //
  321. // Arguments:
  322. // hkey [in] The registry key.
  323. // pszValueName [in] The name of the value to set.
  324. // pszValue [in] The multi-sz to set.
  325. //
  326. // Returns: S_OK or an HRESULT_FROM_WIN32 error code.
  327. //
  328. // Author: shaunco 1 Apr 1997
  329. //
  330. // Notes:
  331. //
  332. inline HRESULT HrRegSetMultiSz (HKEY hkey, PCWSTR pszValueName, PCWSTR pszValue)
  333. {
  334. return HrRegSetValueEx (
  335. hkey,
  336. pszValueName,
  337. REG_MULTI_SZ,
  338. (LPBYTE)pszValue,
  339. (CchOfMultiSzAndTermSafe (pszValue) * sizeof(WCHAR)));
  340. }
  341. //+---------------------------------------------------------------------------
  342. //
  343. // Function: HrRegSetSz, HrRegSetString
  344. //
  345. // Purpose: Sets a string in the registry. Assures that its type and
  346. // size are correct. Easier to read than HrRegSetValueEx
  347. // with 5 parameters. Type safe (no LPBYTE stuff).
  348. //
  349. // Arguments:
  350. // hkey [in] The registry key.
  351. // pszValueName [in] The name of the value to set.
  352. // pszValue, str [in] The string to set.
  353. //
  354. // Returns: S_OK or an HRESULT_FROM_WIN32 error code.
  355. //
  356. // Author: shaunco 1 Apr 1997
  357. //
  358. // Notes:
  359. //
  360. inline HRESULT HrRegSetSz (HKEY hkey, PCWSTR pszValueName, PCWSTR pszValue)
  361. {
  362. return HrRegSetValueEx (hkey, pszValueName, REG_SZ,
  363. (LPBYTE)pszValue,
  364. CbOfSzAndTermSafe (pszValue));
  365. }
  366. inline HRESULT HrRegSetString (HKEY hkey, PCWSTR pszValueName, const tstring& str)
  367. {
  368. return HrRegSetSz (hkey, pszValueName, str.c_str());
  369. }
  370. //+---------------------------------------------------------------------------
  371. //
  372. // Function: HrRegSetBinary
  373. //
  374. // Purpose: Sets a binary value into the registry. Assures the type is
  375. // correct.
  376. //
  377. // Arguments:
  378. // hkey [in] The registry key.
  379. // pszValueName [in] The name of the value to set.
  380. // pbData [in] Buffer containing binary data to write.
  381. // cbData [in] Size of buffer in bytes.
  382. //
  383. // Returns: S_OK or an HRESULT_FROM_WIN32 error code.
  384. //
  385. // Author: danielwe 16 Apr 1997
  386. //
  387. // Notes:
  388. //
  389. inline HRESULT HrRegSetBinary(HKEY hkey, PCWSTR pszValueName,
  390. const BYTE *pbData, DWORD cbData)
  391. {
  392. return HrRegSetValueEx(hkey, pszValueName, REG_BINARY,
  393. pbData, cbData);
  394. }
  395. //+---------------------------------------------------------------------------
  396. //
  397. // Function: HrRegSetSzAsUlong
  398. //
  399. // Purpose: Writes the given ulong to the given registry value as a
  400. // REG_SZ.
  401. //
  402. // Arguments:
  403. // hkey [in] See Win32 docs.
  404. // pszValueName [in] See Win32 docs.
  405. // ulValue [in] The value to write as a string
  406. // nBase [in] The radix to convert the ulong from
  407. //
  408. // Returns: S_OK or an HRESULT_FROM_WIN32 error code.
  409. //
  410. // Author: billbe 14 Jun 1997
  411. //
  412. // Notes:
  413. //
  414. inline
  415. HRESULT
  416. HrRegSetSzAsUlong (
  417. IN HKEY hkey,
  418. IN PCWSTR pszValueName,
  419. IN ULONG ulValue,
  420. IN int nBase)
  421. {
  422. WCHAR szBuffer[33];
  423. // convert the value to a string using the specified base
  424. _ultow(ulValue, szBuffer, nBase);
  425. return HrRegSetSz(hkey, pszValueName, szBuffer);
  426. }
  427. //
  428. // Data structures
  429. //
  430. const HKEY HKLM_SVCS = (HKEY)(INT_PTR)(int)(0x80000007);
  431. const DWORD REG_MIN = REG_QWORD;
  432. const DWORD REG_CREATE = (REG_MIN + 1);
  433. const DWORD REG_BOOL = (REG_MIN + 2);
  434. const DWORD REG_IP = (REG_MIN + 3);
  435. const DWORD REG_FILE = (REG_MIN + 4);
  436. const DWORD REG_HEX = (REG_MIN + 5);
  437. struct VALUETABLE
  438. {
  439. // Name of value key
  440. PCWSTR pszValueName;
  441. // Data and offset location
  442. DWORD dwType;
  443. INT cbOffset;
  444. // Default values
  445. BYTE* pbDefault;
  446. };
  447. struct REGBATCH
  448. {
  449. // Location of the registry entry
  450. HKEY hkey;
  451. PCWSTR pszSubkey;
  452. PCWSTR pszValueName;
  453. // Data and offset location
  454. DWORD dwType;
  455. INT cbOffset;
  456. // Default values
  457. BYTE* pbDefault;
  458. };
  459. VOID
  460. RegReadValues (
  461. IN INT crvc,
  462. IN const REGBATCH* arb,
  463. OUT const BYTE* pbUserData,
  464. IN REGSAM samDesired);
  465. HRESULT
  466. HrRegWriteValues (
  467. IN INT crvc,
  468. IN const REGBATCH* arb,
  469. IN const BYTE* pbUserData,
  470. IN DWORD dwOptions,
  471. IN REGSAM samDesired);
  472. HRESULT
  473. HrRegWriteValueTable(
  474. IN HKEY hkeyRoot,
  475. IN INT cvt,
  476. IN const VALUETABLE* avt,
  477. IN const BYTE* pbUserData,
  478. IN DWORD dwOptions,
  479. IN REGSAM samDesired);
  480. VOID
  481. RegSafeCloseKey (
  482. IN HKEY hkey);
  483. #endif // _NCREG_H_