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.

411 lines
14 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Copyright (C) 1992, Microsoft Corporation.
  4. //
  5. // File: registry.cxx
  6. //
  7. // Contents: implementations for CRegKey member Members
  8. //
  9. // Members: CRegKey::CRegKey - constructor for registry key object
  10. // CRegKey::CRegKey - constructor for registry key object
  11. // CRegKey::CreateKey - real worker for constructors
  12. // CRegKey::~CRegKey - destructor for registry key object
  13. // CRegKey::Delete - delete a registry key
  14. // CRegKey::EnumValues - enumerate values of a registry key
  15. // CRegKey::EnumKeys - enumerate subkeys of a registry key
  16. // CRegKey::NotifyChange - setup change notification for a key
  17. //
  18. // CRegValue::GetValue - sets a registry value
  19. // CRegValue::SetValue - retrieves a registry value
  20. // CRegValue::Delete - deletes a registry value
  21. // CRegValue::GetTypeCode - returns the type code of the value
  22. //
  23. // CRegMSZ::SetStrings - sets a multi-string registry value
  24. // CRegMSZ::GetStrings - retrieves a multi-string registry value
  25. //
  26. // History: 09/30/92 Rickhi Created
  27. // 09/22/93 AlokS Took out exception throwing code
  28. // and added proper return code for
  29. // each method.
  30. //
  31. // 07/26/94 AlokS Made it real light weight for simple
  32. // registry set/get operations
  33. //
  34. // Notes: see notes in registry.hxx
  35. //
  36. //----------------------------------------------------------------------------
  37. #include <stdlib.h>
  38. #include <windows.h>
  39. #include <registry.hxx>
  40. //+-------------------------------------------------------------------------
  41. //
  42. // Member: CRegKey::CRegKey
  43. //
  44. // Synopsis: Constructor for registry key object, using HKEY for parent
  45. //
  46. // Arguments: [hkParent] - handle to parent key
  47. // [pwszPath] - pathname to key
  48. // [samDesiredAccess] - desired access rights to the key
  49. // [pwszClass] - class for the key
  50. // [dwOptions] - options for the key eg volatile or not
  51. // [pdwDisposition] - to find out if key was opened or created
  52. // [pSecurityAttributes] - used only if the key is created
  53. // [fThrowExceptionOnError] - Constructor throw exception on error
  54. //
  55. // Signals: Internal error state is set if construction fails.
  56. //
  57. // Returns: -none-
  58. //
  59. // History: 09/30/92 Rickhi Created
  60. //
  61. // Notes: All except the hkParent and pwszPath are optional parameters.
  62. //
  63. //--------------------------------------------------------------------------
  64. CRegKey::CRegKey (
  65. HKEY hkParent,
  66. const LPWSTR pwszPath,
  67. REGSAM samDesiredAccess,
  68. const LPWSTR pwszClass,
  69. DWORD dwOptions,
  70. DWORD *pdwDisposition,
  71. const LPSECURITY_ATTRIBUTES pSecurityAttributes )
  72. :_hkParent(hkParent),
  73. _hkThis(NULL),
  74. _dwErr (ERROR_SUCCESS)
  75. {
  76. _dwErr = CreateKey( _hkParent,
  77. pwszPath,
  78. samDesiredAccess,
  79. pwszClass,
  80. dwOptions,
  81. pdwDisposition,
  82. pSecurityAttributes );
  83. }
  84. //+-------------------------------------------------------------------------
  85. //
  86. // Member: CRegKey::CRegKey
  87. //
  88. // Synopsis: Constructor for registry key object, using CRegKey for parent
  89. //
  90. // Arguments: [prkParent] - ptr to Parent CRegKey
  91. // [pwszPath] - pathname to key
  92. // [samDesiredAccess] - desired access rights to the key
  93. // [pwszClass] - class for the key
  94. // [dwOptions] - options for the key eg volatile or not
  95. // [pdwDisposition] - to find out if key was opened or created
  96. // [pSecurityAttributes] - used only if the key is created
  97. // [fThrowExceptionOnError] - Constructor throw exception on error
  98. //
  99. // Signals: Internal Error state is set if error occures during construction.
  100. //
  101. // Returns: nothing
  102. //
  103. // History: 09/30/92 Rickhi Created
  104. //
  105. // Notes: All except the prkParent and pwszPath are optional parameters.
  106. //
  107. //--------------------------------------------------------------------------
  108. CRegKey::CRegKey (
  109. const CRegKey &crkParent,
  110. const LPWSTR pwszPath,
  111. REGSAM samDesiredAccess,
  112. const LPWSTR pwszClass,
  113. DWORD dwOptions,
  114. DWORD *pdwDisposition,
  115. const LPSECURITY_ATTRIBUTES pSecurityAttributes )
  116. :_hkParent(crkParent.GetHandle()),
  117. _hkThis(NULL),
  118. _dwErr(ERROR_SUCCESS)
  119. {
  120. _dwErr = CreateKey ( _hkParent,
  121. pwszPath,
  122. samDesiredAccess,
  123. pwszClass,
  124. dwOptions,
  125. pdwDisposition,
  126. pSecurityAttributes );
  127. }
  128. //+-------------------------------------------------------------------------
  129. //
  130. // Member: CRegKey::CRegKey
  131. //
  132. // Synopsis: Constructor for registry key object, using HKEY for parent
  133. // Merely opens the key, if exist
  134. //
  135. // Arguments: [hkParent] - HKEY to Parent
  136. // [dwErr] - Error code returned here
  137. // [pwszPath] - pathname to key
  138. // [samDesiredAccess] - desired access rights to the key
  139. //
  140. // Signals: Internal Error state is set if error occures during construction
  141. //
  142. // Returns: nothing
  143. //
  144. // History: 09/22/93 AlokS Created
  145. //
  146. // Notes: Check error status to determine if constructor succeeded
  147. //
  148. //--------------------------------------------------------------------------
  149. CRegKey::CRegKey (
  150. HKEY hkParent,
  151. DWORD *pdwErr,
  152. const LPWSTR pwszPath,
  153. REGSAM samDesiredAccess )
  154. :_hkParent(hkParent),
  155. _hkThis(NULL),
  156. _dwErr(ERROR_SUCCESS)
  157. {
  158. *pdwErr = _dwErr = OpenKey ( _hkParent, pwszPath, samDesiredAccess );
  159. }
  160. //+-------------------------------------------------------------------------
  161. //
  162. // Member: CRegKey::CRegKey
  163. //
  164. // Synopsis: Constructor for registry key object, using CRegKey for parent
  165. // Merely opens the key, if exist
  166. //
  167. // Arguments: [prkParent] - ptr to Parent CRegKey
  168. // [dwErr] - Error code returned here.
  169. // [pwszPath] - pathname to key
  170. // [samDesiredAccess] - desired access rights to the key
  171. //
  172. // Signals: Internal Error state is set if error occures during construction
  173. //
  174. // Returns: nothing
  175. //
  176. // History: 09/22/93 AlokS Created
  177. //
  178. // Notes: Check error status to determine if constructor succeeded
  179. //
  180. //--------------------------------------------------------------------------
  181. CRegKey::CRegKey (
  182. const CRegKey &crkParent,
  183. DWORD *pdwErr,
  184. const LPWSTR pwszPath,
  185. REGSAM samDesiredAccess )
  186. :_hkParent(crkParent.GetHandle()),
  187. _hkThis(NULL),
  188. _dwErr(ERROR_SUCCESS)
  189. {
  190. *pdwErr = _dwErr = OpenKey ( _hkParent, pwszPath, samDesiredAccess );
  191. }
  192. //+-------------------------------------------------------------------------
  193. //
  194. // Member: CRegKey::~CRegKey, public
  195. //
  196. // Synopsis: Destructor for registry key object
  197. //
  198. // Arguments: none
  199. //
  200. // Signals: nothing
  201. //
  202. // Returns: nothing
  203. //
  204. // History: 09/30/92 Rickhi Created
  205. //
  206. // Notes:
  207. //
  208. //--------------------------------------------------------------------------
  209. CRegKey::~CRegKey()
  210. {
  211. if (_hkThis != NULL)
  212. RegCloseKey(_hkThis);
  213. }
  214. //+-------------------------------------------------------------------------
  215. //
  216. // Member: CRegKey::CreateKey, private
  217. //
  218. // Synopsis: This method does the real work of the constructors.
  219. //
  220. // Arguments: [hkParent] - handle to parent key
  221. // [pwszPath] - pathname to key
  222. // [samDesiredAccess] - desired access rights to the key
  223. // [pwszClass] - class for the key
  224. // [dwOptions] - options for the key eg volatile or not
  225. // [pdwDisposition] - to find out if key was opened or created
  226. // [pSecurityAttributes] - used only if the key is created
  227. //
  228. // Signals: -none-
  229. //
  230. // Returns: ERROR_SUCCESS on success. Else error from either Registry APIs
  231. // or from Memory allocation
  232. //
  233. // History: 09/30/92 Rickhi Created
  234. //
  235. // Notes: All parameters are required.
  236. //
  237. //--------------------------------------------------------------------------
  238. DWORD CRegKey::CreateKey (
  239. HKEY hkParent,
  240. const LPWSTR pwszPath,
  241. REGSAM samDesiredAccess,
  242. const LPWSTR pwszClass,
  243. DWORD dwOptions,
  244. DWORD *pdwDisposition,
  245. const LPSECURITY_ATTRIBUTES pSecurityAttributes )
  246. {
  247. DWORD dwDisposition;
  248. DWORD dwRc;
  249. DWORD dwErr = ERROR_SUCCESS;
  250. LPSECURITY_ATTRIBUTES lpsec = pSecurityAttributes;
  251. // create/open the key
  252. if ((dwRc = RegCreateKeyEx(hkParent,
  253. pwszPath, // path to key
  254. 0, // title index
  255. pwszClass, // class of key
  256. dwOptions, // key options
  257. samDesiredAccess, // desired access
  258. lpsec, // if created
  259. &_hkThis, // handle
  260. &dwDisposition) // opened/created
  261. )==ERROR_SUCCESS)
  262. {
  263. // save away the name
  264. _cwszName.Set(pwszPath);
  265. // setup the return parameters
  266. if (pdwDisposition != NULL)
  267. *pdwDisposition = dwDisposition;
  268. }
  269. else
  270. dwErr = Creg_ERROR(dwRc);
  271. return(dwErr);
  272. }
  273. //+-------------------------------------------------------------------------
  274. //
  275. // Member: CRegKey::OpenKey, private
  276. //
  277. // Synopsis: This method does the real work of the constructors.
  278. //
  279. // Arguments: [hkParent] - handle to parent key
  280. // [pwszPath] - pathname to key
  281. // [samDesiredAccess] - desired access rights to the key
  282. //
  283. // Signals: -none-
  284. //
  285. // Returns: ERROR_SUCCESS on success. Else error from either Registry APIs
  286. // or from Memory allocation
  287. //
  288. // History: 09/22/93 AlokS Created
  289. //
  290. // Notes: All parameters are required.
  291. //
  292. //--------------------------------------------------------------------------
  293. DWORD CRegKey::OpenKey (
  294. HKEY hkParent,
  295. const LPWSTR pwszPath,
  296. REGSAM samDesiredAccess )
  297. {
  298. DWORD dwRc;
  299. DWORD dwErr = ERROR_SUCCESS;
  300. // open the key
  301. if ((dwRc = RegOpenKeyEx(hkParent,
  302. pwszPath, // path to key
  303. 0, // reserved
  304. samDesiredAccess, // desired access
  305. &_hkThis // handle
  306. ))==ERROR_SUCCESS)
  307. {
  308. // save away the name
  309. _cwszName.Set(pwszPath);
  310. }
  311. else
  312. dwErr = Creg_ERROR(dwRc);
  313. return(dwErr);
  314. }
  315. //+-------------------------------------------------------------------------
  316. //
  317. // Member: CRegValue::GetValue, public
  318. //
  319. // Purpose: Returns the data associated with a registry value.
  320. //
  321. // Arguements: [pbData] - ptr to buffer supplied by caller.
  322. // [cbData] - size of data buffer supplied.
  323. // [pdwTypeCode] - type of data returned.
  324. //
  325. // Signals:
  326. //
  327. // Returns: ERROR_SUCCESS on success. Else error from either Registry APIs
  328. // or from Memory allocation
  329. //
  330. // History: 09/30/92 Rickhi Created
  331. //
  332. // Notes:
  333. //
  334. //
  335. //
  336. //--------------------------------------------------------------------------
  337. DWORD CRegValue::GetValue(LPBYTE pbData, ULONG* pcbData, DWORD *pdwTypeCode)
  338. {
  339. DWORD dwRc = RegQueryValueEx(GetParentHandle(),
  340. (LPWSTR)_cwszValueID, // value id
  341. NULL, // title index
  342. pdwTypeCode, // type of data returned
  343. pbData, // data
  344. pcbData); // size of data
  345. return(dwRc);
  346. }
  347. //+-------------------------------------------------------------------------
  348. //
  349. // Member: CRegValue::SetValue
  350. //
  351. // Purpose: Writes the data associated with a registry value.
  352. //
  353. // Arguements: [pbData] - ptr to data to write.
  354. // [cbData] - size of data to write.
  355. // [dwTypeCode] - type of data to write.
  356. //
  357. // Signals: -none-
  358. //
  359. // Returns: ERROR_SUCCESS on success. Else error from either Registry APIs
  360. // or from Memory allocation
  361. //
  362. // History: 09/30/92 Rickhi Created
  363. //
  364. // Notes:
  365. //
  366. //--------------------------------------------------------------------------
  367. DWORD CRegValue::SetValue(const LPBYTE pbData, ULONG cbData, DWORD dwTypeCode)
  368. {
  369. DWORD dwRc;
  370. DWORD dwErr = ERROR_SUCCESS;
  371. if ((dwRc = RegSetValueEx(GetParentHandle(), // key handle
  372. (LPWSTR)_cwszValueID, // value id
  373. NULL, // title index
  374. dwTypeCode, // type of info in buffer
  375. pbData, // data
  376. cbData) // size of data
  377. )!= ERROR_SUCCESS)
  378. {
  379. dwErr = Creg_ERROR(dwRc);
  380. }
  381. return(dwErr);
  382. }