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.

1580 lines
36 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 - 1) * 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. //
  415. // Ensure NULL termination if not already null terminated
  416. //
  417. if (szString[*pcLength] != L'\0') {
  418. szString[*pcLength] = L'\0';
  419. (*pcLength)++;
  420. }
  421. } WsbCatch ( hr )
  422. WsbCloseRegistryKey ( &hKeyMachine, &hKey );
  423. WsbTraceOut ( L"WsbGetRegistryValueString",
  424. L"HRESULT = %ls, szString = '%ls', *pcbLength = %ls",
  425. WsbHrAsString ( hr ), szString, WsbPtrToUlongAsString ( pcLength ) );
  426. return ( hr );
  427. }
  428. HRESULT
  429. WsbGetRegistryValueMultiString (
  430. IN const OLECHAR * szMachine OPTIONAL,
  431. IN const OLECHAR * szPath,
  432. IN const OLECHAR * szValue,
  433. OUT OLECHAR * szMultiString,
  434. IN DWORD cSize,
  435. OUT DWORD *pcLength OPTIONAL
  436. )
  437. /*++
  438. Routine Description:
  439. This routine get the value specified
  440. Type of the value must be REG_MULTI_SZ
  441. Arguments:
  442. szMachine - Name of computer to connect to.
  443. szPath - Path inside registry to connect to.
  444. szValue - Name of the value to get.
  445. szMultiString - The string buffer to fill with the value.
  446. cSize - Size of szString in OLECAHR's.
  447. pcLength - Number of OLECHAR actually written (without L'\0').
  448. Return Value:
  449. S_OK - Connection made, Success.
  450. CO_E_OBJNOTCONNECTED - could not connect to registry or key.
  451. E_POINTER - invalid pointer in parameters.
  452. E_FAIL - Failure occured setting the value.
  453. --*/
  454. {
  455. WsbTraceIn ( L"WsbGetRegistryValueMultiString",
  456. L"szMachine = '%ls', szPath = '%ls', szValue = '%ls', szMultiString = 0x%p, cSize = '%ld', pcLength = 0x%p",
  457. szMachine, szPath, szValue, szMultiString, cSize, pcLength );
  458. HKEY hKeyMachine = 0,
  459. hKey = 0;
  460. HRESULT hr = S_OK;
  461. try {
  462. //
  463. // Ensure parameters are valid
  464. //
  465. WsbAssert ( 0 != szPath, E_POINTER );
  466. WsbAssert ( 0 != szValue, E_POINTER );
  467. WsbAssert ( 0 != szMultiString, E_POINTER );
  468. //
  469. // Open the key
  470. //
  471. WsbAffirmHr ( WsbOpenRegistryKey ( szMachine, szPath, KEY_QUERY_VALUE, &hKeyMachine, &hKey ) );
  472. //
  473. // Temporary size vars in case pcLength is NULL
  474. //
  475. DWORD dwType, cbData2;
  476. if ( !pcLength ) {
  477. pcLength = &cbData2;
  478. }
  479. //
  480. // And do the query
  481. //
  482. *pcLength = (cSize - 1) * sizeof ( OLECHAR );
  483. WsbAffirmWin32 ( RegQueryValueEx ( hKey, szValue, 0, &dwType, (BYTE*)szMultiString, pcLength ) ) ;
  484. WsbAffirm ( REG_MULTI_SZ == dwType, E_FAIL );
  485. //
  486. // return characters, not bytes
  487. //
  488. *pcLength = ( *pcLength / sizeof ( OLECHAR ) ) - 1;
  489. //
  490. // Ensure NULL termination if not already null terminated
  491. //
  492. if (szMultiString[*pcLength] != L'\0') {
  493. szMultiString[*pcLength] = L'\0';
  494. (*pcLength)++;
  495. }
  496. } WsbCatch ( hr )
  497. WsbCloseRegistryKey ( &hKeyMachine, &hKey );
  498. WsbTraceOut ( L"WsbGetRegistryValueMultiString",
  499. L"HRESULT = %ls, *pcbLength = %ls",
  500. WsbHrAsString ( hr ), WsbPtrToUlongAsString ( pcLength ) );
  501. return ( hr );
  502. }
  503. HRESULT
  504. WsbSetRegistryValueDWORD (
  505. IN const OLECHAR * szMachine OPTIONAL,
  506. IN const OLECHAR * szPath,
  507. IN const OLECHAR * szValue,
  508. IN DWORD dw
  509. )
  510. /*++
  511. Routine Description:
  512. This routine set the value of a key as specified to the data
  513. given. Type of the value is REG_DWORD
  514. Arguments:
  515. szMachine - Name of computer to connect to.
  516. szPath - Path inside registry to connect to.
  517. szValue - Name of the value to set.
  518. dw - DWORD value to store.
  519. Return Value:
  520. S_OK - Connection made, Success.
  521. CO_E_OBJNOTCONNECTED - could not connect to registry or key.
  522. E_POINTER - invalid pointer in parameters.
  523. E_FAIL - Failure occured setting the value.
  524. --*/
  525. {
  526. WsbTraceIn ( L"WsbSetRegistryValueDWORD",
  527. L"szMachine = '%ls', szPath = '%ls', szValue = '%ls', dw = %lu [0x%p]",
  528. szMachine, szPath, szValue, dw, dw );
  529. HKEY hKeyMachine = 0,
  530. hKey = 0;
  531. HRESULT hr = S_OK;
  532. try {
  533. //
  534. // Ensure parameters are valid
  535. //
  536. WsbAssertPointer( szPath );
  537. WsbAssertPointer( szValue );
  538. //
  539. // Open the key
  540. //
  541. WsbAffirmHr ( WsbOpenRegistryKey ( szMachine, szPath, KEY_SET_VALUE, &hKeyMachine, &hKey ) );
  542. WsbAffirmWin32 ( RegSetValueEx ( hKey, szValue, 0, REG_DWORD, (BYTE*)&dw, sizeof( DWORD ) ) );
  543. } WsbCatch ( hr )
  544. WsbCloseRegistryKey ( &hKeyMachine, &hKey );
  545. WsbTraceOut ( L"WsbSetRegistryValueDWORD",
  546. L"HRESULT = %ls", WsbHrAsString ( hr ) );
  547. return ( hr );
  548. }
  549. HRESULT
  550. WsbGetRegistryValueDWORD(
  551. IN const OLECHAR * szMachine OPTIONAL,
  552. IN const OLECHAR * szPath,
  553. IN const OLECHAR * szValue,
  554. OUT DWORD * pdw
  555. )
  556. /*++
  557. Routine Description:
  558. This routine set the value of a key as specified to the data
  559. given. Type of the value is REG_SZ or REG_EXPAND_SZ
  560. Arguments:
  561. szMachine - Name of computer to connect to.
  562. szPath - Path inside registry to connect to.
  563. szValue - Name of the value to get.
  564. pdw - pointer to a DWORD to store value in.
  565. Return Value:
  566. S_OK - Connection made, Success.
  567. CO_E_OBJNOTCONNECTED - could not connect to registry or key.
  568. E_POINTER - invalid pointer in parameters.
  569. E_FAIL - Failure occured setting the value.
  570. --*/
  571. {
  572. WsbTraceIn ( L"WsbGetRegistryValueDWORD",
  573. L"szMachine = '%ls', szPath = '%ls', szValue = '%ls', pdw = 0x%p",
  574. szMachine, szPath, szValue, pdw );
  575. HKEY hKeyMachine = 0,
  576. hKey = 0;
  577. HRESULT hr = S_OK;
  578. try {
  579. //
  580. // Ensure parameters are valid
  581. //
  582. WsbAssertPointer( szPath );
  583. WsbAssertPointer( szValue );
  584. WsbAssertPointer( pdw );
  585. //
  586. // Open the key
  587. //
  588. WsbAffirmHr ( WsbOpenRegistryKey ( szMachine, szPath, KEY_QUERY_VALUE, &hKeyMachine, &hKey ) );
  589. //
  590. // And do the query
  591. //
  592. DWORD dwType, cbData = sizeof( DWORD );
  593. WsbAffirmWin32 ( RegQueryValueEx ( hKey, szValue, 0, &dwType, (BYTE*)pdw, &cbData ) ) ;
  594. WsbAffirm( REG_DWORD == dwType, E_FAIL );
  595. } WsbCatch ( hr )
  596. WsbCloseRegistryKey ( &hKeyMachine, &hKey );
  597. WsbTraceOut ( L"WsbGetRegistryValueDWORD",
  598. L"HRESULT = %ls, *pdw = %ls",
  599. WsbHrAsString ( hr ), WsbPtrToUlongAsString ( pdw ) );
  600. return ( hr );
  601. }
  602. HRESULT
  603. WsbAddRegistryValueDWORD (
  604. IN const OLECHAR * szMachine OPTIONAL,
  605. IN const OLECHAR * szPath,
  606. IN const OLECHAR * szValue,
  607. IN DWORD adw
  608. )
  609. /*++
  610. Routine Description:
  611. This routine adds an amount to a registry value.
  612. Type of the value must be REG_DWORD
  613. Arguments:
  614. szMachine - Name of computer to connect to.
  615. szPath - Path inside registry to connect to.
  616. szValue - Name of the value to increment
  617. adw - DWORD value to add.
  618. Return Value:
  619. S_OK - Connection made, Success.
  620. CO_E_OBJNOTCONNECTED - could not connect to registry or key.
  621. E_POINTER - invalid pointer in parameters.
  622. E_FAIL - Failure occured setting the value.
  623. --*/
  624. {
  625. WsbTraceIn ( L"WsbAddRegistryValueDWORD",
  626. L"szMachine = '%ls', szPath = '%ls', szValue = '%ls', adw = %lu",
  627. szMachine, szPath, szValue, adw);
  628. HRESULT hr = S_OK;
  629. DWORD value = 0;
  630. // Get the old value
  631. hr = WsbGetRegistryValueDWORD(szMachine, szPath, szValue, &value);
  632. // Add to value and replace
  633. if (S_OK == hr) {
  634. value += adw;
  635. } else {
  636. value = adw;
  637. }
  638. hr = WsbSetRegistryValueDWORD(szMachine, szPath, szValue, value);
  639. WsbTraceOut ( L"WsbAddRegistryValueDWORD",
  640. L"HRESULT = %ls", WsbHrAsString ( hr ) );
  641. return ( hr );
  642. }
  643. HRESULT
  644. WsbIncRegistryValueDWORD (
  645. IN const OLECHAR * szMachine OPTIONAL,
  646. IN const OLECHAR * szPath,
  647. IN const OLECHAR * szValue
  648. )
  649. /*++
  650. Routine Description:
  651. This routine increments a registry value by one.
  652. Type of the value must be REG_DWORD
  653. Arguments:
  654. szMachine - Name of computer to connect to.
  655. szPath - Path inside registry to connect to.
  656. szValue - Name of the value to increment
  657. Return Value:
  658. S_OK - Connection made, Success.
  659. CO_E_OBJNOTCONNECTED - could not connect to registry or key.
  660. E_POINTER - invalid pointer in parameters.
  661. E_FAIL - Failure occured setting the value.
  662. --*/
  663. {
  664. WsbTraceIn ( L"WsbIncRegistryValueDWORD",
  665. L"szMachine = '%ls', szPath = '%ls', szValue = '%ls'",
  666. szMachine, szPath, szValue);
  667. HRESULT hr = S_OK;
  668. hr = WsbAddRegistryValueDWORD(szMachine, szPath, szValue, 1);
  669. WsbTraceOut ( L"WsbIncRegistryValueDWORD",
  670. L"HRESULT = %ls", WsbHrAsString ( hr ) );
  671. return ( hr );
  672. }
  673. HRESULT
  674. WsbCheckIfRegistryKeyExists(
  675. IN const OLECHAR * szMachine OPTIONAL,
  676. IN const OLECHAR * szPath
  677. )
  678. /*++
  679. Routine Description:
  680. This routine check if the supplied key exists
  681. If the key already exists, S_OK is returned.
  682. If it needed to be created, S_FALSE is returned.
  683. Arguments:
  684. szMachine - Name of computer to connect to.
  685. szPath - Path inside registry to connect to.
  686. Return Value:
  687. S_OK - Connection made, Key already exists.
  688. S_FALSE - Connection made, key did not exist but was created
  689. CO_E_OBJNOTCONNECTED - could not connect to registry or key.
  690. E_POINTER - invalid pointer in parameters.
  691. E_FAIL - Failure occured creating the key.
  692. --*/
  693. {
  694. WsbTraceIn ( L"WsbCheckIfRegistryKeyExists",
  695. L"szMachine = '%ls', szPath = '%ls'", szMachine, szPath );
  696. HKEY hKeyMachine = 0,
  697. hKey = 0;
  698. HRESULT hr = S_OK;
  699. try {
  700. //
  701. // Ensure parameters are valid
  702. //
  703. WsbAssert ( 0 != szPath, E_POINTER );
  704. //
  705. // Open the key
  706. //
  707. HRESULT resultOpen = WsbOpenRegistryKey ( szMachine, szPath, KEY_QUERY_VALUE, &hKeyMachine, &hKey );
  708. //
  709. // If key could be opened, everything is fine - return S_OK
  710. //
  711. if ( SUCCEEDED ( resultOpen ) ) {
  712. hr = S_OK;
  713. WsbCloseRegistryKey ( &hKeyMachine, &hKey );
  714. } else {
  715. hr = S_FALSE;
  716. }
  717. } WsbCatch ( hr )
  718. WsbTraceOut ( L"WsbCheckIfRegistryKeyExists",
  719. L"HRESULT = %ls", WsbHrAsString ( hr ) );
  720. return ( hr );
  721. }
  722. HRESULT
  723. WsbEnsureRegistryKeyExists (
  724. IN const OLECHAR * szMachine OPTIONAL,
  725. IN const OLECHAR * szPath
  726. )
  727. /*++
  728. Routine Description:
  729. This routine creates the key specified by szPath. Multiple
  730. levels in the path can be missing and thus created. If the
  731. key already exists, S_OK is returned. If it needed to be
  732. created, S_FALSE is returned.
  733. Arguments:
  734. szMachine - Name of computer to connect to.
  735. szPath - Path inside registry to connect to.
  736. Return Value:
  737. S_OK - Connection made, Key already exists.
  738. S_FALSE - Connection made, key did not exist but was created
  739. CO_E_OBJNOTCONNECTED - could not connect to registry or key.
  740. E_POINTER - invalid pointer in parameters.
  741. E_FAIL - Failure occured creating the key.
  742. --*/
  743. {
  744. WsbTraceIn ( L"WsbEnsureRegistryKeyExists",
  745. L"szMachine = '%ls', szPath = '%ls'", szMachine, szPath );
  746. HKEY hKeyMachine = 0,
  747. hKey = 0;
  748. HRESULT hr = S_OK;
  749. try {
  750. //
  751. // Ensure parameters are valid
  752. //
  753. WsbAssert ( 0 != szPath, E_POINTER );
  754. //
  755. // Open the key
  756. //
  757. HRESULT resultOpen = WsbOpenRegistryKey ( szMachine, szPath, KEY_QUERY_VALUE, &hKeyMachine, &hKey );
  758. //
  759. // If key could be opened, everything is fine - return S_OK
  760. //
  761. if ( SUCCEEDED ( resultOpen ) ) {
  762. hr = S_OK;
  763. } else {
  764. //
  765. // Otherwise, we need to start at root and create missing portion
  766. //
  767. //
  768. // Create a copy of the string. Using WsbQuickString so we have
  769. // automatic freeing of memory
  770. //
  771. WsbQuickString copyString ( szPath );
  772. WCHAR * pSubKey = copyString;
  773. WsbAffirm ( 0 != pSubKey, E_OUTOFMEMORY );
  774. DWORD result, createResult;
  775. HKEY hSubKey;
  776. WsbAffirmHr ( WsbOpenRegistryKey ( szMachine, L"", KEY_CREATE_SUB_KEY, &hKeyMachine, &hKey ) );
  777. pSubKey = wcstok ( pSubKey, L"\\" );
  778. while ( 0 != pSubKey ) {
  779. //
  780. // Create the key. If it exists, RegCreateKeyEx returns
  781. // REG_OPENED_EXISTING_KEY which is ok here.
  782. //
  783. createResult = 0;
  784. result = RegCreateKeyEx ( hKey, pSubKey, 0, L"",
  785. REG_OPTION_NON_VOLATILE, KEY_CREATE_SUB_KEY, 0, &hSubKey, &createResult );
  786. WsbAffirm ( ERROR_SUCCESS == result, E_FAIL );
  787. WsbAffirm (
  788. ( REG_CREATED_NEW_KEY == createResult ) ||
  789. ( REG_OPENED_EXISTING_KEY == createResult), E_FAIL );
  790. //
  791. // And move this hkey to be the next parent
  792. //
  793. RegCloseKey ( hKey );
  794. hKey = hSubKey;
  795. hSubKey = 0;
  796. //
  797. // And finally, find next token
  798. //
  799. pSubKey = wcstok ( 0, L"\\" );
  800. };
  801. //
  802. // If we succeeded to this point, return S_FALSE
  803. // for successfull creation of path
  804. //
  805. hr = S_FALSE;
  806. }
  807. } WsbCatch ( hr )
  808. WsbCloseRegistryKey ( &hKeyMachine, &hKey );
  809. WsbTraceOut ( L"WsbEnsureRegistryKeyExists",
  810. L"HRESULT = %ls", WsbHrAsString ( hr ) );
  811. return ( hr );
  812. }
  813. HRESULT
  814. WsbSetRegistryValueUlongAsString (
  815. IN const OLECHAR * szMachine OPTIONAL,
  816. IN const OLECHAR * szPath,
  817. IN const OLECHAR * szValue,
  818. IN ULONG value
  819. )
  820. /*++
  821. Routine Description:
  822. This routine puts a ULONG value in the registry as a string value.
  823. Arguments:
  824. szMachine - Name of computer to connect to.
  825. szPath - Path inside registry to connect to.
  826. szValue - Name of the value to set.
  827. value - ULONG value to store.
  828. Return Value:
  829. S_OK - Connection made, Success.
  830. CO_E_OBJNOTCONNECTED - could not connect to registry or key.
  831. E_POINTER - invalid pointer in parameters.
  832. E_FAIL - Failure occured setting the value.
  833. --*/
  834. {
  835. HRESULT hr = S_OK;
  836. WsbTraceIn ( L"WsbSetRegistryValueUlongAsString",
  837. L"szMachine = '%ls', szPath = '%ls', szValue = '%ls', value = %lu",
  838. szMachine, szPath, szValue, value );
  839. try {
  840. OLECHAR dataString[100];
  841. WsbAffirmHr(WsbEnsureRegistryKeyExists(szMachine, szPath));
  842. wsprintf(dataString, OLESTR("%lu"), value);
  843. WsbAffirmHr(WsbSetRegistryValueString (szMachine, szPath, szValue,
  844. dataString, REG_SZ));
  845. } WsbCatch ( hr )
  846. WsbTraceOut ( L"WsbSetRegistryValueUlongAsString",
  847. L"HRESULT = %ls", WsbHrAsString ( hr ) );
  848. return ( hr );
  849. }
  850. HRESULT
  851. WsbGetRegistryValueUlongAsString(
  852. IN const OLECHAR * szMachine OPTIONAL,
  853. IN const OLECHAR * szPath,
  854. IN const OLECHAR * szValue,
  855. OUT ULONG * pvalue
  856. )
  857. /*++
  858. Routine Description:
  859. This routine gets a string value from the registry and converts
  860. it to a ULONG value.
  861. Arguments:
  862. szMachine - Name of computer to connect to.
  863. szPath - Path inside registry to connect to.
  864. szValue - Name of the value to get.
  865. pvalue - pointer to a ULONG to store value in.
  866. Return Value:
  867. S_OK - Connection made, Success.
  868. CO_E_OBJNOTCONNECTED - could not connect to registry or key.
  869. E_POINTER - invalid pointer in parameters.
  870. E_FAIL - Failure occured setting the value.
  871. --*/
  872. {
  873. HRESULT hr = S_OK;
  874. WsbTraceIn ( L"WsbGetRegistryValueUlongAsString",
  875. L"szMachine = '%ls', szPath = '%ls', szValue = '%ls', pvalue = 0x%p",
  876. szMachine, szPath, szValue, pvalue );
  877. try {
  878. OLECHAR dataString[100];
  879. DWORD sizeGot;
  880. OLECHAR * stopString;
  881. WsbAssertPointer( pvalue );
  882. WsbAffirmHr(WsbGetRegistryValueString(szMachine, szPath, szValue,
  883. dataString, 100, &sizeGot));
  884. *pvalue = wcstoul( dataString, &stopString, 10 );
  885. } WsbCatch ( hr )
  886. WsbTraceOut ( L"WsbGetRegistryValueUlongAsString",
  887. L"HRESULT = %ls, *pvalue = %ls",
  888. WsbHrAsString ( hr ), WsbPtrToUlongAsString ( pvalue ) );
  889. return ( hr );
  890. }
  891. HRESULT
  892. WsbGetRegistryValueUlongAsMultiString(
  893. IN const OLECHAR * szMachine OPTIONAL,
  894. IN const OLECHAR * szPath,
  895. IN const OLECHAR * szValue,
  896. OUT ULONG ** ppValues,
  897. OUT ULONG * pNumValues
  898. )
  899. /*++
  900. Routine Description:
  901. This routine gets a multi-string value from the registry and converts
  902. it to a vector of ULONG values.
  903. Arguments:
  904. szMachine - Name of computer to connect to.
  905. szPath - Path inside registry to connect to.
  906. szValue - Name of the value to get.
  907. ppvalues - pointer to a ULONG * to alloacte and store the output vector in
  908. pNumValues - Number of items returned
  909. Return Value:
  910. S_OK - Connection made, Success.
  911. CO_E_OBJNOTCONNECTED - could not connect to registry or key.
  912. E_POINTER - invalid pointer in parameters.
  913. E_FAIL - Failure occured getting the value.
  914. --*/
  915. {
  916. HRESULT hr = S_OK;
  917. WsbTraceIn ( L"WsbGetRegistryValueUlongAsString",
  918. L"szMachine = '%ls', szPath = '%ls', szValue = '%ls'",
  919. szMachine, szPath, szValue );
  920. try {
  921. OLECHAR dataString[256];
  922. DWORD sizeGot;
  923. OLECHAR * stopString;
  924. WsbAssertPointer(ppValues);
  925. WsbAssertPointer(pNumValues);
  926. *pNumValues = 0;
  927. *ppValues = NULL;
  928. WsbAffirmHr(WsbGetRegistryValueMultiString(szMachine, szPath, szValue,
  929. dataString, 256, &sizeGot));
  930. // Build the output vector
  931. OLECHAR *currentString = dataString;
  932. int size = 10;
  933. if ((*currentString) != NULL) {
  934. // first alocation
  935. *ppValues = (ULONG *)WsbAlloc(size*sizeof(ULONG));
  936. WsbAffirm(*ppValues != 0, E_OUTOFMEMORY);
  937. } else {
  938. hr = E_FAIL;
  939. }
  940. while ((*currentString) != NULL) {
  941. (*ppValues)[*pNumValues] = wcstoul( currentString, &stopString, 10 );
  942. (*pNumValues)++;
  943. if (*pNumValues == size) {
  944. size += 10;
  945. ULONG* pTmp = (ULONG *)WsbRealloc(*ppValues, size*sizeof(ULONG));
  946. WsbAffirm(0 != pTmp, E_OUTOFMEMORY);
  947. *ppValues = pTmp;
  948. }
  949. currentString += wcslen(currentString);
  950. currentString ++;
  951. }
  952. } WsbCatch ( hr )
  953. WsbTraceOut ( L"WsbGetRegistryValueUlongAsString",
  954. L"HRESULT = %ls, num values = %lu",
  955. WsbHrAsString ( hr ), *pNumValues );
  956. return ( hr );
  957. }
  958. HRESULT
  959. WsbRegistryValueUlongAsString(
  960. IN const OLECHAR * szMachine OPTIONAL,
  961. IN const OLECHAR * szPath,
  962. IN const OLECHAR * szValue,
  963. IN OUT ULONG * pvalue
  964. )
  965. /*++
  966. Routine Description:
  967. If a registry string value is present, this routine gets it and converts
  968. it to a ULONG value. If it is not present, this routine sets it to the
  969. supplied default value.
  970. Arguments:
  971. szMachine - Name of computer to connect to.
  972. szPath - Path inside registry to connect to.
  973. szValue - Name of the value to get.
  974. pvalue - In: default value , Out: pointer to a ULONG to store value.
  975. Return Value:
  976. S_OK - Connection made, Success.
  977. CO_E_OBJNOTCONNECTED - could not connect to registry or key.
  978. E_POINTER - invalid pointer in parameters.
  979. E_FAIL - Failure occured setting the value.
  980. --*/
  981. {
  982. HRESULT hr = S_OK;
  983. WsbTraceIn ( L"WsbRegistryValueUlongAsString",
  984. L"szMachine = '%ls', szPath = '%ls', szValue = '%ls', pvalue = 0x%p",
  985. szMachine, szPath, szValue, pvalue );
  986. try {
  987. ULONG l_value;
  988. WsbAssertPointer( pvalue );
  989. if (S_OK == WsbGetRegistryValueUlongAsString(szMachine, szPath, szValue,
  990. &l_value)) {
  991. *pvalue = l_value;
  992. } else {
  993. WsbAffirmHr(WsbSetRegistryValueUlongAsString(szMachine, szPath,
  994. szValue, *pvalue));
  995. }
  996. } WsbCatch ( hr )
  997. WsbTraceOut ( L"WsbRegistryValueUlongAsString",
  998. L"HRESULT = %ls, *pvalue = %ls",
  999. WsbHrAsString ( hr ), WsbPtrToUlongAsString ( pvalue ) );
  1000. return ( hr );
  1001. }