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.

1564 lines
34 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. 1998 Seagate Software, Inc. All rights reserved
  4. Module Name:
  5. wsbregty.cpp
  6. Abstract:
  7. This is the implementation of registry access helper functions
  8. and is a part of RsCommon.dll.
  9. Author:
  10. Rohde Wakefield [rohde] 05-Nov-1996
  11. Revision History:
  12. --*/
  13. #include "stdafx.h"
  14. HRESULT
  15. WsbOpenRegistryKey (
  16. IN const OLECHAR * szMachine OPTIONAL,
  17. IN const OLECHAR * szPath,
  18. IN REGSAM sam,
  19. OUT HKEY * phKeyMachine,
  20. OUT HKEY * phKey
  21. )
  22. /*++
  23. Routine Description:
  24. Given a machine name and path, connect to obtain an HKEY
  25. that can be used to do registry work.
  26. Arguments:
  27. szMachine - Name of computer to connect to.
  28. szPath - Path inside registry to connect to.
  29. sam - permission desired to registry key.
  30. phKeyMachine - return of HKEY to machine.
  31. phKey - return of HKEY to path.
  32. Return Value:
  33. S_OK - Connection made, Success.
  34. CO_E_OBJNOTCONNECTED - could not connect to registry or key.
  35. E_POINTER - invalid pointer in parameters.
  36. --*/
  37. {
  38. WsbTraceIn ( L"WsbOpenRegistryKey",
  39. L"szMachine = '%ls', szPath = '%ls', sam = 0x%p, phKeyMachine = 0x%p, phKey = 0x%p",
  40. szMachine, szPath, sam, phKeyMachine, phKey );
  41. HRESULT hr = S_OK;
  42. try {
  43. //
  44. // Ensure parameters are valid
  45. //
  46. WsbAssert ( 0 != szPath, E_POINTER );
  47. WsbAssert ( 0 != phKey, E_POINTER );
  48. WsbAssert ( 0 != phKeyMachine, E_POINTER );
  49. *phKey = *phKeyMachine = 0;
  50. WsbAffirmWin32 ( RegConnectRegistry ( (WCHAR*) szMachine, HKEY_LOCAL_MACHINE, phKeyMachine ) );
  51. WsbAffirmWin32 ( RegOpenKeyEx ( *phKeyMachine, szPath, 0, sam, phKey ) );
  52. } WsbCatchAndDo ( hr,
  53. //
  54. // Clean up from error
  55. //
  56. if ( phKeyMachine && *phKeyMachine ) {
  57. RegCloseKey ( *phKeyMachine );
  58. *phKeyMachine = 0;
  59. }
  60. ) // WsbCatchAndDo
  61. WsbTraceOut ( L"WsbOpenRegistryKey",
  62. L"HRESULT = %ls, *phKeyMachine = %ls, *phKey = %ls",
  63. WsbHrAsString ( hr ),
  64. WsbStringCopy ( WsbPtrToPtrAsString ( (void**)phKeyMachine ) ),
  65. WsbStringCopy ( WsbPtrToPtrAsString ( (void**)phKey ) ) );
  66. return ( hr );
  67. }
  68. HRESULT
  69. WsbCloseRegistryKey (
  70. IN OUT HKEY * phKeyMachine,
  71. IN OUT HKEY * phKey
  72. )
  73. /*++
  74. Routine Description:
  75. As a companion to WsbOpenRegistryKey, close the given keys and zero
  76. their results.
  77. Arguments:
  78. phKeyMachine - HKEY to machine.
  79. phKey - HKEY to path.
  80. Return Value:
  81. S_OK - Success.
  82. E_POINTER - Invalid pointer passed in.
  83. --*/
  84. {
  85. WsbTraceIn ( L"WsbCloseRegistryKey",
  86. L"phKeyMachine = 0x%p, phKey = 0x%p", phKeyMachine, phKey );
  87. HRESULT hr = S_OK;
  88. try {
  89. //
  90. // Ensure parameters are valid
  91. //
  92. WsbAssert ( 0 != phKey, E_POINTER );
  93. WsbAssert ( 0 != phKeyMachine, E_POINTER );
  94. //
  95. // Clean up the keys
  96. //
  97. if ( *phKey ) {
  98. RegCloseKey ( *phKey );
  99. *phKey = 0;
  100. }
  101. if ( *phKeyMachine ) {
  102. RegCloseKey ( *phKeyMachine );
  103. *phKeyMachine = 0;
  104. }
  105. } WsbCatch ( hr )
  106. WsbTraceOut ( L"WsbCloseRegistryKey",
  107. L"HRESULT = %ls", WsbHrAsString ( hr ) );
  108. return ( hr );
  109. }
  110. HRESULT
  111. WsbRemoveRegistryKey (
  112. IN const OLECHAR * szMachine OPTIONAL,
  113. IN const OLECHAR * szPath,
  114. IN const OLECHAR * szKey
  115. )
  116. /*++
  117. Routine Description:
  118. This routine removes the value of a key as specified.
  119. Arguments:
  120. szMachine - Name of computer to connect to.
  121. szPath - Path inside registry to connect to.
  122. Return Value:
  123. S_OK - Connection made, Success.
  124. CO_E_OBJNOTCONNECTED - could not connect to registry or key.
  125. E_FAIL - Failure occured setting the value.
  126. E_POINTER - invalid pointer passed in as parameter.
  127. --*/
  128. {
  129. WsbTraceIn ( L"WsbRemoveRegistryKey",
  130. L"szMachine = '%ls', szPath = '%ls', szKey = '%ls'",
  131. szMachine, szPath, szKey );
  132. HKEY hKeyMachine = 0,
  133. hKey = 0;
  134. HRESULT hr = S_OK;
  135. try {
  136. //
  137. // Ensure parameters are valid
  138. //
  139. WsbAssert ( 0 != szPath, E_POINTER );
  140. //
  141. // Open and delete the key
  142. //
  143. WsbAffirmHr ( WsbOpenRegistryKey ( szMachine, szPath, KEY_SET_VALUE | DELETE, &hKeyMachine, &hKey ) );
  144. WsbAffirmWin32 ( RegDeleteKey ( hKey, szKey ) );
  145. } WsbCatch ( hr )
  146. WsbCloseRegistryKey ( &hKeyMachine, &hKey );
  147. WsbTraceOut ( L"WsbRemoveRegistryKey",
  148. L"HRESULT = %ls", WsbHrAsString ( hr ) );
  149. return ( hr );
  150. }
  151. HRESULT
  152. WsbRemoveRegistryValue (
  153. IN const OLECHAR * szMachine OPTIONAL,
  154. IN const OLECHAR * szPath,
  155. IN const OLECHAR * szValue
  156. )
  157. /*++
  158. Routine Description:
  159. This routine removes the value of a key as specified.
  160. Arguments:
  161. szMachine - Name of computer to connect to.
  162. szPath - Path inside registry to connect to.
  163. szValue - Name of the value to remove.
  164. Return Value:
  165. S_OK - Connection made, Success.
  166. CO_E_OBJNOTCONNECTED - could not connect to registry or key.
  167. E_FAIL - Failure occured setting the value.
  168. E_POINTER - invalid pointer passed in as parameter.
  169. --*/
  170. {
  171. WsbTraceIn ( L"WsbRemoveRegistryValue",
  172. L"szMachine = '%ls', szPath = '%ls', szValue = '%ls'",
  173. szMachine, szPath, szValue );
  174. HKEY hKeyMachine = 0,
  175. hKey = 0;
  176. HRESULT hr = S_OK;
  177. try {
  178. //
  179. // Ensure parameters are valid
  180. //
  181. WsbAssert ( 0 != szPath, E_POINTER );
  182. WsbAssert ( 0 != szValue, E_POINTER );
  183. //
  184. // Open and write the value in the key
  185. //
  186. WsbAffirmHr ( WsbOpenRegistryKey ( szMachine, szPath, KEY_SET_VALUE, &hKeyMachine, &hKey ) );
  187. WsbAffirmWin32 ( RegDeleteValue ( hKey, szValue ) );
  188. } WsbCatch ( hr )
  189. WsbCloseRegistryKey ( &hKeyMachine, &hKey );
  190. WsbTraceOut ( L"WsbRemoveRegistryValue",
  191. L"HRESULT = %ls", WsbHrAsString ( hr ) );
  192. return ( hr );
  193. }
  194. HRESULT
  195. WsbSetRegistryValueData (
  196. IN const OLECHAR * szMachine OPTIONAL,
  197. IN const OLECHAR * szPath,
  198. IN const OLECHAR * szValue,
  199. IN const BYTE *pData,
  200. IN DWORD cbData
  201. )
  202. /*++
  203. Routine Description:
  204. This routine set the value of a key as specified to the data
  205. given. Type of the value is REG_BINARY.
  206. Arguments:
  207. szMachine - Name of computer to connect to.
  208. szPath - Path inside registry to connect to.
  209. szValue - Name of the value to set.
  210. pData - Pointer to the data buffer to copy into value.
  211. cbData - Number of bytes to copy from pData.
  212. Return Value:
  213. S_OK - Connection made, Success.
  214. CO_E_OBJNOTCONNECTED - could not connect to registry or key.
  215. E_FAIL - Failure occured setting the value.
  216. E_POINTER - invalid pointer passed in as parameter.
  217. --*/
  218. {
  219. WsbTraceIn ( L"WsbSetRegistryValueData",
  220. L"szMachine = '%ls', szPath = '%ls', szValue = '%ls', pData = 0x%p, cbData = %ld",
  221. szMachine, szPath, szValue, pData, cbData );
  222. HKEY hKeyMachine = 0,
  223. hKey = 0;
  224. HRESULT hr = S_OK;
  225. try {
  226. //
  227. // Ensure parameters are valid
  228. //
  229. WsbAssert ( 0 != szPath, E_POINTER );
  230. WsbAssert ( 0 != szValue, E_POINTER );
  231. //
  232. // Open and write the value in the key
  233. //
  234. WsbAffirmHr ( WsbOpenRegistryKey ( szMachine, szPath, KEY_SET_VALUE, &hKeyMachine, &hKey ) );
  235. WsbAffirmWin32 ( RegSetValueEx ( hKey, szValue, 0, REG_BINARY, pData, cbData ) );
  236. } WsbCatch ( hr )
  237. WsbCloseRegistryKey ( &hKeyMachine, &hKey );
  238. WsbTraceOut ( L"WsbSetRegistryValueData",
  239. L"HRESULT = %ls", WsbHrAsString ( hr ) );
  240. return ( hr );
  241. }
  242. HRESULT
  243. WsbGetRegistryValueData (
  244. IN const OLECHAR * szMachine OPTIONAL,
  245. IN const OLECHAR * szPath,
  246. IN const OLECHAR * szValue,
  247. OUT BYTE *pData,
  248. IN DWORD cbData,
  249. OUT DWORD * pcbData OPTIONAL
  250. )
  251. /*++
  252. Routine Description:
  253. This routine retrieves the value of a key as specified. Type of
  254. the value must be REG_BINARY.
  255. Arguments:
  256. szMachine - Name of computer to connect to.
  257. szPath - Path inside registry to connect to.
  258. szValue - Name of the value to get.
  259. pData - Pointer to the data buffer to copy into value.
  260. cbData - Size in bytes of pData.
  261. pcbData - number of bytes filled in pData.
  262. Return Value:
  263. S_OK - Connection made, Success.
  264. CO_E_OBJNOTCONNECTED - could not connect to registry or key.
  265. E_POINTER - invalid pointer in parameters.
  266. E_FAIL - Failure occured getting the value.
  267. --*/
  268. {
  269. WsbTraceIn ( L"WsbGetRegistryValueData",
  270. L"szMachine = '%ls', szPath = '%ls', szValue = '%ls', pData = 0x%p, cbData = %ld, pcbData = 0x%p",
  271. szMachine, szPath, szValue, pData, cbData, pcbData );
  272. HKEY hKeyMachine = 0,
  273. hKey = 0;
  274. HRESULT hr = S_OK;
  275. try {
  276. //
  277. // Ensure parameters are valid
  278. //
  279. WsbAssert ( 0 != szPath, E_POINTER );
  280. WsbAssert ( 0 != szValue, E_POINTER );
  281. WsbAssert ( 0 != pData, E_POINTER );
  282. //
  283. // Open the key
  284. //
  285. WsbAffirmHr ( WsbOpenRegistryKey ( szMachine, szPath, KEY_QUERY_VALUE, &hKeyMachine, &hKey ) );
  286. //
  287. // Set up temporary vars in case NULL passed for pcbData
  288. //
  289. DWORD dwType, cbData2;
  290. if ( !pcbData ) {
  291. pcbData = &cbData2;
  292. }
  293. //
  294. // Query for the REG_BINARY value
  295. //
  296. *pcbData = cbData;
  297. WsbAffirmWin32 ( RegQueryValueEx ( hKey, szValue, 0, &dwType, pData, pcbData ) );
  298. WsbAffirm ( REG_BINARY == dwType, E_FAIL );
  299. } WsbCatch ( hr )
  300. WsbCloseRegistryKey ( &hKeyMachine, &hKey );
  301. WsbTraceOut ( L"WsbGetRegistryValueData",
  302. L"HRESULT = %ls, *pcbData = %ls", WsbHrAsString ( hr ), WsbPtrToUlongAsString ( pcbData ) );
  303. return ( hr );
  304. }
  305. HRESULT
  306. WsbSetRegistryValueString (
  307. IN const OLECHAR * szMachine OPTIONAL,
  308. IN const OLECHAR * szPath,
  309. IN const OLECHAR * szValue,
  310. IN const OLECHAR * szString,
  311. IN DWORD dwType
  312. )
  313. /*++
  314. Routine Description:
  315. This routine set the value of a key as specified to the data
  316. given. Type of the value is dwType (defaults to REG_SZ)
  317. Arguments:
  318. szMachine - Name of computer to connect to.
  319. szPath - Path inside registry to connect to.
  320. szValue - Name of the value to set.
  321. szString - The string to place in the value.
  322. Return Value:
  323. S_OK - Connection made, Success.
  324. CO_E_OBJNOTCONNECTED - could not connect to registry or key.
  325. E_POINTER - invalid pointer in parameters.
  326. E_FAIL - Failure occured setting the value.
  327. --*/
  328. {
  329. WsbTraceIn ( L"WsbSetRegistryValueString",
  330. L"szMachine = '%ls', szPath = '%ls', szValue = '%ls', szString = '%ls'",
  331. szMachine, szPath, szValue, szString );
  332. HKEY hKeyMachine = 0,
  333. hKey = 0;
  334. HRESULT hr = S_OK;
  335. try {
  336. //
  337. // Ensure parameters are valid
  338. //
  339. WsbAssert ( 0 != szPath, E_POINTER );
  340. WsbAssert ( 0 != szValue, E_POINTER );
  341. WsbAssert ( 0 != szString, E_POINTER );
  342. //
  343. // Open the key
  344. //
  345. WsbAffirmHr ( WsbOpenRegistryKey ( szMachine, szPath, KEY_SET_VALUE, &hKeyMachine, &hKey ) );
  346. WsbAffirmWin32 ( RegSetValueEx ( hKey, szValue, 0, dwType, (BYTE*)szString, ( wcslen ( szString ) + 1 ) * sizeof ( OLECHAR ) ) );
  347. } WsbCatch ( hr )
  348. WsbCloseRegistryKey ( &hKeyMachine, &hKey );
  349. WsbTraceOut ( L"WsbSetRegistryValueString",
  350. L"HRESULT = %ls", WsbHrAsString ( hr ) );
  351. return ( hr );
  352. }
  353. HRESULT
  354. WsbGetRegistryValueString (
  355. IN const OLECHAR * szMachine OPTIONAL,
  356. IN const OLECHAR * szPath,
  357. IN const OLECHAR * szValue,
  358. OUT OLECHAR * szString,
  359. IN DWORD cSize,
  360. OUT DWORD *pcLength OPTIONAL
  361. )
  362. /*++
  363. Routine Description:
  364. This routine get the value specified
  365. Type of the value must be REG_SZ or REG_EXPAND_SZ
  366. Arguments:
  367. szMachine - Name of computer to connect to.
  368. szPath - Path inside registry to connect to.
  369. szValue - Name of the value to get.
  370. szString - The string buffer to fill with the value.
  371. cSize - Size of szString in OLECAHR's.
  372. pcLength - Number of OLECHAR actually written (without L'\0').
  373. Return Value:
  374. S_OK - Connection made, Success.
  375. CO_E_OBJNOTCONNECTED - could not connect to registry or key.
  376. E_POINTER - invalid pointer in parameters.
  377. E_FAIL - Failure occured setting the value.
  378. --*/
  379. {
  380. WsbTraceIn ( L"WsbGetRegistryValueString",
  381. L"szMachine = '%ls', szPath = '%ls', szValue = '%ls', szString = 0x%p, cSize = '%ld', pcLength = 0x%p",
  382. szMachine, szPath, szValue, szString, cSize, pcLength );
  383. HKEY hKeyMachine = 0,
  384. hKey = 0;
  385. HRESULT hr = S_OK;
  386. try {
  387. //
  388. // Ensure parameters are valid
  389. //
  390. WsbAssert ( 0 != szPath, E_POINTER );
  391. WsbAssert ( 0 != szValue, E_POINTER );
  392. WsbAssert ( 0 != szString, E_POINTER );
  393. //
  394. // Open the key
  395. //
  396. WsbAffirmHr ( WsbOpenRegistryKey ( szMachine, szPath, KEY_QUERY_VALUE, &hKeyMachine, &hKey ) );
  397. //
  398. // Temporary size vars in case pcLength is NULL
  399. //
  400. DWORD dwType, cbData2;
  401. if ( !pcLength ) {
  402. pcLength = &cbData2;
  403. }
  404. //
  405. // And do the query
  406. //
  407. *pcLength = cSize * sizeof ( OLECHAR );
  408. WsbAffirmWin32 ( RegQueryValueEx ( hKey, szValue, 0, &dwType, (BYTE*)szString, pcLength ) ) ;
  409. WsbAffirm ( (REG_SZ == dwType) || (REG_EXPAND_SZ == dwType), E_FAIL );
  410. //
  411. // return characters, not bytes
  412. //
  413. *pcLength = ( *pcLength / sizeof ( OLECHAR ) ) - 1;
  414. } WsbCatch ( hr )
  415. WsbCloseRegistryKey ( &hKeyMachine, &hKey );
  416. WsbTraceOut ( L"WsbGetRegistryValueString",
  417. L"HRESULT = %ls, szString = '%ls', *pcbLength = %ls",
  418. WsbHrAsString ( hr ), szString, WsbPtrToUlongAsString ( pcLength ) );
  419. return ( hr );
  420. }
  421. HRESULT
  422. WsbGetRegistryValueMultiString (
  423. IN const OLECHAR * szMachine OPTIONAL,
  424. IN const OLECHAR * szPath,
  425. IN const OLECHAR * szValue,
  426. OUT OLECHAR * szMultiString,
  427. IN DWORD cSize,
  428. OUT DWORD *pcLength OPTIONAL
  429. )
  430. /*++
  431. Routine Description:
  432. This routine get the value specified
  433. Type of the value must be REG_MULTI_SZ
  434. Arguments:
  435. szMachine - Name of computer to connect to.
  436. szPath - Path inside registry to connect to.
  437. szValue - Name of the value to get.
  438. szMultiString - The string buffer to fill with the value.
  439. cSize - Size of szString in OLECAHR's.
  440. pcLength - Number of OLECHAR actually written (without L'\0').
  441. Return Value:
  442. S_OK - Connection made, Success.
  443. CO_E_OBJNOTCONNECTED - could not connect to registry or key.
  444. E_POINTER - invalid pointer in parameters.
  445. E_FAIL - Failure occured setting the value.
  446. --*/
  447. {
  448. WsbTraceIn ( L"WsbGetRegistryValueMultiString",
  449. L"szMachine = '%ls', szPath = '%ls', szValue = '%ls', szMultiString = 0x%p, cSize = '%ld', pcLength = 0x%p",
  450. szMachine, szPath, szValue, szMultiString, cSize, pcLength );
  451. HKEY hKeyMachine = 0,
  452. hKey = 0;
  453. HRESULT hr = S_OK;
  454. try {
  455. //
  456. // Ensure parameters are valid
  457. //
  458. WsbAssert ( 0 != szPath, E_POINTER );
  459. WsbAssert ( 0 != szValue, E_POINTER );
  460. WsbAssert ( 0 != szMultiString, E_POINTER );
  461. //
  462. // Open the key
  463. //
  464. WsbAffirmHr ( WsbOpenRegistryKey ( szMachine, szPath, KEY_QUERY_VALUE, &hKeyMachine, &hKey ) );
  465. //
  466. // Temporary size vars in case pcLength is NULL
  467. //
  468. DWORD dwType, cbData2;
  469. if ( !pcLength ) {
  470. pcLength = &cbData2;
  471. }
  472. //
  473. // And do the query
  474. //
  475. *pcLength = cSize * sizeof ( OLECHAR );
  476. WsbAffirmWin32 ( RegQueryValueEx ( hKey, szValue, 0, &dwType, (BYTE*)szMultiString, pcLength ) ) ;
  477. WsbAffirm ( REG_MULTI_SZ == dwType, E_FAIL );
  478. //
  479. // return characters, not bytes
  480. //
  481. *pcLength = ( *pcLength / sizeof ( OLECHAR ) ) - 1;
  482. } WsbCatch ( hr )
  483. WsbCloseRegistryKey ( &hKeyMachine, &hKey );
  484. WsbTraceOut ( L"WsbGetRegistryValueMultiString",
  485. L"HRESULT = %ls, *pcbLength = %ls",
  486. WsbHrAsString ( hr ), WsbPtrToUlongAsString ( pcLength ) );
  487. return ( hr );
  488. }
  489. HRESULT
  490. WsbSetRegistryValueDWORD (
  491. IN const OLECHAR * szMachine OPTIONAL,
  492. IN const OLECHAR * szPath,
  493. IN const OLECHAR * szValue,
  494. IN DWORD dw
  495. )
  496. /*++
  497. Routine Description:
  498. This routine set the value of a key as specified to the data
  499. given. Type of the value is REG_DWORD
  500. Arguments:
  501. szMachine - Name of computer to connect to.
  502. szPath - Path inside registry to connect to.
  503. szValue - Name of the value to set.
  504. dw - DWORD value to store.
  505. Return Value:
  506. S_OK - Connection made, Success.
  507. CO_E_OBJNOTCONNECTED - could not connect to registry or key.
  508. E_POINTER - invalid pointer in parameters.
  509. E_FAIL - Failure occured setting the value.
  510. --*/
  511. {
  512. WsbTraceIn ( L"WsbSetRegistryValueDWORD",
  513. L"szMachine = '%ls', szPath = '%ls', szValue = '%ls', dw = %lu [0x%p]",
  514. szMachine, szPath, szValue, dw, dw );
  515. HKEY hKeyMachine = 0,
  516. hKey = 0;
  517. HRESULT hr = S_OK;
  518. try {
  519. //
  520. // Ensure parameters are valid
  521. //
  522. WsbAssertPointer( szPath );
  523. WsbAssertPointer( szValue );
  524. //
  525. // Open the key
  526. //
  527. WsbAffirmHr ( WsbOpenRegistryKey ( szMachine, szPath, KEY_SET_VALUE, &hKeyMachine, &hKey ) );
  528. WsbAffirmWin32 ( RegSetValueEx ( hKey, szValue, 0, REG_DWORD, (BYTE*)&dw, sizeof( DWORD ) ) );
  529. } WsbCatch ( hr )
  530. WsbCloseRegistryKey ( &hKeyMachine, &hKey );
  531. WsbTraceOut ( L"WsbSetRegistryValueDWORD",
  532. L"HRESULT = %ls", WsbHrAsString ( hr ) );
  533. return ( hr );
  534. }
  535. HRESULT
  536. WsbGetRegistryValueDWORD(
  537. IN const OLECHAR * szMachine OPTIONAL,
  538. IN const OLECHAR * szPath,
  539. IN const OLECHAR * szValue,
  540. OUT DWORD * pdw
  541. )
  542. /*++
  543. Routine Description:
  544. This routine set the value of a key as specified to the data
  545. given. Type of the value is REG_SZ or REG_EXPAND_SZ
  546. Arguments:
  547. szMachine - Name of computer to connect to.
  548. szPath - Path inside registry to connect to.
  549. szValue - Name of the value to get.
  550. pdw - pointer to a DWORD to store value in.
  551. Return Value:
  552. S_OK - Connection made, Success.
  553. CO_E_OBJNOTCONNECTED - could not connect to registry or key.
  554. E_POINTER - invalid pointer in parameters.
  555. E_FAIL - Failure occured setting the value.
  556. --*/
  557. {
  558. WsbTraceIn ( L"WsbGetRegistryValueDWORD",
  559. L"szMachine = '%ls', szPath = '%ls', szValue = '%ls', pdw = 0x%p",
  560. szMachine, szPath, szValue, pdw );
  561. HKEY hKeyMachine = 0,
  562. hKey = 0;
  563. HRESULT hr = S_OK;
  564. try {
  565. //
  566. // Ensure parameters are valid
  567. //
  568. WsbAssertPointer( szPath );
  569. WsbAssertPointer( szValue );
  570. WsbAssertPointer( pdw );
  571. //
  572. // Open the key
  573. //
  574. WsbAffirmHr ( WsbOpenRegistryKey ( szMachine, szPath, KEY_QUERY_VALUE, &hKeyMachine, &hKey ) );
  575. //
  576. // And do the query
  577. //
  578. DWORD dwType, cbData = sizeof( DWORD );
  579. WsbAffirmWin32 ( RegQueryValueEx ( hKey, szValue, 0, &dwType, (BYTE*)pdw, &cbData ) ) ;
  580. WsbAffirm( REG_DWORD == dwType, E_FAIL );
  581. } WsbCatch ( hr )
  582. WsbCloseRegistryKey ( &hKeyMachine, &hKey );
  583. WsbTraceOut ( L"WsbGetRegistryValueDWORD",
  584. L"HRESULT = %ls, *pdw = %ls",
  585. WsbHrAsString ( hr ), WsbPtrToUlongAsString ( pdw ) );
  586. return ( hr );
  587. }
  588. HRESULT
  589. WsbAddRegistryValueDWORD (
  590. IN const OLECHAR * szMachine OPTIONAL,
  591. IN const OLECHAR * szPath,
  592. IN const OLECHAR * szValue,
  593. IN DWORD adw
  594. )
  595. /*++
  596. Routine Description:
  597. This routine adds an amount to a registry value.
  598. Type of the value must be REG_DWORD
  599. Arguments:
  600. szMachine - Name of computer to connect to.
  601. szPath - Path inside registry to connect to.
  602. szValue - Name of the value to increment
  603. adw - DWORD value to add.
  604. Return Value:
  605. S_OK - Connection made, Success.
  606. CO_E_OBJNOTCONNECTED - could not connect to registry or key.
  607. E_POINTER - invalid pointer in parameters.
  608. E_FAIL - Failure occured setting the value.
  609. --*/
  610. {
  611. WsbTraceIn ( L"WsbAddRegistryValueDWORD",
  612. L"szMachine = '%ls', szPath = '%ls', szValue = '%ls', adw = %lu",
  613. szMachine, szPath, szValue, adw);
  614. HRESULT hr = S_OK;
  615. DWORD value = 0;
  616. // Get the old value
  617. hr = WsbGetRegistryValueDWORD(szMachine, szPath, szValue, &value);
  618. // Add to value and replace
  619. if (S_OK == hr) {
  620. value += adw;
  621. } else {
  622. value = adw;
  623. }
  624. hr = WsbSetRegistryValueDWORD(szMachine, szPath, szValue, value);
  625. WsbTraceOut ( L"WsbAddRegistryValueDWORD",
  626. L"HRESULT = %ls", WsbHrAsString ( hr ) );
  627. return ( hr );
  628. }
  629. HRESULT
  630. WsbIncRegistryValueDWORD (
  631. IN const OLECHAR * szMachine OPTIONAL,
  632. IN const OLECHAR * szPath,
  633. IN const OLECHAR * szValue
  634. )
  635. /*++
  636. Routine Description:
  637. This routine increments a registry value by one.
  638. Type of the value must be REG_DWORD
  639. Arguments:
  640. szMachine - Name of computer to connect to.
  641. szPath - Path inside registry to connect to.
  642. szValue - Name of the value to increment
  643. Return Value:
  644. S_OK - Connection made, Success.
  645. CO_E_OBJNOTCONNECTED - could not connect to registry or key.
  646. E_POINTER - invalid pointer in parameters.
  647. E_FAIL - Failure occured setting the value.
  648. --*/
  649. {
  650. WsbTraceIn ( L"WsbIncRegistryValueDWORD",
  651. L"szMachine = '%ls', szPath = '%ls', szValue = '%ls'",
  652. szMachine, szPath, szValue);
  653. HRESULT hr = S_OK;
  654. hr = WsbAddRegistryValueDWORD(szMachine, szPath, szValue, 1);
  655. WsbTraceOut ( L"WsbIncRegistryValueDWORD",
  656. L"HRESULT = %ls", WsbHrAsString ( hr ) );
  657. return ( hr );
  658. }
  659. HRESULT
  660. WsbCheckIfRegistryKeyExists(
  661. IN const OLECHAR * szMachine OPTIONAL,
  662. IN const OLECHAR * szPath
  663. )
  664. /*++
  665. Routine Description:
  666. This routine check if the supplied key exists
  667. If the key already exists, S_OK is returned.
  668. If it needed to be created, S_FALSE is returned.
  669. Arguments:
  670. szMachine - Name of computer to connect to.
  671. szPath - Path inside registry to connect to.
  672. Return Value:
  673. S_OK - Connection made, Key already exists.
  674. S_FALSE - Connection made, key did not exist but was created
  675. CO_E_OBJNOTCONNECTED - could not connect to registry or key.
  676. E_POINTER - invalid pointer in parameters.
  677. E_FAIL - Failure occured creating the key.
  678. --*/
  679. {
  680. WsbTraceIn ( L"WsbCheckIfRegistryKeyExists",
  681. L"szMachine = '%ls', szPath = '%ls'", szMachine, szPath );
  682. HKEY hKeyMachine = 0,
  683. hKey = 0;
  684. HRESULT hr = S_OK;
  685. try {
  686. //
  687. // Ensure parameters are valid
  688. //
  689. WsbAssert ( 0 != szPath, E_POINTER );
  690. //
  691. // Open the key
  692. //
  693. HRESULT resultOpen = WsbOpenRegistryKey ( szMachine, szPath, KEY_QUERY_VALUE, &hKeyMachine, &hKey );
  694. //
  695. // If key could be opened, everything is fine - return S_OK
  696. //
  697. if ( SUCCEEDED ( resultOpen ) ) {
  698. hr = S_OK;
  699. WsbCloseRegistryKey ( &hKeyMachine, &hKey );
  700. } else {
  701. hr = S_FALSE;
  702. }
  703. } WsbCatch ( hr )
  704. WsbTraceOut ( L"WsbCheckIfRegistryKeyExists",
  705. L"HRESULT = %ls", WsbHrAsString ( hr ) );
  706. return ( hr );
  707. }
  708. HRESULT
  709. WsbEnsureRegistryKeyExists (
  710. IN const OLECHAR * szMachine OPTIONAL,
  711. IN const OLECHAR * szPath
  712. )
  713. /*++
  714. Routine Description:
  715. This routine creates the key specified by szPath. Multiple
  716. levels in the path can be missing and thus created. If the
  717. key already exists, S_OK is returned. If it needed to be
  718. created, S_FALSE is returned.
  719. Arguments:
  720. szMachine - Name of computer to connect to.
  721. szPath - Path inside registry to connect to.
  722. Return Value:
  723. S_OK - Connection made, Key already exists.
  724. S_FALSE - Connection made, key did not exist but was created
  725. CO_E_OBJNOTCONNECTED - could not connect to registry or key.
  726. E_POINTER - invalid pointer in parameters.
  727. E_FAIL - Failure occured creating the key.
  728. --*/
  729. {
  730. WsbTraceIn ( L"WsbEnsureRegistryKeyExists",
  731. L"szMachine = '%ls', szPath = '%ls'", szMachine, szPath );
  732. HKEY hKeyMachine = 0,
  733. hKey = 0;
  734. HRESULT hr = S_OK;
  735. try {
  736. //
  737. // Ensure parameters are valid
  738. //
  739. WsbAssert ( 0 != szPath, E_POINTER );
  740. //
  741. // Open the key
  742. //
  743. HRESULT resultOpen = WsbOpenRegistryKey ( szMachine, szPath, KEY_QUERY_VALUE, &hKeyMachine, &hKey );
  744. //
  745. // If key could be opened, everything is fine - return S_OK
  746. //
  747. if ( SUCCEEDED ( resultOpen ) ) {
  748. hr = S_OK;
  749. } else {
  750. //
  751. // Otherwise, we need to start at root and create missing portion
  752. //
  753. //
  754. // Create a copy of the string. Using WsbQuickString so we have
  755. // automatic freeing of memory
  756. //
  757. WsbQuickString copyString ( szPath );
  758. WCHAR * pSubKey = copyString;
  759. WsbAffirm ( 0 != pSubKey, E_OUTOFMEMORY );
  760. DWORD result, createResult;
  761. HKEY hSubKey;
  762. WsbAffirmHr ( WsbOpenRegistryKey ( szMachine, L"", KEY_CREATE_SUB_KEY, &hKeyMachine, &hKey ) );
  763. pSubKey = wcstok ( pSubKey, L"\\" );
  764. while ( 0 != pSubKey ) {
  765. //
  766. // Create the key. If it exists, RegCreateKeyEx returns
  767. // REG_OPENED_EXISTING_KEY which is ok here.
  768. //
  769. createResult = 0;
  770. result = RegCreateKeyEx ( hKey, pSubKey, 0, L"",
  771. REG_OPTION_NON_VOLATILE, KEY_CREATE_SUB_KEY, 0, &hSubKey, &createResult );
  772. WsbAffirm ( ERROR_SUCCESS == result, E_FAIL );
  773. WsbAffirm (
  774. ( REG_CREATED_NEW_KEY == createResult ) ||
  775. ( REG_OPENED_EXISTING_KEY == createResult), E_FAIL );
  776. //
  777. // And move this hkey to be the next parent
  778. //
  779. RegCloseKey ( hKey );
  780. hKey = hSubKey;
  781. hSubKey = 0;
  782. //
  783. // And finally, find next token
  784. //
  785. pSubKey = wcstok ( 0, L"\\" );
  786. };
  787. //
  788. // If we succeeded to this point, return S_FALSE
  789. // for successfull creation of path
  790. //
  791. hr = S_FALSE;
  792. }
  793. } WsbCatch ( hr )
  794. WsbCloseRegistryKey ( &hKeyMachine, &hKey );
  795. WsbTraceOut ( L"WsbEnsureRegistryKeyExists",
  796. L"HRESULT = %ls", WsbHrAsString ( hr ) );
  797. return ( hr );
  798. }
  799. HRESULT
  800. WsbSetRegistryValueUlongAsString (
  801. IN const OLECHAR * szMachine OPTIONAL,
  802. IN const OLECHAR * szPath,
  803. IN const OLECHAR * szValue,
  804. IN ULONG value
  805. )
  806. /*++
  807. Routine Description:
  808. This routine puts a ULONG value in the registry as a string value.
  809. Arguments:
  810. szMachine - Name of computer to connect to.
  811. szPath - Path inside registry to connect to.
  812. szValue - Name of the value to set.
  813. value - ULONG value to store.
  814. Return Value:
  815. S_OK - Connection made, Success.
  816. CO_E_OBJNOTCONNECTED - could not connect to registry or key.
  817. E_POINTER - invalid pointer in parameters.
  818. E_FAIL - Failure occured setting the value.
  819. --*/
  820. {
  821. HRESULT hr = S_OK;
  822. WsbTraceIn ( L"WsbSetRegistryValueUlongAsString",
  823. L"szMachine = '%ls', szPath = '%ls', szValue = '%ls', value = %lu",
  824. szMachine, szPath, szValue, value );
  825. try {
  826. OLECHAR dataString[100];
  827. WsbAffirmHr(WsbEnsureRegistryKeyExists(szMachine, szPath));
  828. wsprintf(dataString, OLESTR("%lu"), value);
  829. WsbAffirmHr(WsbSetRegistryValueString (szMachine, szPath, szValue,
  830. dataString, REG_SZ));
  831. } WsbCatch ( hr )
  832. WsbTraceOut ( L"WsbSetRegistryValueUlongAsString",
  833. L"HRESULT = %ls", WsbHrAsString ( hr ) );
  834. return ( hr );
  835. }
  836. HRESULT
  837. WsbGetRegistryValueUlongAsString(
  838. IN const OLECHAR * szMachine OPTIONAL,
  839. IN const OLECHAR * szPath,
  840. IN const OLECHAR * szValue,
  841. OUT ULONG * pvalue
  842. )
  843. /*++
  844. Routine Description:
  845. This routine gets a string value from the registry and converts
  846. it to a ULONG value.
  847. Arguments:
  848. szMachine - Name of computer to connect to.
  849. szPath - Path inside registry to connect to.
  850. szValue - Name of the value to get.
  851. pvalue - pointer to a ULONG to store value in.
  852. Return Value:
  853. S_OK - Connection made, Success.
  854. CO_E_OBJNOTCONNECTED - could not connect to registry or key.
  855. E_POINTER - invalid pointer in parameters.
  856. E_FAIL - Failure occured setting the value.
  857. --*/
  858. {
  859. HRESULT hr = S_OK;
  860. WsbTraceIn ( L"WsbGetRegistryValueUlongAsString",
  861. L"szMachine = '%ls', szPath = '%ls', szValue = '%ls', pvalue = 0x%p",
  862. szMachine, szPath, szValue, pvalue );
  863. try {
  864. OLECHAR dataString[100];
  865. DWORD sizeGot;
  866. OLECHAR * stopString;
  867. WsbAssertPointer( pvalue );
  868. WsbAffirmHr(WsbGetRegistryValueString(szMachine, szPath, szValue,
  869. dataString, 100, &sizeGot));
  870. *pvalue = wcstoul( dataString, &stopString, 10 );
  871. } WsbCatch ( hr )
  872. WsbTraceOut ( L"WsbGetRegistryValueUlongAsString",
  873. L"HRESULT = %ls, *pvalue = %ls",
  874. WsbHrAsString ( hr ), WsbPtrToUlongAsString ( pvalue ) );
  875. return ( hr );
  876. }
  877. HRESULT
  878. WsbGetRegistryValueUlongAsMultiString(
  879. IN const OLECHAR * szMachine OPTIONAL,
  880. IN const OLECHAR * szPath,
  881. IN const OLECHAR * szValue,
  882. OUT ULONG ** ppValues,
  883. OUT ULONG * pNumValues
  884. )
  885. /*++
  886. Routine Description:
  887. This routine gets a multi-string value from the registry and converts
  888. it to a vector of ULONG values.
  889. Arguments:
  890. szMachine - Name of computer to connect to.
  891. szPath - Path inside registry to connect to.
  892. szValue - Name of the value to get.
  893. ppvalues - pointer to a ULONG * to alloacte and store the output vector in
  894. pNumValues - Number of items returned
  895. Return Value:
  896. S_OK - Connection made, Success.
  897. CO_E_OBJNOTCONNECTED - could not connect to registry or key.
  898. E_POINTER - invalid pointer in parameters.
  899. E_FAIL - Failure occured getting the value.
  900. --*/
  901. {
  902. HRESULT hr = S_OK;
  903. WsbTraceIn ( L"WsbGetRegistryValueUlongAsString",
  904. L"szMachine = '%ls', szPath = '%ls', szValue = '%ls'",
  905. szMachine, szPath, szValue );
  906. try {
  907. OLECHAR dataString[256];
  908. DWORD sizeGot;
  909. OLECHAR * stopString;
  910. WsbAssertPointer(ppValues);
  911. WsbAssertPointer(pNumValues);
  912. *pNumValues = 0;
  913. *ppValues = NULL;
  914. WsbAffirmHr(WsbGetRegistryValueMultiString(szMachine, szPath, szValue,
  915. dataString, 256, &sizeGot));
  916. // Build the output vector
  917. OLECHAR *currentString = dataString;
  918. int size = 10;
  919. if ((*currentString) != NULL) {
  920. // first alocation
  921. *ppValues = (ULONG *)WsbAlloc(size*sizeof(ULONG));
  922. WsbAffirm(*ppValues != 0, E_OUTOFMEMORY);
  923. } else {
  924. hr = E_FAIL;
  925. }
  926. while ((*currentString) != NULL) {
  927. (*ppValues)[*pNumValues] = wcstoul( currentString, &stopString, 10 );
  928. (*pNumValues)++;
  929. if (*pNumValues == size) {
  930. size += 10;
  931. ULONG* pTmp = (ULONG *)WsbRealloc(*ppValues, size*sizeof(ULONG));
  932. WsbAffirm(0 != pTmp, E_OUTOFMEMORY);
  933. *ppValues = pTmp;
  934. }
  935. currentString += wcslen(currentString);
  936. currentString ++;
  937. }
  938. } WsbCatch ( hr )
  939. WsbTraceOut ( L"WsbGetRegistryValueUlongAsString",
  940. L"HRESULT = %ls, num values = %lu",
  941. WsbHrAsString ( hr ), *pNumValues );
  942. return ( hr );
  943. }
  944. HRESULT
  945. WsbRegistryValueUlongAsString(
  946. IN const OLECHAR * szMachine OPTIONAL,
  947. IN const OLECHAR * szPath,
  948. IN const OLECHAR * szValue,
  949. IN OUT ULONG * pvalue
  950. )
  951. /*++
  952. Routine Description:
  953. If a registry string value is present, this routine gets it and converts
  954. it to a ULONG value. If it is not present, this routine sets it to the
  955. supplied default value.
  956. Arguments:
  957. szMachine - Name of computer to connect to.
  958. szPath - Path inside registry to connect to.
  959. szValue - Name of the value to get.
  960. pvalue - In: default value , Out: pointer to a ULONG to store value.
  961. Return Value:
  962. S_OK - Connection made, Success.
  963. CO_E_OBJNOTCONNECTED - could not connect to registry or key.
  964. E_POINTER - invalid pointer in parameters.
  965. E_FAIL - Failure occured setting the value.
  966. --*/
  967. {
  968. HRESULT hr = S_OK;
  969. WsbTraceIn ( L"WsbRegistryValueUlongAsString",
  970. L"szMachine = '%ls', szPath = '%ls', szValue = '%ls', pvalue = 0x%p",
  971. szMachine, szPath, szValue, pvalue );
  972. try {
  973. ULONG l_value;
  974. WsbAssertPointer( pvalue );
  975. if (S_OK == WsbGetRegistryValueUlongAsString(szMachine, szPath, szValue,
  976. &l_value)) {
  977. *pvalue = l_value;
  978. } else {
  979. WsbAffirmHr(WsbSetRegistryValueUlongAsString(szMachine, szPath,
  980. szValue, *pvalue));
  981. }
  982. } WsbCatch ( hr )
  983. WsbTraceOut ( L"WsbRegistryValueUlongAsString",
  984. L"HRESULT = %ls, *pvalue = %ls",
  985. WsbHrAsString ( hr ), WsbPtrToUlongAsString ( pvalue ) );
  986. return ( hr );
  987. }