home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / winbase / cluster / smbsmp / smbsmpex / regext.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-29  |  26.9 KB  |  1,040 lines

  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. //    Copyright (c) 1997 <company name>
  4. //
  5. //    Module Name:
  6. //        RegExt.cpp
  7. //
  8. //    Abstract:
  9. //        Implementation of routines for extension registration.
  10. //
  11. //    Author:
  12. //        <name> (<e-mail name>) Mmmm DD, 1997
  13. //
  14. //    Revision History:
  15. //
  16. //    Notes:
  17. //
  18. /////////////////////////////////////////////////////////////////////////////
  19.  
  20. #include <stdafx.h>
  21. #include <ole2.h>
  22.  
  23. #ifdef _DEBUG
  24. #define new DEBUG_NEW
  25. #undef THIS_FILE
  26. static char THIS_FILE[] = __FILE__;
  27. #endif
  28.  
  29. #define REG_VALUE_ADMIN_EXTENSIONS L"AdminExtensions"
  30.  
  31. /////////////////////////////////////////////////////////////////////////////
  32. // Static Function Prototypes
  33. /////////////////////////////////////////////////////////////////////////////
  34.  
  35. static HRESULT RegisterAnyCluAdminExtension(
  36.     IN HCLUSTER            hCluster,
  37.     IN LPCWSTR            pwszKeyName,
  38.     IN const CLSID *    pClsid
  39.     );
  40. static HRESULT RegisterAnyCluAdminExtension(
  41.     IN HKEY                hkey,
  42.     IN const CLSID *    pClsid
  43.     );
  44. static HRESULT UnregisterAnyCluAdminExtension(
  45.     IN HCLUSTER            hCluster,
  46.     IN LPCWSTR            pwszKeyName,
  47.     IN const CLSID *    pClsid
  48.     );
  49. static HRESULT UnregisterAnyCluAdminExtension(
  50.     IN HKEY                hkey,
  51.     IN const CLSID *    pClsid
  52.     );
  53. static DWORD ReadValue(
  54.     IN HKEY            hkey,
  55.     IN LPCWSTR        pwszValueName,
  56.     OUT LPWSTR *    ppwszValue,
  57.     OUT DWORD *        pcbSize
  58.     );
  59.  
  60. /////////////////////////////////////////////////////////////////////////////
  61. //++
  62. //
  63. //    RegisterCluAdminClusterExtension
  64. //
  65. //    Routine Description:
  66. //        Register with the cluster database a Cluster Administrator Extension
  67. //        DLL that extends the cluster object.
  68. //
  69. //    Arguments:
  70. //        hCluster        [IN] Handle to the cluster to modify.
  71. //        pClsid            [IN] Extension's CLSID.
  72. //
  73. //    Return Value:
  74. //        S_OK            Extension registered successfully.
  75. //        Win32 error code if another failure occurred.
  76. //
  77. //--
  78. /////////////////////////////////////////////////////////////////////////////
  79. STDAPI RegisterCluAdminClusterExtension(
  80.     IN HCLUSTER            hCluster,
  81.     IN const CLSID *    pClsid
  82.     )
  83. {
  84.     HRESULT        hr;
  85.     HKEY        hkey;
  86.  
  87.     // Get the cluster registry key.
  88.     hkey = GetClusterKey(hCluster, KEY_ALL_ACCESS);
  89.     if (hkey == NULL)
  90.         hr = GetLastError();
  91.     else
  92.     {
  93.         // Register the extension.
  94.         hr = RegisterAnyCluAdminExtension(hkey, pClsid);
  95.  
  96.         ClusterRegCloseKey(hkey);
  97.     }  // else:  GetClusterKey succeeded
  98.  
  99.     return hr;
  100.  
  101. }  //*** RegisterCluAdminClusterExtension()
  102.  
  103. /////////////////////////////////////////////////////////////////////////////
  104. //++
  105. //
  106. //    RegisterCluAdminAllNodesExtension
  107. //
  108. //    Routine Description:
  109. //        Register with the cluster database a Cluster Administrator Extension
  110. //        DLL that extends all nodes.
  111. //
  112. //    Arguments:
  113. //        hCluster        [IN] Handle to the cluster to modify.
  114. //        pClsid            [IN] Extension's CLSID.
  115. //
  116. //    Return Value:
  117. //        S_OK            Extension registered successfully.
  118. //        Win32 error code if another failure occurred.
  119. //
  120. //--
  121. /////////////////////////////////////////////////////////////////////////////
  122. STDAPI RegisterCluAdminAllNodesExtension(
  123.     IN HCLUSTER            hCluster,
  124.     IN const CLSID *    pClsid
  125.     )
  126. {
  127.     HRESULT        hr;
  128.  
  129.     hr = RegisterAnyCluAdminExtension(hCluster, L"Nodes", pClsid);
  130.  
  131.     return hr;
  132.  
  133. }  //*** RegisterCluAdminAllNodesExtension()
  134.  
  135. /////////////////////////////////////////////////////////////////////////////
  136. //++
  137. //
  138. //    RegisterCluAdminAllGroupsExtension
  139. //
  140. //    Routine Description:
  141. //        Register with the cluster database a Cluster Administrator Extension
  142. //        DLL that extends all groups.
  143. //
  144. //    Arguments:
  145. //        hCluster        [IN] Handle to the cluster to modify.
  146. //        pClsid            [IN] Extension's CLSID.
  147. //
  148. //    Return Value:
  149. //        S_OK            Extension registered successfully.
  150. //        Win32 error code if another failure occurred.
  151. //
  152. //--
  153. /////////////////////////////////////////////////////////////////////////////
  154. STDAPI RegisterCluAdminAllGroupsExtension(
  155.     IN HCLUSTER            hCluster,
  156.     IN const CLSID *    pClsid
  157.     )
  158. {
  159.     HRESULT        hr;
  160.  
  161.     hr = RegisterAnyCluAdminExtension(hCluster, L"Groups", pClsid);
  162.  
  163.     return hr;
  164.  
  165. }  //*** RegisterCluAdminAllGroupsExtension()
  166.  
  167. /////////////////////////////////////////////////////////////////////////////
  168. //++
  169. //
  170. //    RegisterCluAdminAllResourcesExtension
  171. //
  172. //    Routine Description:
  173. //        Register with the cluster database a Cluster Administrator Extension
  174. //        DLL that extends all resources.
  175. //
  176. //    Arguments:
  177. //        hCluster        [IN] Handle to the cluster to modify.
  178. //        pClsid            [IN] Extension's CLSID.
  179. //
  180. //    Return Value:
  181. //        S_OK            Extension registered successfully.
  182. //        Win32 error code if another failure occurred.
  183. //
  184. //--
  185. /////////////////////////////////////////////////////////////////////////////
  186. STDAPI RegisterCluAdminAllResourcesExtension(
  187.     IN HCLUSTER            hCluster,
  188.     IN const CLSID *    pClsid
  189.     )
  190. {
  191.     HRESULT        hr;
  192.  
  193.     hr = RegisterAnyCluAdminExtension(hCluster, L"Resources", pClsid);
  194.  
  195.     return hr;
  196.  
  197. }  //*** RegisterCluAdminAllResourcesExtension()
  198.  
  199. /////////////////////////////////////////////////////////////////////////////
  200. //++
  201. //
  202. //    RegisterCluAdminAllResourceTypesExtension
  203. //
  204. //    Routine Description:
  205. //        Register with the cluster database a Cluster Administrator Extension
  206. //        DLL that extends all resource types.
  207. //
  208. //    Arguments:
  209. //        hCluster        [IN] Handle to the cluster to modify.
  210. //        pClsid            [IN] Extension's CLSID.
  211. //
  212. //    Return Value:
  213. //        S_OK            Extension registered successfully.
  214. //        Win32 error code if another failure occurred.
  215. //
  216. //--
  217. /////////////////////////////////////////////////////////////////////////////
  218. STDAPI RegisterCluAdminAllResourceTypesExtension(
  219.     IN HCLUSTER            hCluster,
  220.     IN const CLSID *    pClsid
  221.     )
  222. {
  223.     HRESULT        hr;
  224.  
  225.     hr = RegisterAnyCluAdminExtension(hCluster, L"ResourceTypes", pClsid);
  226.  
  227.     return hr;
  228.  
  229. }  //*** RegisterCluAdminAllResourceTypesExtension()
  230.  
  231. /////////////////////////////////////////////////////////////////////////////
  232. //++
  233. //
  234. //    RegisterCluAdminAllNetworksExtension
  235. //
  236. //    Routine Description:
  237. //        Register with the cluster database a Cluster Administrator Extension
  238. //        DLL that extends all networks.
  239. //
  240. //    Arguments:
  241. //        hCluster        [IN] Handle to the cluster to modify.
  242. //        pClsid            [IN] Extension's CLSID.
  243. //
  244. //    Return Value:
  245. //        S_OK            Extension registered successfully.
  246. //        Win32 error code if another failure occurred.
  247. //
  248. //--
  249. /////////////////////////////////////////////////////////////////////////////
  250. STDAPI RegisterCluAdminAllNetworksExtension(
  251.     IN HCLUSTER            hCluster,
  252.     IN const CLSID *    pClsid
  253.     )
  254. {
  255.     HRESULT        hr;
  256.  
  257.     hr = RegisterAnyCluAdminExtension(hCluster, L"Networks", pClsid);
  258.  
  259.     return hr;
  260.  
  261. }  //*** RegisterCluAdminAllNetworksExtension()
  262.  
  263. /////////////////////////////////////////////////////////////////////////////
  264. //++
  265. //
  266. //    RegisterCluAdminAllNetInterfacesExtension
  267. //
  268. //    Routine Description:
  269. //        Register with the cluster database a Cluster Administrator Extension
  270. //        DLL that extends all network interfaces.
  271. //
  272. //    Arguments:
  273. //        hCluster        [IN] Handle to the cluster to modify.
  274. //        pClsid            [IN] Extension's CLSID.
  275. //
  276. //    Return Value:
  277. //        S_OK            Extension registered successfully.
  278. //        Win32 error code if another failure occurred.
  279. //
  280. //--
  281. /////////////////////////////////////////////////////////////////////////////
  282. STDAPI RegisterCluAdminAllNetInterfacesExtension(
  283.     IN HCLUSTER            hCluster,
  284.     IN const CLSID *    pClsid
  285.     )
  286. {
  287.     HRESULT        hr;
  288.  
  289.     hr = RegisterAnyCluAdminExtension(hCluster, L"NetInterfaces", pClsid);
  290.  
  291.     return hr;
  292.  
  293. }  //*** RegisterCluAdminAllNetInterfacesExtension()
  294.  
  295. /////////////////////////////////////////////////////////////////////////////
  296. //++
  297. //
  298. //    RegisterCluAdminResourceTypeExtension
  299. //
  300. //    Routine Description:
  301. //        Register with the cluster database a Cluster Administrator Extension
  302. //        DLL that extends resources of a specific type, or the resource type
  303. //        itself.
  304. //
  305. //    Arguments:
  306. //        hCluster            [IN] Handle to the cluster to modify.
  307. //        pwszResourceType    [IN] Resource type name.
  308. //        pClsid                [IN] Extension's CLSID.
  309. //
  310. //    Return Value:
  311. //        S_OK            Extension registered successfully.
  312. //        Win32 error code if another failure occurred.
  313. //
  314. //--
  315. /////////////////////////////////////////////////////////////////////////////
  316. STDAPI RegisterCluAdminResourceTypeExtension(
  317.     IN HCLUSTER            hCluster,
  318.     IN LPCWSTR            pwszResourceType,
  319.     IN const CLSID *    pClsid
  320.     )
  321. {
  322.     HRESULT        hr;
  323.     HKEY        hkey;
  324.  
  325.     // Get the resource type registry key.
  326.     hkey = GetClusterResourceTypeKey(hCluster, pwszResourceType, KEY_ALL_ACCESS);
  327.     if (hkey == NULL)
  328.         hr = GetLastError();
  329.     else
  330.     {
  331.         // Register the extension.
  332.         hr = RegisterAnyCluAdminExtension(hkey, pClsid);
  333.  
  334.         ClusterRegCloseKey(hkey);
  335.     }  // else:  GetClusterResourceTypeKey succeeded
  336.  
  337.     return hr;
  338.  
  339. }  //*** RegisterCluAdminResourceTypeExtension()
  340.  
  341. /////////////////////////////////////////////////////////////////////////////
  342. //++
  343. //
  344. //    UnregisterCluAdminClusterExtension
  345. //
  346. //    Routine Description:
  347. //        Unregister with the cluster database a Cluster Administrator Extension
  348. //        DLL that extends the cluster object.
  349. //
  350. //    Arguments:
  351. //        hCluster        [IN] Handle to the cluster to modify.
  352. //        pClsid            [IN] Extension's CLSID.
  353. //
  354. //    Return Value:
  355. //        S_OK            Extension registered successfully.
  356. //        Win32 error code if another failure occurred.
  357. //
  358. //--
  359. /////////////////////////////////////////////////////////////////////////////
  360. STDAPI UnregisterCluAdminClusterExtension(
  361.     IN HCLUSTER            hCluster,
  362.     IN const CLSID *    pClsid
  363.     )
  364. {
  365.     HRESULT        hr;
  366.     HKEY        hkey;
  367.  
  368.     // Get the cluster registry key.
  369.     hkey = GetClusterKey(hCluster, KEY_ALL_ACCESS);
  370.     if (hkey == NULL)
  371.         hr = GetLastError();
  372.     else
  373.     {
  374.         // Unregister the extension.
  375.         hr = UnregisterAnyCluAdminExtension(hkey, pClsid);
  376.  
  377.         ClusterRegCloseKey(hkey);
  378.     }  // else:  GetClusterKey succeeded
  379.  
  380.     return hr;
  381.  
  382. }  //*** UnregisterCluAdminClusterExtension()
  383.  
  384. /////////////////////////////////////////////////////////////////////////////
  385. //++
  386. //
  387. //    UnregisterCluAdminAllNodesExtension
  388. //
  389. //    Routine Description:
  390. //        Unregister with the cluster database a Cluster Administrator Extension
  391. //        DLL that extends all nodes.
  392. //
  393. //    Arguments:
  394. //        hCluster        [IN] Handle to the cluster to modify.
  395. //        pClsid            [IN] Extension's CLSID.
  396. //
  397. //    Return Value:
  398. //        S_OK            Extension unregistered successfully.
  399. //        Win32 error code if another failure occurred.
  400. //
  401. //--
  402. /////////////////////////////////////////////////////////////////////////////
  403. STDAPI UnregisterCluAdminAllNodesExtension(
  404.     IN HCLUSTER            hCluster,
  405.     IN const CLSID *    pClsid
  406.     )
  407. {
  408.     HRESULT        hr;
  409.  
  410.     hr = UnregisterAnyCluAdminExtension(hCluster, L"Nodes", pClsid);
  411.  
  412.     return hr;
  413.  
  414. }  //*** UnregisterCluAdminAllNodesExtension()
  415.  
  416. /////////////////////////////////////////////////////////////////////////////
  417. //++
  418. //
  419. //    UnregisterCluAdminAllGroupsExtension
  420. //
  421. //    Routine Description:
  422. //        Unregister with the cluster database a Cluster Administrator Extension
  423. //        DLL that extends all groups.
  424. //
  425. //    Arguments:
  426. //        hCluster        [IN] Handle to the cluster to modify.
  427. //        pClsid            [IN] Extension's CLSID.
  428. //
  429. //    Return Value:
  430. //        S_OK            Extension unregistered successfully.
  431. //        Win32 error code if another failure occurred.
  432. //
  433. //--
  434. /////////////////////////////////////////////////////////////////////////////
  435. STDAPI UnregisterCluAdminAllGroupsExtension(
  436.     IN HCLUSTER            hCluster,
  437.     IN const CLSID *    pClsid
  438.     )
  439. {
  440.     HRESULT        hr;
  441.  
  442.     hr = UnregisterAnyCluAdminExtension(hCluster, L"Groups", pClsid);
  443.  
  444.     return hr;
  445.  
  446. }  //*** UnregisterCluAdminAllGroupsExtension()
  447.  
  448. /////////////////////////////////////////////////////////////////////////////
  449. //++
  450. //
  451. //    UnregisterCluAdminAllResourcesExtension
  452. //
  453. //    Routine Description:
  454. //        Unregister with the cluster database a Cluster Administrator Extension
  455. //        DLL that extends all resources.
  456. //
  457. //    Arguments:
  458. //        hCluster        [IN] Handle to the cluster to modify.
  459. //        pClsid            [IN] Extension's CLSID.
  460. //
  461. //    Return Value:
  462. //        S_OK            Extension unregistered successfully.
  463. //        Win32 error code if another failure occurred.
  464. //
  465. //--
  466. /////////////////////////////////////////////////////////////////////////////
  467. STDAPI UnregisterCluAdminAllResourcesExtension(
  468.     IN HCLUSTER            hCluster,
  469.     IN const CLSID *    pClsid
  470.     )
  471. {
  472.     HRESULT        hr;
  473.  
  474.     hr = UnregisterAnyCluAdminExtension(hCluster, L"Resources", pClsid);
  475.  
  476.     return hr;
  477.  
  478. }  //*** UnregisterCluAdminAllResourcesExtension()
  479.  
  480. /////////////////////////////////////////////////////////////////////////////
  481. //++
  482. //
  483. //    UnregisterCluAdminAllResourceTypesExtension
  484. //
  485. //    Routine Description:
  486. //        Unregister with the cluster database a Cluster Administrator Extension
  487. //        DLL that extends all resource types.
  488. //
  489. //    Arguments:
  490. //        hCluster        [IN] Handle to the cluster to modify.
  491. //        pClsid            [IN] Extension's CLSID.
  492. //
  493. //    Return Value:
  494. //        S_OK            Extension unregistered successfully.
  495. //        Win32 error code if another failure occurred.
  496. //
  497. //--
  498. /////////////////////////////////////////////////////////////////////////////
  499. STDAPI UnregisterCluAdminAllResourceTypesExtension(
  500.     IN HCLUSTER            hCluster,
  501.     IN const CLSID *    pClsid
  502.     )
  503. {
  504.     HRESULT        hr;
  505.  
  506.     hr = UnregisterAnyCluAdminExtension(hCluster, L"ResourceTypes", pClsid);
  507.  
  508.     return hr;
  509.  
  510. }  //*** UnregisterCluAdminAllResourceTypesExtension()
  511.  
  512. /////////////////////////////////////////////////////////////////////////////
  513. //++
  514. //
  515. //    UnregisterCluAdminAllNetworksExtension
  516. //
  517. //    Routine Description:
  518. //        Unregister with the cluster database a Cluster Administrator Extension
  519. //        DLL that extends all networks.
  520. //
  521. //    Arguments:
  522. //        hCluster        [IN] Handle to the cluster to modify.
  523. //        pClsid            [IN] Extension's CLSID.
  524. //
  525. //    Return Value:
  526. //        S_OK            Extension unregistered successfully.
  527. //        Win32 error code if another failure occurred.
  528. //
  529. //--
  530. /////////////////////////////////////////////////////////////////////////////
  531. STDAPI UnregisterCluAdminAllNetworksExtension(
  532.     IN HCLUSTER            hCluster,
  533.     IN const CLSID *    pClsid
  534.     )
  535. {
  536.     HRESULT        hr;
  537.  
  538.     hr = UnregisterAnyCluAdminExtension(hCluster, L"Networks", pClsid);
  539.  
  540.     return hr;
  541.  
  542. }  //*** UnregisterCluAdminAllNetworksExtension()
  543.  
  544. /////////////////////////////////////////////////////////////////////////////
  545. //++
  546. //
  547. //    UnregisterCluAdminAllNetInterfacesExtension
  548. //
  549. //    Routine Description:
  550. //        Unregister with the cluster database a Cluster Administrator Extension
  551. //        DLL that extends all network interfaces.
  552. //
  553. //    Arguments:
  554. //        hCluster        [IN] Handle to the cluster to modify.
  555. //        pClsid            [IN] Extension's CLSID.
  556. //
  557. //    Return Value:
  558. //        S_OK            Extension unregistered successfully.
  559. //        Win32 error code if another failure occurred.
  560. //
  561. //--
  562. /////////////////////////////////////////////////////////////////////////////
  563. STDAPI UnregisterCluAdminAllNetInterfacesExtension(
  564.     IN HCLUSTER            hCluster,
  565.     IN const CLSID *    pClsid
  566.     )
  567. {
  568.     HRESULT        hr;
  569.  
  570.     hr = UnregisterAnyCluAdminExtension(hCluster, L"NetInterfaces", pClsid);
  571.  
  572.     return hr;
  573.  
  574. }  //*** UnregisterCluAdminAllNetInterfacesExtension()
  575.  
  576. /////////////////////////////////////////////////////////////////////////////
  577. //++
  578. //
  579. //    UnregisterCluAdminResourceTypeExtension
  580. //
  581. //    Routine Description:
  582. //        Unregister with the cluster database a Cluster Administrator Extension
  583. //        DLL that extends resources of a specific type, or the resource type
  584. //        itself.
  585. //
  586. //    Arguments:
  587. //        hCluster            [IN] Handle to the cluster to modify.
  588. //        pwszResourceType    [IN] Resource type name.
  589. //        pClsid                [IN] Extension's CLSID.
  590. //
  591. //    Return Value:
  592. //        S_OK            Extension unregistered successfully.
  593. //        Win32 error code if another failure occurred.
  594. //
  595. //--
  596. /////////////////////////////////////////////////////////////////////////////
  597. STDAPI UnregisterCluAdminResourceTypeExtension(
  598.     IN HCLUSTER            hCluster,
  599.     IN LPCWSTR            pwszResourceType,
  600.     IN const CLSID *    pClsid
  601.     )
  602. {
  603.     HRESULT        hr;
  604.     HKEY        hkey;
  605.  
  606.     // Get the resource type registry key.
  607.     hkey = GetClusterResourceTypeKey(hCluster, pwszResourceType, KEY_ALL_ACCESS);
  608.     if (hkey == NULL)
  609.         hr = GetLastError();
  610.     else
  611.     {
  612.         // Unregister the extension.
  613.         hr = UnregisterAnyCluAdminExtension(hkey, pClsid);
  614.  
  615.         ClusterRegCloseKey(hkey);
  616.     }  // else:  GetClusterResourceTypeKey succeeded
  617.  
  618.     return hr;
  619.  
  620. }  //*** UnregisterCluAdminResourceTypeExtension()
  621.  
  622. /////////////////////////////////////////////////////////////////////////////
  623. //++
  624. //
  625. //    RegisterAnyCluAdminExtension
  626. //
  627. //    Routine Description:
  628. //        Register any Cluster Administrator Extension DLL with the cluster
  629. //        database.
  630. //
  631. //    Arguments:
  632. //        hCluster        [IN] Handle to the cluster to modify.
  633. //        pwszKeyName        [IN] Key name.
  634. //        pClsid            [IN] Extension's CLSID.
  635. //
  636. //    Return Value:
  637. //        S_OK            Extension registered successfully.
  638. //        Win32 error code if another failure occurred.
  639. //
  640. //--
  641. /////////////////////////////////////////////////////////////////////////////
  642. static HRESULT RegisterAnyCluAdminExtension(
  643.     IN HCLUSTER            hCluster,
  644.     IN LPCWSTR            pwszKeyName,
  645.     IN const CLSID *    pClsid
  646.     )
  647. {
  648.     HRESULT        hr;
  649.     HKEY        hkeyCluster;
  650.     HKEY        hkey;
  651.  
  652.     // Get the cluster key.
  653.     hkeyCluster = GetClusterKey(hCluster, KEY_ALL_ACCESS);
  654.     if (hkeyCluster == NULL)
  655.         hr = GetLastError();
  656.     else
  657.     {
  658.         // Get the specified key.
  659.         hr = ClusterRegOpenKey(hkeyCluster, pwszKeyName, KEY_ALL_ACCESS, &hkey);
  660.         if (hr == ERROR_SUCCESS)
  661.         {
  662.             // Register the extension.
  663.             hr = RegisterAnyCluAdminExtension(hkey, pClsid);
  664.  
  665.             ClusterRegCloseKey(hkey);
  666.         }  // else:  GetClusterResourceTypeKey succeeded
  667.  
  668.         ClusterRegCloseKey(hkeyCluster);
  669.     }  // if:  CLSID converted to a string successfully
  670.  
  671.     return hr;
  672.  
  673. }  //*** RegisterAnyCluAdminExtension()
  674.  
  675. /////////////////////////////////////////////////////////////////////////////
  676. //++
  677. //
  678. //    RegisterAnyCluAdminExtension
  679. //
  680. //    Routine Description:
  681. //        Register any Cluster Administrator Extension DLL with the cluster
  682. //        database.
  683. //
  684. //    Arguments:
  685. //        hkey            [IN] Cluster database key.
  686. //        pClsid            [IN] Extension's CLSID.
  687. //
  688. //    Return Value:
  689. //        S_OK            Extension registered successfully.
  690. //        Win32 error code if another failure occurred.
  691. //
  692. //--
  693. /////////////////////////////////////////////////////////////////////////////
  694. static HRESULT RegisterAnyCluAdminExtension(
  695.     IN HKEY                hkey,
  696.     IN const CLSID *    pClsid
  697.     )
  698. {
  699.     HRESULT        hr;
  700.     LPOLESTR    pwszClsid;
  701.     DWORD        cbSize;
  702.     DWORD        cbNewSize;
  703.     LPWSTR        pwszValue;
  704.     LPWSTR        pwszNewValue;
  705.     BOOL        bAlreadyRegistered;
  706.  
  707.     // Convert the CLSID to a string.
  708.     hr = StringFromCLSID(*pClsid, &pwszClsid);
  709.     if (hr == S_OK)
  710.     {
  711.         // Read the current value.
  712.         hr = ReadValue(hkey, REG_VALUE_ADMIN_EXTENSIONS, &pwszValue, &cbSize);
  713.         if (hr == S_OK)
  714.         {
  715.             // Check to see if the extension has been registered yet.
  716.             if (pwszValue == NULL)
  717.                 bAlreadyRegistered = FALSE;
  718.             else
  719.             {
  720.                 LPCWSTR    pwszValueBuf = pwszValue;
  721.  
  722.                 while (*pwszValueBuf != L'\0')
  723.                 {
  724.                     if (lstrcmpiW(pwszClsid, pwszValueBuf) == 0)
  725.                         break;
  726.                     pwszValueBuf += lstrlenW(pwszValueBuf) + 1;
  727.                 }  // while:  more strings in the extension list
  728.                 bAlreadyRegistered = (*pwszValueBuf != L'\0');
  729.             }  // else:  extension value exists
  730.  
  731.             // Register the extension.
  732.             if (!bAlreadyRegistered)
  733.             {
  734.                 // Allocate a new buffer.
  735.                 cbNewSize = cbSize + (lstrlenW(pwszClsid) + 1) * sizeof(WCHAR);
  736.                 if (cbSize == 0) // Add size of final NULL if first entry.
  737.                     cbNewSize += sizeof(WCHAR);
  738.                 pwszNewValue = (LPWSTR) LocalAlloc(LMEM_FIXED, cbNewSize);
  739.                 if (pwszNewValue == NULL)
  740.                     hr = GetLastError();
  741.                 else
  742.                 {
  743.                     LPCWSTR    pwszValueBuf    = pwszValue;
  744.                     LPWSTR    pwszNewValueBuf    = pwszNewValue;
  745.                     DWORD    cch;
  746.                     DWORD    dwType;
  747.  
  748.                     // Copy the existing extensions to the new buffer.
  749.                     if (pwszValue != NULL)
  750.                     {
  751.                         while (*pwszValueBuf != L'\0')
  752.                         {
  753.                             lstrcpyW(pwszNewValueBuf, pwszValueBuf);
  754.                             cch = lstrlenW(pwszValueBuf);
  755.                             pwszValueBuf += cch + 1;
  756.                             pwszNewValueBuf += cch + 1;
  757.                         }  // while:  more strings in the extension list
  758.                     }  // if:  previous value buffer existed
  759.  
  760.                     // Add the new CLSID to the list.
  761.                     lstrcpyW(pwszNewValueBuf, pwszClsid);
  762.                     pwszNewValueBuf += lstrlenW(pwszClsid) + 1;
  763.                     *pwszNewValueBuf = L'\0';
  764.  
  765.                     // Write the value to the cluster database.
  766.                     dwType = REG_MULTI_SZ;
  767.                     hr = ClusterRegSetValue(
  768.                                     hkey,
  769.                                     REG_VALUE_ADMIN_EXTENSIONS,
  770.                                     dwType,
  771.                                     (LPBYTE) pwszNewValue,
  772.                                     cbNewSize
  773.                                     );
  774.  
  775.                     LocalFree(pwszNewValue);
  776.                 }  // else:  new buffer allocated successfully
  777.  
  778.             }  // if:  extension not registered yet
  779.  
  780.             LocalFree(pwszValue);
  781.         }  // if:  value read successfully
  782.  
  783.         CoTaskMemFree(pwszClsid);
  784.     }  // if:  CLSID converted to a string successfully
  785.  
  786.     return hr;
  787.  
  788. }  //*** RegisterAnyCluAdminExtension()
  789.  
  790. /////////////////////////////////////////////////////////////////////////////
  791. //++
  792. //
  793. //    UnregisterAnyCluAdminExtension
  794. //
  795. //    Routine Description:
  796. //        Unregister any Cluster Administrator Extension DLL with the cluster
  797. //        database.
  798. //
  799. //    Arguments:
  800. //        hCluster        [IN] Handle to the cluster to modify.
  801. //        pwszKeyName        [IN] Key name.
  802. //        pClsid            [IN] Extension's CLSID.
  803. //
  804. //    Return Value:
  805. //        S_OK            Extension unregistered successfully.
  806. //        Win32 error code if another failure occurred.
  807. //
  808. //--
  809. /////////////////////////////////////////////////////////////////////////////
  810. static HRESULT UnregisterAnyCluAdminExtension(
  811.     IN HCLUSTER            hCluster,
  812.     IN LPCWSTR            pwszKeyName,
  813.     IN const CLSID *    pClsid
  814.     )
  815. {
  816.     HRESULT        hr;
  817.     HKEY        hkeyCluster;
  818.     HKEY        hkey;
  819.  
  820.     // Get the cluster key.
  821.     hkeyCluster = GetClusterKey(hCluster, KEY_ALL_ACCESS);
  822.     if (hkeyCluster == NULL)
  823.         hr = GetLastError();
  824.     else
  825.     {
  826.         // Get the specified key.
  827.         hr = ClusterRegOpenKey(hkeyCluster, pwszKeyName, KEY_ALL_ACCESS, &hkey);
  828.         if (hr == ERROR_SUCCESS)
  829.         {
  830.             // Unregister the extension.
  831.             hr = UnregisterAnyCluAdminExtension(hkey, pClsid);
  832.  
  833.             ClusterRegCloseKey(hkey);
  834.         }  // else:  GetClusterResourceTypeKey succeeded
  835.  
  836.         ClusterRegCloseKey(hkeyCluster);
  837.     }  // if:  CLSID converted to a string successfully
  838.  
  839.     return hr;
  840.  
  841. }  //*** UnregisterAnyCluAdminExtension()
  842.  
  843. /////////////////////////////////////////////////////////////////////////////
  844. //++
  845. //
  846. //    UnregisterAnyCluAdminExtension
  847. //
  848. //    Routine Description:
  849. //        Unregister any Cluster Administrator Extension DLL with the cluster
  850. //        database.
  851. //
  852. //    Arguments:
  853. //        hkey            [IN] Cluster database key.
  854. //        pClsid            [IN] Extension's CLSID.
  855. //
  856. //    Return Value:
  857. //        S_OK            Extension unregistered successfully.
  858. //        Win32 error code if another failure occurred.
  859. //
  860. //--
  861. /////////////////////////////////////////////////////////////////////////////
  862. static HRESULT UnregisterAnyCluAdminExtension(
  863.     IN HKEY                hkey,
  864.     IN const CLSID *    pClsid
  865.     )
  866. {
  867.     HRESULT        hr;
  868.     LPOLESTR    pwszClsid;
  869.     DWORD        cbSize;
  870.     DWORD        cbNewSize;
  871.     LPWSTR        pwszValue;
  872.     LPWSTR        pwszNewValue;
  873.     BOOL        bAlreadyUnregistered;
  874.  
  875.     // Convert the CLSID to a string.
  876.     hr = StringFromCLSID(*pClsid, &pwszClsid);
  877.     if (hr == S_OK)
  878.     {
  879.         // Read the current value.
  880.         hr = ReadValue(hkey, REG_VALUE_ADMIN_EXTENSIONS, &pwszValue, &cbSize);
  881.         if (hr == S_OK)
  882.         {
  883.             // Check to see if the extension has been unregistered yet.
  884.             if (pwszValue == NULL)
  885.                 bAlreadyUnregistered = TRUE;
  886.             else
  887.             {
  888.                 LPCWSTR pwszValueBuf = pwszValue;
  889.  
  890.                 while (*pwszValueBuf != L'\0')
  891.                 {
  892.                     if (lstrcmpiW(pwszClsid, pwszValueBuf) == 0)
  893.                         break;
  894.                     pwszValueBuf += lstrlenW(pwszValueBuf) + 1;
  895.                 }  // while:  more strings in the extension list
  896.                 bAlreadyUnregistered = (*pwszValueBuf == L'\0');
  897.             }  // else:  extension value exists
  898.  
  899.             // Unregister the extension.
  900.             if (!bAlreadyUnregistered)
  901.             {
  902.                 // Allocate a new buffer.
  903.                 cbNewSize = cbSize - (lstrlenW(pwszClsid) + 1) * sizeof(WCHAR);
  904.                 if (cbNewSize == sizeof(WCHAR))
  905.                     cbNewSize = 0;
  906.                 pwszNewValue = (LPWSTR) LocalAlloc(LMEM_FIXED, cbNewSize);
  907.                 if (pwszNewValue == NULL)
  908.                     hr = GetLastError();
  909.                 else
  910.                 {
  911.                     LPCWSTR    pwszValueBuf    = pwszValue;
  912.                     LPWSTR    pwszNewValueBuf    = pwszNewValue;
  913.                     DWORD    dwType;
  914.  
  915.                     // Copy the existing extensions to the new buffer.
  916.                     if ((cbNewSize > 0) && (pwszValue != NULL))
  917.                     {
  918.                         while (*pwszValueBuf != L'\0')
  919.                         {
  920.                             if (lstrcmpiW(pwszClsid, pwszValueBuf) != 0)
  921.                             {
  922.                                 lstrcpyW(pwszNewValueBuf, pwszValueBuf);
  923.                                 pwszNewValueBuf += lstrlenW(pwszNewValueBuf) + 1;
  924.                             }  // if:  not CLSID being removed
  925.                             pwszValueBuf += lstrlenW(pwszValueBuf) + 1;
  926.                         }  // while:  more strings in the extension list
  927.                         *pwszNewValueBuf = L'\0';
  928.                     }  // if:  previous value buffer existed
  929.  
  930.                     // Write the value to the cluster database.
  931.                     dwType = REG_MULTI_SZ;
  932.                     hr = ClusterRegSetValue(
  933.                                     hkey,
  934.                                     REG_VALUE_ADMIN_EXTENSIONS,
  935.                                     dwType,
  936.                                     (LPBYTE) pwszNewValue,
  937.                                     cbNewSize
  938.                                     );
  939.  
  940.                     LocalFree(pwszNewValue);
  941.                 }  // else:  new buffer allocated successfully
  942.  
  943.             }  // if:  extension not unregistered yet
  944.  
  945.             LocalFree(pwszValue);
  946.         }  // if:  value read successfully
  947.  
  948.         CoTaskMemFree(pwszClsid);
  949.     }  // if:  CLSID converted to a string successfully
  950.  
  951.     return hr;
  952.  
  953. }  //*** UnregisterAnyCluAdminExtension()
  954.  
  955. /////////////////////////////////////////////////////////////////////////////
  956. //++
  957. //
  958. //    ReadValue
  959. //
  960. //    Routine Description:
  961. //        Reads a value from the cluster database.
  962. //
  963. //    Arguments:
  964. //        hkey            [IN] Handle for the key to read from.
  965. //        pwszValueName    [IN] Name of value to read.
  966. //        ppwszValue        [OUT] Address of pointer in which to return data.
  967. //                            The string is allocated using LocalAlloc and must
  968. //                            be deallocated by the calling LocalFree.
  969. //        pcbSize            [OUT] Size in bytes of the allocated value buffer.
  970. //
  971. //    Return Value:
  972. //        Any return values from ClusterRegQueryValue or errors from new.
  973. //
  974. //--
  975. /////////////////////////////////////////////////////////////////////////////
  976.  
  977. static DWORD ReadValue(
  978.     IN HKEY            hkey,
  979.     IN LPCWSTR        pwszValueName,
  980.     OUT LPWSTR *    ppwszValue,
  981.     OUT DWORD *        pcbSize
  982.     )
  983. {
  984.     DWORD        dwStatus;
  985.     DWORD        cbSize;
  986.     DWORD        dwType;
  987.     LPWSTR        pwszValue;
  988.  
  989.     *ppwszValue = NULL;
  990.     *pcbSize = 0;
  991.  
  992.     // Get the length of the value.
  993.     dwStatus = ClusterRegQueryValue(
  994.                     hkey,
  995.                     pwszValueName,
  996.                     &dwType,
  997.                     NULL,
  998.                     &cbSize
  999.                     );
  1000.     if (   (dwStatus != ERROR_SUCCESS)
  1001.         && (dwStatus != ERROR_MORE_DATA))
  1002.     {
  1003.         if (dwStatus  == ERROR_FILE_NOT_FOUND)
  1004.             dwStatus = ERROR_SUCCESS;
  1005.         return dwStatus;
  1006.     }  // if:  error occurred
  1007.  
  1008.     if (cbSize > 0)
  1009.     {
  1010.         // Allocate a value string.
  1011.         pwszValue = (LPWSTR) LocalAlloc(LMEM_FIXED, cbSize);
  1012.         if (pwszValue == NULL)
  1013.         {
  1014.             dwStatus = GetLastError();
  1015.             return dwStatus;
  1016.         }  // if:  error allocating memory
  1017.  
  1018.         // Read the the value.
  1019.         dwStatus = ClusterRegQueryValue(
  1020.                         hkey,
  1021.                         pwszValueName,
  1022.                         &dwType,
  1023.                         (LPBYTE) pwszValue,
  1024.                         &cbSize
  1025.                         );
  1026.         if (dwStatus != ERROR_SUCCESS)
  1027.         {
  1028.             LocalFree(pwszValue);
  1029.             pwszValue = NULL;
  1030.             cbSize = 0;
  1031.         }  // if:  error occurred
  1032.  
  1033.         *ppwszValue = pwszValue;
  1034.         *pcbSize = cbSize;
  1035.     }  // if:  value is not empty
  1036.  
  1037.     return dwStatus;
  1038.  
  1039. }  //*** ReadValue()
  1040.