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.

1388 lines
37 KiB

  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 1997-2002 Microsoft Corporation
  4. //
  5. // Module Name:
  6. // ClusNetW.cpp
  7. //
  8. // Description:
  9. // Implementation of the network classes for the MSCLUS
  10. // automation classes.
  11. //
  12. // Author:
  13. // Ramakrishna Rosanuru via David Potter (davidp) 5-Sep-1997
  14. // Galen Barbee (galenb) July 1998
  15. //
  16. // Revision History:
  17. // July 1998 GalenB Maaaaaajjjjjjjjjoooooorrrr clean up
  18. //
  19. // Notes:
  20. //
  21. /////////////////////////////////////////////////////////////////////////////
  22. #include "stdafx.h"
  23. #include "ClusterObject.h"
  24. #include "property.h"
  25. #include "clusneti.h"
  26. #include "clusnetw.h"
  27. /////////////////////////////////////////////////////////////////////////////
  28. // Global variables
  29. /////////////////////////////////////////////////////////////////////////////
  30. static const IID * iidCClusNetwork[] =
  31. {
  32. &IID_ISClusNetwork
  33. };
  34. static const IID * iidCClusNetworks[] =
  35. {
  36. &IID_ISClusNetworks
  37. };
  38. //*************************************************************************//
  39. /////////////////////////////////////////////////////////////////////////////
  40. // CClusNetwork class
  41. /////////////////////////////////////////////////////////////////////////////
  42. /////////////////////////////////////////////////////////////////////////////
  43. //++
  44. //
  45. // CClusNetwork::CClusterNetworkCClusterNetwork
  46. //
  47. // Description:
  48. // Constructor.
  49. //
  50. // Arguments:
  51. // None.
  52. //
  53. // Return Value:
  54. // None.
  55. //
  56. //--
  57. /////////////////////////////////////////////////////////////////////////////
  58. CClusNetwork::CClusNetwork( void )
  59. {
  60. m_hNetwork = NULL;
  61. m_pClusRefObject = NULL;
  62. m_pNetInterfaces = NULL;
  63. m_pCommonProperties = NULL;
  64. m_pPrivateProperties = NULL;
  65. m_pCommonROProperties = NULL;
  66. m_pPrivateROProperties = NULL;
  67. m_piids = (const IID *) iidCClusNetwork;
  68. m_piidsSize = ARRAYSIZE( iidCClusNetwork );
  69. } //*** CClusNetwork::CClusNetwork()
  70. /////////////////////////////////////////////////////////////////////////////
  71. //++
  72. //
  73. // CClusNetwork::~CClusNetwork
  74. //
  75. // Description:
  76. // Destructor.
  77. //
  78. // Arguments:
  79. // None.
  80. //
  81. // Return Value:
  82. // None.
  83. //
  84. //--
  85. /////////////////////////////////////////////////////////////////////////////
  86. CClusNetwork::~CClusNetwork( void )
  87. {
  88. if ( m_hNetwork != NULL )
  89. {
  90. CloseClusterNetwork( m_hNetwork );
  91. } // if:
  92. if ( m_pNetInterfaces != NULL )
  93. {
  94. m_pNetInterfaces->Release();
  95. m_pNetInterfaces = NULL;
  96. } // if:
  97. if ( m_pCommonProperties != NULL )
  98. {
  99. m_pCommonProperties->Release();
  100. m_pCommonProperties = NULL;
  101. } // if: release the property collection
  102. if ( m_pPrivateProperties != NULL )
  103. {
  104. m_pPrivateProperties->Release();
  105. m_pPrivateProperties = NULL;
  106. } // if: release the property collection
  107. if ( m_pCommonROProperties != NULL )
  108. {
  109. m_pCommonROProperties->Release();
  110. m_pCommonROProperties = NULL;
  111. } // if: release the property collection
  112. if ( m_pPrivateROProperties != NULL )
  113. {
  114. m_pPrivateROProperties->Release();
  115. m_pPrivateROProperties = NULL;
  116. } // if: release the property collection
  117. if ( m_pClusRefObject != NULL )
  118. {
  119. m_pClusRefObject->Release();
  120. m_pClusRefObject = NULL;
  121. } // if:
  122. } //*** CClusNetwork::~CClusNetwork()
  123. /////////////////////////////////////////////////////////////////////////////
  124. //++
  125. //
  126. // CClusNetwork::Open
  127. //
  128. // Description:
  129. // Open the passed in network.
  130. //
  131. // Arguments:
  132. // pClusRefObject [IN] - Wraps the cluster handle.
  133. // bstrNetworkName [IN] - The name of the interface to open.
  134. //
  135. // Return Value:
  136. // S_OK if successful, E_POINTER, or Win32 error as HRESULT.
  137. //
  138. //--
  139. /////////////////////////////////////////////////////////////////////////////
  140. HRESULT CClusNetwork::Open(
  141. IN ISClusRefObject * pClusRefObject,
  142. IN BSTR bstrNetworkName
  143. )
  144. {
  145. ASSERT( pClusRefObject != NULL );
  146. //ASSERT( bstrNetworkName != NULL );
  147. HRESULT _hr = E_POINTER;
  148. if ( ( pClusRefObject ) && ( bstrNetworkName != NULL ) )
  149. {
  150. HCLUSTER hCluster;
  151. m_pClusRefObject = pClusRefObject;
  152. m_pClusRefObject->AddRef();
  153. _hr = m_pClusRefObject->get_Handle((ULONG_PTR *) &hCluster);
  154. if ( SUCCEEDED( _hr ) )
  155. {
  156. m_hNetwork = OpenClusterNetwork( hCluster, bstrNetworkName );
  157. if ( m_hNetwork == NULL )
  158. {
  159. DWORD _sc = GetLastError();
  160. _hr = HRESULT_FROM_WIN32( _sc );
  161. }
  162. else
  163. {
  164. m_bstrNetworkName = bstrNetworkName;
  165. _hr = S_OK;
  166. } // else:
  167. } // if:
  168. } // if:
  169. return _hr;
  170. } //*** CClusNetwork::Open()
  171. /////////////////////////////////////////////////////////////////////////////
  172. //++
  173. //
  174. // CClusNetwork::GetProperties
  175. //
  176. // Description:
  177. // Creates a property collection for this object type (Network).
  178. //
  179. // Arguments:
  180. // ppProperties [OUT] - Catches the newly created collection.
  181. // bPrivate [IN] - Are these private properties? Or Common?
  182. // bReadOnly [IN] - Are these read only properties?
  183. //
  184. // Return Value:
  185. // S_OK if successful, or other HRESULT error.
  186. //
  187. //--
  188. /////////////////////////////////////////////////////////////////////////////
  189. HRESULT CClusNetwork::GetProperties(
  190. OUT ISClusProperties ** ppProperties,
  191. IN BOOL bPrivate,
  192. IN BOOL bReadOnly
  193. )
  194. {
  195. //ASSERT( ppProperties != NULL );
  196. HRESULT _hr = E_POINTER;
  197. if ( ppProperties != NULL )
  198. {
  199. CComObject< CClusProperties > * pProperties = NULL;
  200. *ppProperties = NULL;
  201. _hr = CComObject< CClusProperties >::CreateInstance( &pProperties );
  202. if ( SUCCEEDED( _hr ) )
  203. {
  204. CSmartPtr< CComObject< CClusProperties > > ptrProperties( pProperties );
  205. _hr = ptrProperties->Create( this, bPrivate, bReadOnly );
  206. if ( SUCCEEDED( _hr ) )
  207. {
  208. _hr = ptrProperties->Refresh();
  209. if ( SUCCEEDED( _hr ) )
  210. {
  211. _hr = ptrProperties->QueryInterface( IID_ISClusProperties, (void **) ppProperties );
  212. if ( SUCCEEDED( _hr ) )
  213. {
  214. ptrProperties->AddRef();
  215. if ( bPrivate )
  216. {
  217. if ( bReadOnly )
  218. {
  219. m_pPrivateROProperties = pProperties;
  220. }
  221. else
  222. {
  223. m_pPrivateProperties = pProperties;
  224. }
  225. }
  226. else
  227. {
  228. if ( bReadOnly )
  229. {
  230. m_pCommonROProperties = pProperties;
  231. }
  232. else
  233. {
  234. m_pCommonProperties = pProperties;
  235. }
  236. }
  237. }
  238. }
  239. }
  240. }
  241. }
  242. return _hr;
  243. } //*** CClusNetwork::GetProperties()
  244. /////////////////////////////////////////////////////////////////////////////
  245. //++
  246. //
  247. // CClusNetwork::get_Handle
  248. //
  249. // Description:
  250. // Return the raw handle to this objec (Network).
  251. //
  252. // Arguments:
  253. // phandle [OUT] - Catches the handle.
  254. //
  255. // Return Value:
  256. // S_OK if successful, or E_POINTER if not.
  257. //
  258. //--
  259. /////////////////////////////////////////////////////////////////////////////
  260. STDMETHODIMP CClusNetwork::get_Handle( OUT ULONG_PTR * phandle )
  261. {
  262. //ASSERT( phandle != NULL );
  263. HRESULT _hr = E_POINTER;
  264. if ( phandle != NULL )
  265. {
  266. *phandle = (ULONG_PTR) m_hNetwork;
  267. _hr = S_OK;
  268. }
  269. return _hr;
  270. } //*** CClusNetwork::get_Handle()
  271. /////////////////////////////////////////////////////////////////////////////
  272. //++
  273. //
  274. // CClusNetwork::put_Name
  275. //
  276. // Description:
  277. // Change the name of this object (Network).
  278. //
  279. // Arguments:
  280. // bstrNetworkName [IN] - The new name.
  281. //
  282. // Return Value:
  283. // S_OK if successful, E_POINTER, or other Win32 error as HRESULT.
  284. //
  285. //--
  286. /////////////////////////////////////////////////////////////////////////////
  287. STDMETHODIMP CClusNetwork::put_Name( IN BSTR bstrNetworkName )
  288. {
  289. //ASSERT( bstrNetworkName != NULL );
  290. HRESULT _hr = E_POINTER;
  291. if ( bstrNetworkName != NULL )
  292. {
  293. DWORD _sc = ERROR_SUCCESS;
  294. _sc = SetClusterNetworkName( m_hNetwork, bstrNetworkName );
  295. if ( _sc == ERROR_SUCCESS )
  296. {
  297. m_bstrNetworkName = bstrNetworkName;
  298. }
  299. _hr = HRESULT_FROM_WIN32( _sc );
  300. }
  301. return _hr;
  302. } //*** CClusNetwork::put_Name()
  303. /////////////////////////////////////////////////////////////////////////////
  304. //++
  305. //
  306. // CClusNetwork::get_Name
  307. //
  308. // Description:
  309. // Return the name of this object (Network).
  310. //
  311. // Arguments:
  312. // pbstrNetworkName [OUT] - Catches the name of this object.
  313. //
  314. // Return Value:
  315. // S_OK if successful, E_POINTER, or other HRESULT error.
  316. //
  317. //--
  318. /////////////////////////////////////////////////////////////////////////////
  319. STDMETHODIMP CClusNetwork::get_Name( OUT BSTR * pbstrNetworkName )
  320. {
  321. //ASSERT( pbstrNetworkName != NULL );
  322. HRESULT _hr = E_POINTER;
  323. if ( pbstrNetworkName != NULL )
  324. {
  325. *pbstrNetworkName = m_bstrNetworkName.Copy();
  326. _hr = S_OK;
  327. }
  328. return _hr;
  329. } //*** CClusNetwork::get_Name()
  330. /////////////////////////////////////////////////////////////////////////////
  331. //++
  332. //
  333. // CClusNetwork::get_NetworkID
  334. //
  335. // Description:
  336. // Get the network ID of this network.
  337. //
  338. // Arguments:
  339. // pbstrNetworkID [OUT] - Catches the network ID.
  340. //
  341. // Return Value:
  342. // S_OK if successful, E_POINTER, or Win32 error as HRESULT.
  343. //
  344. //--
  345. /////////////////////////////////////////////////////////////////////////////
  346. STDMETHODIMP CClusNetwork::get_NetworkID( OUT BSTR * pbstrNetworkID )
  347. {
  348. //ASSERT( pbstrNetworkID != NULL );
  349. HRESULT _hr = E_POINTER;
  350. if ( pbstrNetworkID != NULL )
  351. {
  352. WCHAR * pszNetworkID;
  353. DWORD dwBytes = 0;
  354. DWORD dwRet = ERROR_SUCCESS;
  355. dwRet = ::GetClusterNetworkId( m_hNetwork, NULL, &dwBytes );
  356. if ( SUCCEEDED( dwRet ) )
  357. {
  358. pszNetworkID = new WCHAR [ dwBytes + 1 ];
  359. if ( pszNetworkID != NULL )
  360. {
  361. dwRet = ::GetClusterNetworkId( m_hNetwork, pszNetworkID, &dwBytes );
  362. if ( SUCCEEDED( dwRet ) )
  363. {
  364. *pbstrNetworkID = ::SysAllocString( pszNetworkID );
  365. if ( *pbstrNetworkID == NULL )
  366. {
  367. _hr = E_OUTOFMEMORY;
  368. }
  369. else
  370. {
  371. _hr = S_OK;
  372. }
  373. }
  374. else
  375. {
  376. _hr = HRESULT_FROM_WIN32( dwRet );
  377. }
  378. delete [] pszNetworkID;
  379. }
  380. else
  381. {
  382. _hr = E_OUTOFMEMORY;
  383. }
  384. }
  385. else
  386. {
  387. _hr = HRESULT_FROM_WIN32( dwRet );
  388. }
  389. }
  390. return _hr;
  391. } //*** CClusNetwork::get_NetworkID()
  392. /////////////////////////////////////////////////////////////////////////////
  393. //++
  394. //
  395. // CClusNetwork::get_State
  396. //
  397. // Description:
  398. // Returns the current state of this object (Network).
  399. //
  400. // Arguments:
  401. // cnsState [OUT] - Catches the state.
  402. //
  403. // Return Value:
  404. // S_OK if successful, E_POINTER, or Win32 error as HRESULT.
  405. //
  406. //--
  407. /////////////////////////////////////////////////////////////////////////////
  408. STDMETHODIMP CClusNetwork::get_State( OUT CLUSTER_NETWORK_STATE * cnsState )
  409. {
  410. //ASSERT( cnsState != NULL );
  411. HRESULT _hr = E_POINTER;
  412. if ( cnsState != NULL )
  413. {
  414. CLUSTER_NETWORK_STATE _cns = ::GetClusterNetworkState( m_hNetwork );
  415. if ( _cns == ClusterNetworkStateUnknown )
  416. {
  417. DWORD _sc = ::GetLastError();
  418. _hr = HRESULT_FROM_WIN32( _sc );
  419. } // if: error
  420. else
  421. {
  422. *cnsState = _cns;
  423. _hr = S_OK;
  424. } // else: success
  425. } // if: args are not NULL
  426. return _hr;
  427. } //*** CClusNetwork::get_State()
  428. /////////////////////////////////////////////////////////////////////////////
  429. //++
  430. //
  431. // CClusNetwork::get_CommonProperties
  432. //
  433. // Description:
  434. // Get this object's (Network) common properties collection.
  435. //
  436. // Arguments:
  437. // ppProperties [OUT] - Catches the properties collection.
  438. //
  439. // Return Value:
  440. // S_OK if successful, or other HRESULT error.
  441. //
  442. //--
  443. /////////////////////////////////////////////////////////////////////////////
  444. STDMETHODIMP CClusNetwork::get_CommonProperties(
  445. OUT ISClusProperties ** ppProperties
  446. )
  447. {
  448. //ASSERT( ppProperties != NULL );
  449. HRESULT _hr = E_POINTER;
  450. if ( ppProperties != NULL )
  451. {
  452. if ( m_pCommonProperties )
  453. {
  454. _hr = m_pCommonProperties->QueryInterface( IID_ISClusProperties, (void **) ppProperties );
  455. }
  456. else
  457. {
  458. _hr = GetProperties( ppProperties, FALSE, FALSE );
  459. }
  460. }
  461. return _hr;
  462. } //*** CClusNetwork::get_CommonProperties()
  463. /////////////////////////////////////////////////////////////////////////////
  464. //++
  465. //
  466. // CClusNetwork::get_PrivateProperties
  467. //
  468. // Description:
  469. // Get this object's (Network) private properties collection.
  470. //
  471. // Arguments:
  472. // ppProperties [OUT] - Catches the properties collection.
  473. //
  474. // Return Value:
  475. // S_OK if successful, or other HRESULT error.
  476. //
  477. //--
  478. /////////////////////////////////////////////////////////////////////////////
  479. STDMETHODIMP CClusNetwork::get_PrivateProperties(
  480. OUT ISClusProperties ** ppProperties
  481. )
  482. {
  483. //ASSERT( ppProperties != NULL );
  484. HRESULT _hr = E_POINTER;
  485. if ( ppProperties != NULL )
  486. {
  487. if ( m_pPrivateProperties )
  488. {
  489. _hr = m_pPrivateProperties->QueryInterface( IID_ISClusProperties, (void **) ppProperties );
  490. }
  491. else
  492. {
  493. _hr = GetProperties( ppProperties, TRUE, FALSE );
  494. }
  495. }
  496. return _hr;
  497. } //*** CClusNetwork::get_PrivateProperties()
  498. /////////////////////////////////////////////////////////////////////////////
  499. //++
  500. //
  501. // CClusNetwork::get_CommonROProperties
  502. //
  503. // Description:
  504. // Get this object's (Network) common read only properties collection.
  505. //
  506. // Arguments:
  507. // ppProperties [OUT] - Catches the properties collection.
  508. //
  509. // Return Value:
  510. // S_OK if successful, or other HRESULT error.
  511. //
  512. //--
  513. /////////////////////////////////////////////////////////////////////////////
  514. STDMETHODIMP CClusNetwork::get_CommonROProperties(
  515. OUT ISClusProperties ** ppProperties
  516. )
  517. {
  518. //ASSERT( ppProperties != NULL );
  519. HRESULT _hr = E_POINTER;
  520. if ( ppProperties != NULL )
  521. {
  522. if ( m_pCommonROProperties )
  523. {
  524. _hr = m_pCommonROProperties->QueryInterface( IID_ISClusProperties, (void **) ppProperties );
  525. }
  526. else
  527. {
  528. _hr = GetProperties( ppProperties, FALSE, TRUE );
  529. }
  530. }
  531. return _hr;
  532. } //*** CClusNetwork::get_CommonROProperties()
  533. /////////////////////////////////////////////////////////////////////////////
  534. //++
  535. //
  536. // CClusNetwork::get_PrivateROProperties
  537. //
  538. // Description:
  539. // Get this object's (Network) private read only properties collection.
  540. //
  541. // Arguments:
  542. // ppProperties [OUT] - Catches the properties collection.
  543. //
  544. // Return Value:
  545. // S_OK if successful, or other HRESULT error.
  546. //
  547. //--
  548. /////////////////////////////////////////////////////////////////////////////
  549. STDMETHODIMP CClusNetwork::get_PrivateROProperties(
  550. ISClusProperties ** ppProperties
  551. )
  552. {
  553. //ASSERT( ppProperties != NULL );
  554. HRESULT _hr = E_POINTER;
  555. if ( ppProperties != NULL )
  556. {
  557. if ( m_pPrivateROProperties )
  558. {
  559. _hr = m_pPrivateROProperties->QueryInterface( IID_ISClusProperties, (void **) ppProperties );
  560. }
  561. else
  562. {
  563. _hr = GetProperties( ppProperties, TRUE, TRUE );
  564. }
  565. }
  566. return _hr;
  567. } //*** CClusNetwork::get_PrivateROProperties()
  568. /////////////////////////////////////////////////////////////////////////////
  569. //++
  570. //
  571. // CClusNetwork::get_NetInterfaces
  572. //
  573. // Description:
  574. // Creates a collection of netinterfaces for this network.
  575. //
  576. // Arguments:
  577. // ppNetInterfaces [OUT] - Catches the collection.
  578. //
  579. // Return Value:
  580. // S_OK if successful, E_POINTER, or Win32 error as HRESULT.
  581. //
  582. //--
  583. /////////////////////////////////////////////////////////////////////////////
  584. STDMETHODIMP CClusNetwork::get_NetInterfaces(
  585. OUT ISClusNetworkNetInterfaces ** ppNetInterfaces
  586. )
  587. {
  588. return ::HrCreateResourceCollection< CClusNetworkNetInterfaces, ISClusNetworkNetInterfaces, HNETWORK >(
  589. &m_pNetInterfaces,
  590. m_hNetwork,
  591. ppNetInterfaces,
  592. IID_ISClusNetworkNetInterfaces,
  593. m_pClusRefObject
  594. );
  595. } //*** CClusNetwork::get_NetInterfaces()
  596. /////////////////////////////////////////////////////////////////////////////
  597. //++
  598. //
  599. // CClusNetwork::get_Cluster
  600. //
  601. // Description:
  602. // Return the cluster this object (Network) belongs to.
  603. //
  604. // Arguments:
  605. // ppCluster [OUT] - Catches the cluster.
  606. //
  607. // Return Value:
  608. // S_OK if successful, or other HRESULT error.
  609. //
  610. //--
  611. /////////////////////////////////////////////////////////////////////////////
  612. STDMETHODIMP CClusNetwork::get_Cluster(
  613. ISCluster ** ppCluster
  614. )
  615. {
  616. return ::HrGetCluster( ppCluster, m_pClusRefObject );
  617. } //*** CClusNetwork::get_Cluster()
  618. /////////////////////////////////////////////////////////////////////////////
  619. //++
  620. //
  621. // CClusNetwork::HrLoadProperties
  622. //
  623. // Description:
  624. // This virtual function does the actual load of the property list from
  625. // the cluster.
  626. //
  627. // Arguments:
  628. // rcplPropList [IN OUT] - The property list to load.
  629. // bReadOnly [IN] - Load the read only properties?
  630. // bPrivate [IN] - Load the common or the private properties?
  631. //
  632. // Return Value:
  633. // S_OK if successful, or other HRESULT error.
  634. //
  635. //--
  636. /////////////////////////////////////////////////////////////////////////////
  637. HRESULT CClusNetwork::HrLoadProperties(
  638. IN OUT CClusPropList & rcplPropList,
  639. IN BOOL bReadOnly,
  640. IN BOOL bPrivate
  641. )
  642. {
  643. HRESULT _hr = S_FALSE;
  644. DWORD _dwControlCode = 0;
  645. DWORD _sc = ERROR_SUCCESS;
  646. if ( bReadOnly )
  647. {
  648. _dwControlCode = bPrivate
  649. ? CLUSCTL_NETWORK_GET_RO_PRIVATE_PROPERTIES
  650. : CLUSCTL_NETWORK_GET_RO_COMMON_PROPERTIES;
  651. }
  652. else
  653. {
  654. _dwControlCode = bPrivate
  655. ? CLUSCTL_NETWORK_GET_PRIVATE_PROPERTIES
  656. : CLUSCTL_NETWORK_GET_COMMON_PROPERTIES;
  657. }
  658. _sc = rcplPropList.ScGetNetworkProperties( m_hNetwork, _dwControlCode );
  659. _hr = HRESULT_FROM_WIN32( _sc );
  660. return _hr;
  661. } //*** CClusNetwork::HrLoadProperties()
  662. /////////////////////////////////////////////////////////////////////////////
  663. //++
  664. //
  665. // CClusNetwork::ScWriteProperties
  666. //
  667. // Description:
  668. // This virtual function does the actual saving of the property list to
  669. // the cluster.
  670. //
  671. // Arguments:
  672. // rcplPropList [IN] - The property list to save.
  673. // bPrivate [IN] - Save the common or the private properties?
  674. //
  675. // Return Value:
  676. // ERROR_SUCCESS if successful, or other Win32 error if not.
  677. //
  678. //--
  679. /////////////////////////////////////////////////////////////////////////////
  680. DWORD CClusNetwork::ScWriteProperties(
  681. const CClusPropList & rcplPropList,
  682. BOOL bPrivate
  683. )
  684. {
  685. DWORD dwControlCode = bPrivate ? CLUSCTL_NETWORK_SET_PRIVATE_PROPERTIES : CLUSCTL_NETWORK_SET_COMMON_PROPERTIES;
  686. DWORD nBytesReturned = 0;
  687. DWORD _sc = ERROR_SUCCESS;
  688. _sc = ClusterNetworkControl(
  689. m_hNetwork,
  690. NULL,
  691. dwControlCode,
  692. rcplPropList,
  693. rcplPropList.CbBufferSize(),
  694. 0,
  695. 0,
  696. &nBytesReturned
  697. );
  698. return _sc;
  699. } //*** CClusNetwork::ScWriteProperties()
  700. //*************************************************************************//
  701. /////////////////////////////////////////////////////////////////////////////
  702. // CClusNetworks class
  703. /////////////////////////////////////////////////////////////////////////////
  704. /////////////////////////////////////////////////////////////////////////////
  705. //++
  706. //
  707. // CClusNetworks::CClusNetworks
  708. //
  709. // Description:
  710. // Constructor.
  711. //
  712. // Arguments:
  713. // None.
  714. //
  715. // Return Value:
  716. // None.
  717. //
  718. //--
  719. /////////////////////////////////////////////////////////////////////////////
  720. CClusNetworks::CClusNetworks( void )
  721. {
  722. m_pClusRefObject = NULL;
  723. m_piids = (const IID *) iidCClusNetworks;
  724. m_piidsSize = ARRAYSIZE( iidCClusNetworks );
  725. } //*** CClusNetworks::CClusNetworks()
  726. /////////////////////////////////////////////////////////////////////////////
  727. //++
  728. //
  729. // CClusNetworks::~CClusNetworks
  730. //
  731. // Description:
  732. // Destructor.
  733. //
  734. // Arguments:
  735. // None.
  736. //
  737. // Return Value:
  738. // None.
  739. //
  740. //--
  741. /////////////////////////////////////////////////////////////////////////////
  742. CClusNetworks::~CClusNetworks( void )
  743. {
  744. Clear();
  745. if ( m_pClusRefObject != NULL )
  746. {
  747. m_pClusRefObject->Release();
  748. m_pClusRefObject = NULL;
  749. }
  750. } //*** CClusNetworks::~CClusNetworks()
  751. /////////////////////////////////////////////////////////////////////////////
  752. //++
  753. //
  754. // CClusNetworks::Create
  755. //
  756. // Description:
  757. // Finish the heavy weight construction.
  758. //
  759. // Arguments:
  760. // pClusRefObject [IN] - Wraps the cluster handle.
  761. //
  762. // Return Value:
  763. // S_OK if successful, E_POINTER if not.
  764. //
  765. //--
  766. /////////////////////////////////////////////////////////////////////////////
  767. HRESULT CClusNetworks::Create( IN ISClusRefObject * pClusRefObject )
  768. {
  769. ASSERT( pClusRefObject != NULL );
  770. HRESULT _hr = E_POINTER;
  771. if ( pClusRefObject != NULL )
  772. {
  773. m_pClusRefObject = pClusRefObject;
  774. m_pClusRefObject->AddRef();
  775. _hr = S_OK;
  776. }
  777. return _hr;
  778. } //*** CClusNetworks::Create()
  779. /////////////////////////////////////////////////////////////////////////////
  780. //++
  781. //
  782. // CClusNetworks::get_Count
  783. //
  784. // Description:
  785. // Return the count of objects (Networks) in the collection.
  786. //
  787. // Arguments:
  788. // plCount [OUT] - Catches the count.
  789. //
  790. // Return Value:
  791. // S_OK if successful, or E_POINTER.
  792. //
  793. //--
  794. /////////////////////////////////////////////////////////////////////////////
  795. STDMETHODIMP CClusNetworks::get_Count( OUT long * plCount )
  796. {
  797. //ASSERT( plCount != NULL );
  798. HRESULT _hr = E_POINTER;
  799. if ( plCount != NULL )
  800. {
  801. *plCount = m_NetworkList.size();
  802. _hr = S_OK;
  803. } // if: args are not NULL
  804. return _hr;
  805. } //*** CClusNetworks::get_Count()
  806. /////////////////////////////////////////////////////////////////////////////
  807. //++
  808. //
  809. // CClusNetworks::get__NewEnum
  810. //
  811. // Description:
  812. // Create and return a new enumeration for this collection.
  813. //
  814. // Arguments:
  815. // ppunk [OUT] - Catches the new enumeration.
  816. //
  817. // Return Value:
  818. // S_OK if successful, E_POINTER, or other HRESULT error.
  819. //
  820. //--
  821. /////////////////////////////////////////////////////////////////////////////
  822. STDMETHODIMP CClusNetworks::get__NewEnum( IUnknown ** punk )
  823. {
  824. return ::HrNewIDispatchEnum< NetworkList, CComObject< CClusNetwork > >( punk, m_NetworkList );
  825. } //*** CClusNetworks::get__NewEnum()
  826. /////////////////////////////////////////////////////////////////////////////
  827. //++
  828. //
  829. // CClusNetworks::Refresh
  830. //
  831. // Description:
  832. // Load the collection from the cluster database.
  833. //
  834. // Arguments:
  835. // None.
  836. //
  837. // Return Value:
  838. // S_OK if successful, E_POINTER, or Win32 error as HRESULT.
  839. //
  840. //--
  841. /////////////////////////////////////////////////////////////////////////////
  842. STDMETHODIMP CClusNetworks::Refresh( void )
  843. {
  844. HRESULT _hr = E_POINTER;
  845. DWORD _sc = ERROR_SUCCESS;
  846. HCLUSENUM hEnum = NULL;
  847. HCLUSTER hCluster = NULL;
  848. ASSERT( m_pClusRefObject != NULL );
  849. _hr = m_pClusRefObject->get_Handle( (ULONG_PTR *) &hCluster );
  850. if ( SUCCEEDED( _hr ) )
  851. {
  852. hEnum = ::ClusterOpenEnum( hCluster, CLUSTER_ENUM_NETWORK );
  853. if ( hEnum != NULL )
  854. {
  855. int _nIndex = 0;
  856. DWORD dwType = 0;
  857. LPWSTR pszName = NULL;
  858. CComObject< CClusNetwork > * pNetwork = NULL;
  859. Clear();
  860. for( _nIndex = 0, _hr = S_OK; SUCCEEDED( _hr ); _nIndex++ )
  861. {
  862. _sc = ::WrapClusterEnum( hEnum, _nIndex, &dwType, &pszName );
  863. if ( _sc == ERROR_NO_MORE_ITEMS )
  864. {
  865. _hr = S_OK;
  866. break;
  867. }
  868. else if ( _sc == ERROR_SUCCESS )
  869. {
  870. _hr = CComObject< CClusNetwork >::CreateInstance( &pNetwork );
  871. if ( SUCCEEDED( _hr ) )
  872. {
  873. CSmartPtr< ISClusRefObject > ptrRefObject( m_pClusRefObject );
  874. CSmartPtr< CComObject< CClusNetwork > > ptrNetwork( pNetwork );
  875. BSTR _bstr = NULL;
  876. _bstr = SysAllocString( pszName );
  877. if ( _bstr == NULL )
  878. {
  879. _hr = E_OUTOFMEMORY;
  880. }
  881. else
  882. {
  883. _hr = ptrNetwork->Open( ptrRefObject, _bstr );
  884. if ( SUCCEEDED( _hr ) )
  885. {
  886. ptrNetwork->AddRef();
  887. m_NetworkList.insert( m_NetworkList.end(), ptrNetwork );
  888. }
  889. else if ( HRESULT_CODE( _hr ) == ERROR_CLUSTER_NETWORK_NOT_FOUND )
  890. {
  891. //
  892. // It is possible for the network to have been deleted from the cluster
  893. // in the time between creating the enum and opening the network. When
  894. // that happens we need to simply skip that network and continue
  895. // enumerating.
  896. //
  897. _hr = S_FALSE; // success code to keep us in the loop
  898. } // else if: the cluster network was not found
  899. SysFreeString( _bstr );
  900. }
  901. }
  902. ::LocalFree( pszName );
  903. pszName = NULL;
  904. }
  905. else
  906. {
  907. _hr = HRESULT_FROM_WIN32( _sc );
  908. }
  909. }
  910. ::ClusterCloseEnum( hEnum );
  911. }
  912. else
  913. {
  914. _sc = GetLastError();
  915. _hr = HRESULT_FROM_WIN32( _sc );
  916. }
  917. }
  918. return _hr;
  919. } //*** CClusNetworks::Refresh()
  920. /////////////////////////////////////////////////////////////////////////////
  921. //++
  922. //
  923. // CClusNetworks::Clear
  924. //
  925. // Description:
  926. // Empty the collection of networks.
  927. //
  928. // Arguments:
  929. // None.
  930. //
  931. // Return Value:
  932. // None.
  933. //
  934. //--
  935. /////////////////////////////////////////////////////////////////////////////
  936. void CClusNetworks::Clear( void )
  937. {
  938. ::ReleaseAndEmptyCollection< NetworkList, CComObject< CClusNetwork > >( m_NetworkList );
  939. } //*** CClusNetworks::Clear()
  940. /////////////////////////////////////////////////////////////////////////////
  941. //++
  942. //
  943. // CClusNetworks::FindItem
  944. //
  945. // Description:
  946. // Find a network in the collection by name and return its index.
  947. //
  948. // Arguments:
  949. // lpszNetworkName [IN] - The name to look for.
  950. // pnIndex [OUT] - Catches the index.
  951. //
  952. // Return Value:
  953. // S_OK if successful, E_POINTER, or E_INVALIDARG.
  954. //
  955. //--
  956. /////////////////////////////////////////////////////////////////////////////
  957. HRESULT CClusNetworks::FindItem(
  958. IN LPWSTR lpszNetworkName,
  959. OUT UINT * pnIndex
  960. )
  961. {
  962. //ASSERT( lpszNetworkName != NULL );
  963. //ASSERT( pnIndex != NULL );
  964. HRESULT _hr = E_POINTER;
  965. if ( ( lpszNetworkName != NULL ) && ( pnIndex != NULL ) )
  966. {
  967. CComObject< CClusNetwork > * pNetwork = NULL;
  968. NetworkList::iterator first = m_NetworkList.begin();
  969. NetworkList::iterator last = m_NetworkList.end();
  970. UINT iIndex = 0;
  971. _hr = E_INVALIDARG;
  972. for ( ; first != last; first++, iIndex++ )
  973. {
  974. pNetwork = *first;
  975. if ( pNetwork && ( lstrcmpi( lpszNetworkName, pNetwork->Name() ) == 0 ) )
  976. {
  977. *pnIndex = iIndex;
  978. _hr = S_OK;
  979. break;
  980. }
  981. }
  982. } // if: args are not NULL
  983. return _hr;
  984. } //*** CClusNetworks::FindItem( lpszNetworkName )
  985. /////////////////////////////////////////////////////////////////////////////
  986. //++
  987. //
  988. // CClusNetworks::FindItem
  989. //
  990. // Description:
  991. // Find a network in the collection and return its index.
  992. //
  993. // Arguments:
  994. // pClusterNetwork [IN] - The network to look for.
  995. // pnIndex [OUT] - Catches the index.
  996. //
  997. // Return Value:
  998. // S_OK if successful, E_POINTER, or E_INVALIDARG.
  999. //
  1000. //--
  1001. /////////////////////////////////////////////////////////////////////////////
  1002. HRESULT CClusNetworks::FindItem(
  1003. IN ISClusNetwork * pClusterNetwork,
  1004. OUT UINT * pnIndex
  1005. )
  1006. {
  1007. //ASSERT( pClusterNetwork != NULL );
  1008. //ASSERT( pnIndex != NULL );
  1009. HRESULT _hr = E_POINTER;
  1010. if ( ( pClusterNetwork != NULL ) && ( pnIndex != NULL ) )
  1011. {
  1012. CComBSTR bstrName;
  1013. _hr = pClusterNetwork->get_Name( &bstrName );
  1014. if ( SUCCEEDED( _hr ) )
  1015. {
  1016. _hr = FindItem( bstrName, pnIndex );
  1017. }
  1018. }
  1019. return _hr;
  1020. } //*** CClusNetworks::FindItem( pClusterNetwork )
  1021. /////////////////////////////////////////////////////////////////////////////
  1022. //++
  1023. //
  1024. // CClusNetworks::GetIndex
  1025. //
  1026. // Description:
  1027. // Convert the passed in variant index into the real index in the
  1028. // collection.
  1029. //
  1030. // Arguments:
  1031. // varIndex [IN] - The index to convert.
  1032. // pnIndex [OUT] - Catches the index.
  1033. //
  1034. // Return Value:
  1035. // S_OK if successful, E_POINTER, or E_INVALIDARG.
  1036. //
  1037. //--
  1038. /////////////////////////////////////////////////////////////////////////////
  1039. HRESULT CClusNetworks::GetIndex(
  1040. IN VARIANT varIndex,
  1041. OUT UINT * pnIndex
  1042. )
  1043. {
  1044. //ASSERT( pnIndex != NULL );
  1045. HRESULT _hr = E_POINTER;
  1046. UINT nIndex = 0;
  1047. if ( pnIndex != NULL )
  1048. {
  1049. CComVariant v;
  1050. *pnIndex = 0;
  1051. v.Copy( &varIndex );
  1052. // Check to see if the index is a number.
  1053. _hr = v.ChangeType( VT_I4 );
  1054. if ( SUCCEEDED( _hr ) )
  1055. {
  1056. nIndex = v.lVal;
  1057. nIndex--; // Adjust index to be 0 relative instead of 1 relative
  1058. }
  1059. else
  1060. {
  1061. // Check to see if the index is a string.
  1062. _hr = v.ChangeType( VT_BSTR );
  1063. if ( SUCCEEDED( _hr ) )
  1064. {
  1065. // Search for the string.
  1066. _hr = FindItem( v.bstrVal, &nIndex );
  1067. }
  1068. }
  1069. // We found an index, now check the range.
  1070. if ( SUCCEEDED( _hr ) )
  1071. {
  1072. if ( nIndex < m_NetworkList.size() )
  1073. {
  1074. *pnIndex = nIndex;
  1075. }
  1076. else
  1077. {
  1078. _hr = E_INVALIDARG;
  1079. }
  1080. }
  1081. }
  1082. return _hr;
  1083. } //*** CClusNetworks::GetIndex()
  1084. /////////////////////////////////////////////////////////////////////////////
  1085. //++
  1086. //
  1087. // CClusNetworks::GetItem
  1088. //
  1089. // Description:
  1090. // Return the item (Network) by name.
  1091. //
  1092. // Arguments:
  1093. // lpszNetworkName [IN] - The name of the item requested.
  1094. // ppClusterNetInterface [OUT] - Catches the item.
  1095. //
  1096. // Return Value:
  1097. // S_OK if successful, E_POINTER, or E_INVALIDARG.
  1098. //
  1099. //--
  1100. /////////////////////////////////////////////////////////////////////////////
  1101. HRESULT CClusNetworks::GetItem(
  1102. IN LPWSTR lpszNetworkName,
  1103. OUT ISClusNetwork ** ppClusterNetwork
  1104. )
  1105. {
  1106. //ASSERT( lpszNetworkName != NULL );
  1107. //ASSERT( ppClusterNetwork != NULL );
  1108. HRESULT _hr = E_POINTER;
  1109. if ( ( lpszNetworkName != NULL ) && ( ppClusterNetwork != NULL ) )
  1110. {
  1111. CComObject<CClusNetwork> * pNetwork = NULL;
  1112. NetworkList::iterator first = m_NetworkList.begin();
  1113. NetworkList::iterator last = m_NetworkList.end();
  1114. while ( first != last )
  1115. {
  1116. pNetwork = *first;
  1117. if ( lstrcmpi( lpszNetworkName, pNetwork->Name() ) == 0 )
  1118. {
  1119. _hr = pNetwork->QueryInterface( IID_ISClusNetwork, (void **) ppClusterNetwork );
  1120. break;
  1121. }
  1122. first++;
  1123. }
  1124. }
  1125. return _hr;
  1126. } //*** CClusNetworks::GetItem( lpszNetworkName )
  1127. /////////////////////////////////////////////////////////////////////////////
  1128. //++
  1129. //
  1130. // CClusNetworks::GetItem
  1131. //
  1132. // Description:
  1133. // Return the item (Network) by index.
  1134. //
  1135. // Arguments:
  1136. // nIndex [IN] - The index of the item requested.
  1137. // ppClusterNetInterface [OUT] - Catches the item.
  1138. //
  1139. // Return Value:
  1140. // S_OK if successful, E_POINTER, or E_INVALIDARG.
  1141. //
  1142. //--
  1143. /////////////////////////////////////////////////////////////////////////////
  1144. HRESULT CClusNetworks::GetItem(
  1145. IN UINT nIndex,
  1146. OUT ISClusNetwork ** ppClusterNetwork
  1147. )
  1148. {
  1149. //ASSERT( ppClusterNetwork != NULL );
  1150. HRESULT _hr = E_POINTER;
  1151. if ( ppClusterNetwork != NULL )
  1152. {
  1153. // Automation collections are 1-relative for languages like VB.
  1154. // We are 0-relative internally.
  1155. nIndex--;
  1156. if ( nIndex < m_NetworkList.size() )
  1157. {
  1158. CComObject< CClusNetwork > * pNetwork = m_NetworkList[ nIndex ];
  1159. _hr = pNetwork->QueryInterface( IID_ISClusNetwork, (void **) ppClusterNetwork );
  1160. } // if: index is in range
  1161. else
  1162. {
  1163. _hr = E_INVALIDARG;
  1164. } // else: index is out of range
  1165. }
  1166. return _hr;
  1167. } //*** CClusNetworks::GetItem( nIndex )
  1168. /////////////////////////////////////////////////////////////////////////////
  1169. //++
  1170. //
  1171. // CClusNetworks::get_Item
  1172. //
  1173. // Description:
  1174. // Return the object (Network) at the passed in index.
  1175. //
  1176. // Arguments:
  1177. // varIndex [IN] - Contains the index requested.
  1178. // ppClusterNetwork [OUT] - Catches the item.
  1179. //
  1180. // Return Value:
  1181. // S_OK if successful, E_POINTER, or E_INVALIDARG.
  1182. //
  1183. //--
  1184. /////////////////////////////////////////////////////////////////////////////
  1185. STDMETHODIMP CClusNetworks::get_Item(
  1186. IN VARIANT varIndex,
  1187. OUT ISClusNetwork ** ppClusterNetwork
  1188. )
  1189. {
  1190. //ASSERT( ppClusterNetwork != NULL );
  1191. HRESULT _hr = E_POINTER;
  1192. if ( ppClusterNetwork != NULL )
  1193. {
  1194. CComObject<CClusNetwork> * pNetwork = NULL;
  1195. UINT nIndex = 0;
  1196. // Zero the out param
  1197. *ppClusterNetwork = 0;
  1198. _hr = GetIndex( varIndex, &nIndex );
  1199. if ( SUCCEEDED( _hr ) )
  1200. {
  1201. pNetwork = m_NetworkList[ nIndex ];
  1202. _hr = pNetwork->QueryInterface( IID_ISClusNetwork, (void **) ppClusterNetwork );
  1203. }
  1204. }
  1205. return _hr;
  1206. } //*** CClusNetworks::get_Item()