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.

513 lines
15 KiB

  1. //+-------------------------------------------------------------------------
  2. // Microsoft OLE
  3. // Copyright (C) Microsoft Corporation, 1993 - 1995.
  4. //
  5. // File: creghelp.cxx
  6. //
  7. // Contents: Implementaion of CRegistryHelp class object.
  8. //
  9. // Classes: CRegistryHelp
  10. //
  11. // Functions: CRegistryHelp::CRegistryHelp
  12. // CRegistryHelp::~CRegistryHelp
  13. // CRegistryHelp::GetValue
  14. // CRegistryHelp::GetValueDword
  15. // CRegistryHelp::GetValueString
  16. // CRegistryHelp::SetValue
  17. // CRegistryHelp::SetValueDword
  18. // CRegistryHelp::SetValueString
  19. // CRegistryHelp::DeleteValue
  20. // CRegistryHelp::DeleteSubKey
  21. //
  22. // History: 03-Sep-93 XimingZ Created
  23. // 23-Nov-94 DeanE Modified for general use
  24. //--------------------------------------------------------------------------
  25. #include <dfheader.hxx>
  26. #pragma hdrstop
  27. //+-------------------------------------------------------------------------
  28. // Method: CRegistryHelp::CRegistryHelp
  29. //
  30. // Synopsis: Constructor.
  31. //
  32. // Arguments: [hKey] - Handle to the root key.
  33. // [pszSubKey] - Name of subkey.
  34. // [fOptions] - Special options.
  35. // [samKey] - Access desired.
  36. // [phr] - Pointer to status code to be returned.
  37. //
  38. // History: 20-Oct-93 XimingZ Created
  39. //--------------------------------------------------------------------------
  40. CRegistryHelp::CRegistryHelp(
  41. HKEY hKey,
  42. LPTSTR pszSubKey,
  43. DWORD fOptions,
  44. REGSAM samKey,
  45. HRESULT *phr) :
  46. _hKey(NULL),
  47. _hSubKey(NULL),
  48. _pszSubKey(NULL),
  49. _fOptions(fOptions)
  50. {
  51. DWORD dwDisposition;
  52. LONG lRes;
  53. // Confirm subkey is valid and save it
  54. if (IsBadReadPtr(pszSubKey, sizeof(LPTSTR)))
  55. {
  56. *phr = E_POINTER;
  57. return;
  58. }
  59. _pszSubKey = new(NullOnFail) TCHAR[lstrlen(pszSubKey)+1];
  60. if (_pszSubKey == NULL)
  61. {
  62. *phr = E_OUTOFMEMORY;
  63. return;
  64. }
  65. lstrcpy(_pszSubKey, pszSubKey);
  66. // Save hKey
  67. if (hKey == NULL)
  68. {
  69. *phr = MAKE_TH_ERROR_CODE(E_HANDLE);
  70. return;
  71. }
  72. _hKey = hKey;
  73. // Open the subkey and save a handle to it
  74. lRes = RegCreateKeyEx(
  75. hKey,
  76. pszSubKey,
  77. 0,
  78. NULL,
  79. fOptions,
  80. samKey,
  81. NULL,
  82. &_hSubKey,
  83. &dwDisposition);
  84. if (lRes != ERROR_SUCCESS)
  85. {
  86. *phr = MAKE_TH_ERROR_CODE(lRes);
  87. delete _pszSubKey;
  88. _hSubKey = NULL;
  89. _pszSubKey = NULL;
  90. }
  91. else
  92. {
  93. *phr = S_OK;
  94. }
  95. }
  96. //+-------------------------------------------------------------------------
  97. // Function: CRegistryHelp::~CRegistryHelp
  98. //
  99. // Synopsis: Destructor.
  100. //
  101. // Arguments: None
  102. //
  103. // History: 20-Oct-93 XimingZ Created
  104. //--------------------------------------------------------------------------
  105. CRegistryHelp::~CRegistryHelp()
  106. {
  107. delete _pszSubKey;
  108. if (_hSubKey != NULL)
  109. {
  110. RegCloseKey(_hSubKey);
  111. }
  112. }
  113. //+-------------------------------------------------------------------------
  114. // Member: CRegistryHelp::GetValue
  115. //
  116. // Synopsis: Retrieves a value for some subkey and value name.
  117. //
  118. // Arguments: [pszSubKey] - Subkey the value is on. NULL means _hSubKey.
  119. // [pszValue] - Name of value to query.
  120. // [pbBuffer] - Holds retrieved data.
  121. // [pcbBuffer] - Holds size of buffer on entry and actual size
  122. // on exit (in bytes).
  123. // [pfType] - Holds type of data retrieved.
  124. //
  125. // Returns: S_OK for success or an error code.
  126. //
  127. // History: 20-Oct-93 XimingZ Created
  128. //--------------------------------------------------------------------------
  129. HRESULT CRegistryHelp::GetValue(
  130. LPTSTR pszSubKey,
  131. LPTSTR pszValue,
  132. LPBYTE pbBuffer,
  133. LPDWORD pcbBuffer,
  134. LPDWORD pfType)
  135. {
  136. HRESULT hr = E_FAIL;
  137. HKEY hSubKey = NULL;
  138. LONG lRes;
  139. // Open the subkey, if necessary
  140. if (NULL == pszSubKey)
  141. {
  142. hSubKey = _hSubKey;
  143. hr = S_OK;
  144. }
  145. else
  146. {
  147. lRes = RegOpenKeyEx(_hSubKey, pszSubKey, NULL, KEY_READ, &hSubKey);
  148. hr = (lRes == ERROR_SUCCESS) ? S_OK : MAKE_TH_ERROR_CODE(lRes);
  149. }
  150. // Query for the data
  151. if (SUCCEEDED(hr))
  152. {
  153. lRes = RegQueryValueEx(
  154. hSubKey,
  155. pszValue,
  156. NULL,
  157. pfType,
  158. pbBuffer,
  159. pcbBuffer);
  160. hr = (lRes == ERROR_SUCCESS) ? S_OK : MAKE_TH_ERROR_CODE(lRes);
  161. }
  162. // Close the subkey handle, if we opened it
  163. if (hSubKey != _hSubKey)
  164. {
  165. RegCloseKey(hSubKey);
  166. }
  167. return(hr);
  168. }
  169. //+-------------------------------------------------------------------------
  170. // Member: CRegistryHelp::GetValueDword
  171. //
  172. // Synopsis: Retrieves a value for some subkey and value name that has
  173. // REG_DWORD, REG_DWORD_LITTLE_ENDIAN, or REG_DWORD_BIG_ENDIAN
  174. // data. Other data types return an error.
  175. //
  176. // Arguments: [pszSubKey] - Subkey the value is on. NULL means
  177. // _hSubKey.
  178. // [pszValue] - Name of value to query.
  179. // [pdwData] - Holds retrieved data.
  180. // [fExpectedType] - Holds expected type.
  181. //
  182. // Returns: S_OK for success or an error code.
  183. //
  184. // History: 20-Oct-93 XimingZ Created
  185. //--------------------------------------------------------------------------
  186. HRESULT CRegistryHelp::GetValueDword(
  187. LPTSTR pszSubKey,
  188. LPTSTR pszValue,
  189. LPDWORD pdwData,
  190. DWORD fExpectedType)
  191. {
  192. HRESULT hr = E_FAIL;
  193. DWORD cbData = sizeof(DWORD);
  194. DWORD fType;
  195. // Check fExpectedType is for a DWORD data type
  196. if ((fExpectedType != REG_DWORD) &&
  197. (fExpectedType != REG_DWORD_LITTLE_ENDIAN) &&
  198. (fExpectedType != REG_DWORD_BIG_ENDIAN))
  199. {
  200. return(MAKE_TH_ERROR_CODE(ERROR_INVALID_PARAMETER));
  201. }
  202. // Get the value
  203. hr = GetValue(pszSubKey, pszValue, (PBYTE)pdwData, &cbData, &fType);
  204. if (SUCCEEDED(hr))
  205. {
  206. if ((fType == fExpectedType) && (cbData == sizeof(DWORD)))
  207. {
  208. hr = S_OK;
  209. }
  210. else
  211. {
  212. hr = E_FAIL;
  213. }
  214. }
  215. return(hr);
  216. }
  217. //+-------------------------------------------------------------------------
  218. // Member: CRegistryHelp::GetValueString
  219. //
  220. // Synopsis: Retrieves a value for some subkey and value name that has
  221. // REG_SZ, REG_EXPAND_SZ, and REG_MULTI_SZ type data.
  222. //
  223. // Arguments: [pszSubKey] - Subkey the value is on. NULL means
  224. // _hSubKey.
  225. // [pszValue] - Name of value to query.
  226. // [pszData] - Holds retrieved data.
  227. // [pcbData] - Size of data buffer in bytes.
  228. // [fExpectedType] - Holds expected type.
  229. //
  230. // Returns: S_OK for success or an error code.
  231. //
  232. // History: 20-Oct-93 XimingZ Created
  233. //--------------------------------------------------------------------------
  234. HRESULT CRegistryHelp::GetValueString(
  235. LPTSTR pszSubKey,
  236. LPTSTR pszValue,
  237. LPTSTR pszData,
  238. LPDWORD pcbData,
  239. DWORD fExpectedType)
  240. {
  241. HRESULT hr = E_FAIL;
  242. DWORD fType;
  243. // Check fExpectedType is for a DWORD data type
  244. if ((fExpectedType != REG_SZ) &&
  245. (fExpectedType != REG_EXPAND_SZ) &&
  246. (fExpectedType != REG_MULTI_SZ))
  247. {
  248. return(MAKE_TH_ERROR_CODE(ERROR_INVALID_PARAMETER));
  249. }
  250. hr = GetValue(pszSubKey, pszValue, (PBYTE)pszData, pcbData, &fType);
  251. if (SUCCEEDED(hr))
  252. {
  253. if (fType == fExpectedType)
  254. {
  255. hr = S_OK;
  256. }
  257. else
  258. {
  259. hr = E_FAIL;
  260. }
  261. }
  262. return(hr);
  263. }
  264. //+-------------------------------------------------------------------------
  265. // Member: CRegistryHelp::SetValue
  266. //
  267. // Synopsis: Stores a value for a given subkey and value name. Subkey
  268. // is expected to exist.
  269. //
  270. // Arguments: [pszSubKey] - Subkey the value is on. NULL means
  271. // _hSubKey.
  272. // [pszValue] - Name of value to set.
  273. // [pbData] - Address of value data.
  274. // [cbData] - Size of data.
  275. // [fType] - Type of data.
  276. //
  277. // Returns: S_OK if value is set properly, error code if not.
  278. //
  279. // History: 20-Oct-93 XimingZ Created
  280. //--------------------------------------------------------------------------
  281. HRESULT CRegistryHelp::SetValue(
  282. LPTSTR pszSubKey,
  283. LPTSTR pszValue,
  284. LPBYTE pbData,
  285. DWORD cbData,
  286. DWORD fType)
  287. {
  288. HRESULT hr = E_FAIL;
  289. LONG lRes;
  290. HKEY hSubKey;
  291. // Open the subkey, if necessary
  292. if (pszSubKey == NULL)
  293. {
  294. hSubKey = _hSubKey;
  295. hr = S_OK;
  296. }
  297. else
  298. {
  299. lRes = RegOpenKeyEx(_hSubKey, pszSubKey, NULL, KEY_WRITE, &hSubKey);
  300. hr = (lRes == ERROR_SUCCESS) ? S_OK : MAKE_TH_ERROR_CODE(lRes);
  301. }
  302. // Set the data
  303. if (SUCCEEDED(hr))
  304. {
  305. lRes = RegSetValueEx(hSubKey, pszValue, 0, fType, pbData, cbData);
  306. hr = (lRes == ERROR_SUCCESS) ? S_OK : MAKE_TH_ERROR_CODE(lRes);
  307. }
  308. // Close the subkey handle, if we opened it
  309. if (hSubKey != _hSubKey)
  310. {
  311. RegCloseKey(hSubKey);
  312. }
  313. return(hr);
  314. }
  315. //+-------------------------------------------------------------------------
  316. // Member: CRegistryHelp::SetValueDword
  317. //
  318. // Synopsis: Stores a DWORD value for some subkey and value name under
  319. // this subkey. Type flag passed must be REG_DWORD,
  320. // REG_DWORD_LITTLE_ENDIAN, or REG_DWORD_BIG_ENDIAN.
  321. //
  322. // Arguments: [pszSubKey] - Subkey the value is on. NULL means _hSubKey.
  323. // [pszValue] - Name of value to set.
  324. // [dwData] - Data to set.
  325. // [fType] - Type of data.
  326. //
  327. // Returns: S_OK if value is set properly, error code if not.
  328. //
  329. // History: 20-Oct-93 XimingZ Created
  330. //--------------------------------------------------------------------------
  331. HRESULT CRegistryHelp::SetValueDword(
  332. LPTSTR pszSubKey,
  333. LPTSTR pszValue,
  334. DWORD dwData,
  335. DWORD fType)
  336. {
  337. HRESULT hr = E_FAIL;
  338. // Check fType is for a DWORD data type
  339. if ((fType != REG_DWORD) &&
  340. (fType != REG_DWORD_LITTLE_ENDIAN) &&
  341. (fType != REG_DWORD_BIG_ENDIAN))
  342. {
  343. return(MAKE_TH_ERROR_CODE(ERROR_INVALID_PARAMETER));
  344. }
  345. // Set the value
  346. hr = SetValue(
  347. pszSubKey,
  348. pszValue,
  349. (LPBYTE)&dwData,
  350. sizeof(DWORD),
  351. fType);
  352. return(hr);
  353. }
  354. //+-------------------------------------------------------------------------
  355. // Member: CRegistryHelp::SetValueString
  356. //
  357. // Synopsis: Stores a string value for some subkey and value name under
  358. // this subkey. Type flag passed must be REG_SZ,
  359. // REG_EXPAND_SZ, or REG_MULTI_SZ.
  360. //
  361. // Arguments: [pszSubKey] - Subkey the value is on. NULL means _hSubKey.
  362. // [pszValue] - Name of value to set.
  363. // [pszData] - Data to set.
  364. // [cbData] - Size of data, in bytes.
  365. // [fType] - Type of data.
  366. //
  367. // Returns: S_OK if value is set properly, error code if not.
  368. //
  369. // History: 20-Oct-93 XimingZ Created
  370. //--------------------------------------------------------------------------
  371. HRESULT CRegistryHelp::SetValueString(
  372. LPTSTR pszSubKey,
  373. LPTSTR pszValue,
  374. LPTSTR pszData,
  375. DWORD cbData,
  376. DWORD fType)
  377. {
  378. HRESULT hr = E_FAIL;
  379. // Check fType is for a DWORD data type
  380. if ((fType != REG_SZ) &&
  381. (fType != REG_EXPAND_SZ) &&
  382. (fType != REG_MULTI_SZ))
  383. {
  384. return(MAKE_TH_ERROR_CODE(ERROR_INVALID_PARAMETER));
  385. }
  386. // Set the value
  387. hr = SetValue(
  388. pszSubKey,
  389. pszValue,
  390. (LPBYTE)pszData,
  391. cbData,
  392. fType);
  393. return(hr);
  394. }
  395. //+-------------------------------------------------------------------------
  396. // Member: CRegistryHelp::DeleteValue
  397. //
  398. // Synopsis: Delete a named value under a given subkey.
  399. //
  400. // Arguments: [pszSubKey] - Subkey the value is on. NULL means _hSubKey.
  401. // [pszValue] - Value to delete.
  402. //
  403. // Returns: S_OK if value deleted or is not there, error code if not
  404. // deleted.
  405. //
  406. // History: 20-Oct-93 XimingZ Created
  407. //--------------------------------------------------------------------------
  408. HRESULT CRegistryHelp::DeleteValue(LPTSTR pszSubKey, LPTSTR pszValue)
  409. {
  410. HRESULT hr = E_FAIL;
  411. HKEY hSubKey;
  412. LONG lRes;
  413. // Open the subkey, if necessary
  414. if (pszSubKey == NULL)
  415. {
  416. hSubKey = _hSubKey;
  417. }
  418. else
  419. {
  420. lRes = RegOpenKeyEx(_hSubKey, pszSubKey, NULL, KEY_WRITE, &hSubKey);
  421. hr = (lRes == ERROR_SUCCESS) ? S_OK : MAKE_TH_ERROR_CODE(lRes);
  422. }
  423. // Delete the value
  424. if (SUCCEEDED(hr))
  425. {
  426. lRes = RegDeleteValue(hSubKey, pszValue);
  427. hr = (lRes == ERROR_SUCCESS) ? S_OK : MAKE_TH_ERROR_CODE(lRes);
  428. }
  429. // Close the subkey handle, if we opened it
  430. if (hSubKey != _hSubKey)
  431. {
  432. RegCloseKey(hSubKey);
  433. }
  434. return(hr);
  435. }
  436. //+-------------------------------------------------------------------------
  437. // Member: CRegistryHelp::DeleteSubKey
  438. //
  439. // Synopsis: Delete the subkey passed under this subkey.
  440. //
  441. // Arguments: [pszSubKey] - Subkey to delete. Must have no child
  442. // subkeys.
  443. //
  444. // Returns: S_OK if value deleted or is not there, error code if not
  445. // deleted.
  446. //
  447. // History: 20-Oct-93 XimingZ Created
  448. //--------------------------------------------------------------------------
  449. HRESULT CRegistryHelp::DeleteSubKey(LPTSTR pszSubKey)
  450. {
  451. LONG lRes;
  452. HRESULT hr;
  453. if (IsBadReadPtr(pszSubKey, sizeof(LPTSTR)))
  454. {
  455. hr = MAKE_TH_ERROR_CODE(ERROR_INVALID_PARAMETER);
  456. }
  457. else
  458. {
  459. lRes = RegDeleteKey(_hSubKey, pszSubKey);
  460. hr = (lRes == ERROR_SUCCESS) ? S_OK : MAKE_TH_ERROR_CODE(lRes);
  461. }
  462. return(hr);
  463. }