Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1368 lines
35 KiB

  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 1997-2000 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. if ( m_pClusRefObject != NULL )
  847. {
  848. HCLUSENUM hEnum = NULL;
  849. HCLUSTER hCluster = NULL;
  850. _hr = m_pClusRefObject->get_Handle( (ULONG_PTR *) &hCluster );
  851. if ( SUCCEEDED( _hr ) )
  852. {
  853. hEnum = ::ClusterOpenEnum( hCluster, CLUSTER_ENUM_NETWORK );
  854. if ( hEnum != NULL )
  855. {
  856. int _nIndex = 0;
  857. DWORD dwType = 0;
  858. LPWSTR pszName = NULL;
  859. CComObject< CClusNetwork > * pNetwork = NULL;
  860. Clear();
  861. for( _nIndex = 0, _hr = S_OK; SUCCEEDED( _hr ); _nIndex++ )
  862. {
  863. _sc = ::WrapClusterEnum( hEnum, _nIndex, &dwType, &pszName );
  864. if ( _sc == ERROR_NO_MORE_ITEMS )
  865. {
  866. _hr = S_OK;
  867. break;
  868. }
  869. else if ( _sc == ERROR_SUCCESS )
  870. {
  871. _hr = CComObject< CClusNetwork >::CreateInstance( &pNetwork );
  872. if ( SUCCEEDED( _hr ) )
  873. {
  874. CSmartPtr< ISClusRefObject > ptrRefObject( m_pClusRefObject );
  875. CSmartPtr< CComObject< CClusNetwork > > ptrNetwork( pNetwork );
  876. _hr = ptrNetwork->Open( ptrRefObject, pszName );
  877. if ( SUCCEEDED( _hr ) )
  878. {
  879. ptrNetwork->AddRef();
  880. m_NetworkList.insert( m_NetworkList.end(), ptrNetwork );
  881. }
  882. }
  883. ::LocalFree( pszName );
  884. pszName = NULL;
  885. }
  886. else
  887. {
  888. _hr = HRESULT_FROM_WIN32( _sc );
  889. }
  890. }
  891. ::ClusterCloseEnum( hEnum );
  892. }
  893. else
  894. {
  895. _sc = GetLastError();
  896. _hr = HRESULT_FROM_WIN32( _sc );
  897. }
  898. }
  899. }
  900. return _hr;
  901. } //*** CClusNetworks::Refresh()
  902. /////////////////////////////////////////////////////////////////////////////
  903. //++
  904. //
  905. // CClusNetworks::Clear
  906. //
  907. // Description:
  908. // Empty the collection of networks.
  909. //
  910. // Arguments:
  911. // None.
  912. //
  913. // Return Value:
  914. // None.
  915. //
  916. //--
  917. /////////////////////////////////////////////////////////////////////////////
  918. void CClusNetworks::Clear( void )
  919. {
  920. ::ReleaseAndEmptyCollection< NetworkList, CComObject< CClusNetwork > >( m_NetworkList );
  921. } //*** CClusNetworks::Clear()
  922. /////////////////////////////////////////////////////////////////////////////
  923. //++
  924. //
  925. // CClusNetworks::FindItem
  926. //
  927. // Description:
  928. // Find a network in the collection by name and return its index.
  929. //
  930. // Arguments:
  931. // lpszNetworkName [IN] - The name to look for.
  932. // pnIndex [OUT] - Catches the index.
  933. //
  934. // Return Value:
  935. // S_OK if successful, E_POINTER, or E_INVALIDARG.
  936. //
  937. //--
  938. /////////////////////////////////////////////////////////////////////////////
  939. HRESULT CClusNetworks::FindItem(
  940. IN LPWSTR lpszNetworkName,
  941. OUT UINT * pnIndex
  942. )
  943. {
  944. //ASSERT( lpszNetworkName != NULL );
  945. //ASSERT( pnIndex != NULL );
  946. HRESULT _hr = E_POINTER;
  947. if ( ( lpszNetworkName != NULL ) && ( pnIndex != NULL ) )
  948. {
  949. CComObject< CClusNetwork > * pNetwork = NULL;
  950. NetworkList::iterator first = m_NetworkList.begin();
  951. NetworkList::iterator last = m_NetworkList.end();
  952. UINT iIndex = 0;
  953. _hr = E_INVALIDARG;
  954. for ( ; first != last; first++, iIndex++ )
  955. {
  956. pNetwork = *first;
  957. if ( pNetwork && ( lstrcmpi( lpszNetworkName, pNetwork->Name() ) == 0 ) )
  958. {
  959. *pnIndex = iIndex;
  960. _hr = S_OK;
  961. break;
  962. }
  963. }
  964. } // if: args are not NULL
  965. return _hr;
  966. } //*** CClusNetworks::FindItem( lpszNetworkName )
  967. /////////////////////////////////////////////////////////////////////////////
  968. //++
  969. //
  970. // CClusNetworks::FindItem
  971. //
  972. // Description:
  973. // Find a network in the collection and return its index.
  974. //
  975. // Arguments:
  976. // pClusterNetwork [IN] - The network to look for.
  977. // pnIndex [OUT] - Catches the index.
  978. //
  979. // Return Value:
  980. // S_OK if successful, E_POINTER, or E_INVALIDARG.
  981. //
  982. //--
  983. /////////////////////////////////////////////////////////////////////////////
  984. HRESULT CClusNetworks::FindItem(
  985. IN ISClusNetwork * pClusterNetwork,
  986. OUT UINT * pnIndex
  987. )
  988. {
  989. //ASSERT( pClusterNetwork != NULL );
  990. //ASSERT( pnIndex != NULL );
  991. HRESULT _hr = E_POINTER;
  992. if ( ( pClusterNetwork != NULL ) && ( pnIndex != NULL ) )
  993. {
  994. CComBSTR bstrName;
  995. _hr = pClusterNetwork->get_Name( &bstrName );
  996. if ( SUCCEEDED( _hr ) )
  997. {
  998. _hr = FindItem( bstrName, pnIndex );
  999. }
  1000. }
  1001. return _hr;
  1002. } //*** CClusNetworks::FindItem( pClusterNetwork )
  1003. /////////////////////////////////////////////////////////////////////////////
  1004. //++
  1005. //
  1006. // CClusNetworks::GetIndex
  1007. //
  1008. // Description:
  1009. // Convert the passed in variant index into the real index in the
  1010. // collection.
  1011. //
  1012. // Arguments:
  1013. // varIndex [IN] - The index to convert.
  1014. // pnIndex [OUT] - Catches the index.
  1015. //
  1016. // Return Value:
  1017. // S_OK if successful, E_POINTER, or E_INVALIDARG.
  1018. //
  1019. //--
  1020. /////////////////////////////////////////////////////////////////////////////
  1021. HRESULT CClusNetworks::GetIndex(
  1022. IN VARIANT varIndex,
  1023. OUT UINT * pnIndex
  1024. )
  1025. {
  1026. //ASSERT( pnIndex != NULL );
  1027. HRESULT _hr = E_POINTER;
  1028. UINT nIndex = 0;
  1029. if ( pnIndex != NULL )
  1030. {
  1031. CComVariant v;
  1032. *pnIndex = 0;
  1033. v.Copy( &varIndex );
  1034. // Check to see if the index is a number.
  1035. _hr = v.ChangeType( VT_I4 );
  1036. if ( SUCCEEDED( _hr ) )
  1037. {
  1038. nIndex = v.lVal;
  1039. nIndex--; // Adjust index to be 0 relative instead of 1 relative
  1040. }
  1041. else
  1042. {
  1043. // Check to see if the index is a string.
  1044. _hr = v.ChangeType( VT_BSTR );
  1045. if ( SUCCEEDED( _hr ) )
  1046. {
  1047. // Search for the string.
  1048. _hr = FindItem( v.bstrVal, &nIndex );
  1049. }
  1050. }
  1051. // We found an index, now check the range.
  1052. if ( SUCCEEDED( _hr ) )
  1053. {
  1054. if ( nIndex < m_NetworkList.size() )
  1055. {
  1056. *pnIndex = nIndex;
  1057. }
  1058. else
  1059. {
  1060. _hr = E_INVALIDARG;
  1061. }
  1062. }
  1063. }
  1064. return _hr;
  1065. } //*** CClusNetworks::GetIndex()
  1066. /////////////////////////////////////////////////////////////////////////////
  1067. //++
  1068. //
  1069. // CClusNetworks::GetItem
  1070. //
  1071. // Description:
  1072. // Return the item (Network) by name.
  1073. //
  1074. // Arguments:
  1075. // lpszNetworkName [IN] - The name of the item requested.
  1076. // ppClusterNetInterface [OUT] - Catches the item.
  1077. //
  1078. // Return Value:
  1079. // S_OK if successful, E_POINTER, or E_INVALIDARG.
  1080. //
  1081. //--
  1082. /////////////////////////////////////////////////////////////////////////////
  1083. HRESULT CClusNetworks::GetItem(
  1084. IN LPWSTR lpszNetworkName,
  1085. OUT ISClusNetwork ** ppClusterNetwork
  1086. )
  1087. {
  1088. //ASSERT( lpszNetworkName != NULL );
  1089. //ASSERT( ppClusterNetwork != NULL );
  1090. HRESULT _hr = E_POINTER;
  1091. if ( ( lpszNetworkName != NULL ) && ( ppClusterNetwork != NULL ) )
  1092. {
  1093. CComObject<CClusNetwork> * pNetwork = NULL;
  1094. NetworkList::iterator first = m_NetworkList.begin();
  1095. NetworkList::iterator last = m_NetworkList.end();
  1096. while ( first != last )
  1097. {
  1098. pNetwork = *first;
  1099. if ( lstrcmpi( lpszNetworkName, pNetwork->Name() ) == 0 )
  1100. {
  1101. _hr = pNetwork->QueryInterface( IID_ISClusNetwork, (void **) ppClusterNetwork );
  1102. break;
  1103. }
  1104. first++;
  1105. }
  1106. }
  1107. return _hr;
  1108. } //*** CClusNetworks::GetItem( lpszNetworkName )
  1109. /////////////////////////////////////////////////////////////////////////////
  1110. //++
  1111. //
  1112. // CClusNetworks::GetItem
  1113. //
  1114. // Description:
  1115. // Return the item (Network) by index.
  1116. //
  1117. // Arguments:
  1118. // nIndex [IN] - The index of the item requested.
  1119. // ppClusterNetInterface [OUT] - Catches the item.
  1120. //
  1121. // Return Value:
  1122. // S_OK if successful, E_POINTER, or E_INVALIDARG.
  1123. //
  1124. //--
  1125. /////////////////////////////////////////////////////////////////////////////
  1126. HRESULT CClusNetworks::GetItem(
  1127. IN UINT nIndex,
  1128. OUT ISClusNetwork ** ppClusterNetwork
  1129. )
  1130. {
  1131. //ASSERT( ppClusterNetwork != NULL );
  1132. HRESULT _hr = E_POINTER;
  1133. if ( ppClusterNetwork != NULL )
  1134. {
  1135. // Automation collections are 1-relative for languages like VB.
  1136. // We are 0-relative internally.
  1137. nIndex--;
  1138. if ( nIndex < m_NetworkList.size() )
  1139. {
  1140. CComObject< CClusNetwork > * pNetwork = m_NetworkList[ nIndex ];
  1141. _hr = pNetwork->QueryInterface( IID_ISClusNetwork, (void **) ppClusterNetwork );
  1142. } // if: index is in range
  1143. else
  1144. {
  1145. _hr = E_INVALIDARG;
  1146. } // else: index is out of range
  1147. }
  1148. return _hr;
  1149. } //*** CClusNetworks::GetItem( nIndex )
  1150. /////////////////////////////////////////////////////////////////////////////
  1151. //++
  1152. //
  1153. // CClusNetworks::get_Item
  1154. //
  1155. // Description:
  1156. // Return the object (Network) at the passed in index.
  1157. //
  1158. // Arguments:
  1159. // varIndex [IN] - Contains the index requested.
  1160. // ppClusterNetwork [OUT] - Catches the item.
  1161. //
  1162. // Return Value:
  1163. // S_OK if successful, E_POINTER, or E_INVALIDARG.
  1164. //
  1165. //--
  1166. /////////////////////////////////////////////////////////////////////////////
  1167. STDMETHODIMP CClusNetworks::get_Item(
  1168. IN VARIANT varIndex,
  1169. OUT ISClusNetwork ** ppClusterNetwork
  1170. )
  1171. {
  1172. //ASSERT( ppClusterNetwork != NULL );
  1173. HRESULT _hr = E_POINTER;
  1174. if ( ppClusterNetwork != NULL )
  1175. {
  1176. CComObject<CClusNetwork> * pNetwork = NULL;
  1177. UINT nIndex = 0;
  1178. // Zero the out param
  1179. *ppClusterNetwork = 0;
  1180. _hr = GetIndex( varIndex, &nIndex );
  1181. if ( SUCCEEDED( _hr ) )
  1182. {
  1183. pNetwork = m_NetworkList[ nIndex ];
  1184. _hr = pNetwork->QueryInterface( IID_ISClusNetwork, (void **) ppClusterNetwork );
  1185. }
  1186. }
  1187. return _hr;
  1188. } //*** CClusNetworks::get_Item()