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.

659 lines
15 KiB

  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2000-2001 Microsoft Corporation
  4. //
  5. // Module Name:
  6. // EnumManageableResources.cpp
  7. //
  8. // Description:
  9. // CEnumManageableResources implementation.
  10. //
  11. // Maintained By:
  12. // Galen Barbee (GalenB) 02-FEB-2000
  13. //
  14. //////////////////////////////////////////////////////////////////////////////
  15. #include "pch.h"
  16. #include "ManagedDevice.h"
  17. #include "EnumManageableResources.h"
  18. DEFINE_THISCLASS("CEnumManageableResources")
  19. // ************************************************************************
  20. //
  21. // Constructor / Destructor
  22. //
  23. // ************************************************************************
  24. //////////////////////////////////////////////////////////////////////////////
  25. //
  26. // HRESULT
  27. // CEnumManageableResources::S_HrCreateInstance(
  28. // IUnknown ** ppunkOut
  29. // )
  30. //
  31. //////////////////////////////////////////////////////////////////////////////
  32. HRESULT
  33. CEnumManageableResources::S_HrCreateInstance(
  34. IUnknown ** ppunkOut
  35. )
  36. {
  37. TraceFunc( "" );
  38. Assert( ppunkOut != NULL );
  39. HRESULT hr;
  40. CEnumManageableResources * pemr = new CEnumManageableResources;
  41. if ( pemr != NULL )
  42. {
  43. hr = THR( pemr->Init( ) );
  44. if ( SUCCEEDED( hr ) )
  45. {
  46. hr = THR( pemr->TypeSafeQI( IUnknown, ppunkOut ) );
  47. }
  48. pemr->Release( );
  49. }
  50. else
  51. {
  52. hr = E_OUTOFMEMORY;
  53. }
  54. HRETURN( hr );
  55. } // S_HrCreateInstance( )
  56. //////////////////////////////////////////////////////////////////////////////
  57. //
  58. // CEnumManageableResources::CEnumManageableResources( void )
  59. //
  60. //////////////////////////////////////////////////////////////////////////////
  61. CEnumManageableResources::CEnumManageableResources( void )
  62. {
  63. TraceFunc( "" );
  64. InterlockedIncrement( &g_cObjects );
  65. TraceFuncExit();
  66. } // CEnumManageableResources( )
  67. //////////////////////////////////////////////////////////////////////////////
  68. //
  69. // STDMETHODIMP
  70. // CEnumManageableResources::Init( void )
  71. //
  72. //////////////////////////////////////////////////////////////////////////////
  73. STDMETHODIMP
  74. CEnumManageableResources::Init( void )
  75. {
  76. TraceFunc( "" );
  77. HRESULT hr = S_OK;
  78. // IUnknown stuff
  79. Assert( m_cRef == 0 );
  80. AddRef( ); // Add one count
  81. // IEnumClusCfgManagedResources
  82. Assert( m_cAlloced == 0 );
  83. Assert( m_cIter == 0 );
  84. Assert( m_pList == NULL );
  85. HRETURN( hr );
  86. } // Init( )
  87. //////////////////////////////////////////////////////////////////////////////
  88. //
  89. // CEnumManageableResources::~CEnumManageableResources( )
  90. //
  91. //////////////////////////////////////////////////////////////////////////////
  92. CEnumManageableResources::~CEnumManageableResources( )
  93. {
  94. TraceFunc( "" );
  95. if ( m_pList != NULL )
  96. {
  97. while ( m_cAlloced != 0 )
  98. {
  99. m_cAlloced --;
  100. AssertMsg( m_pList[ m_cAlloced ], "This shouldn't happen" );
  101. if ( m_pList[ m_cAlloced ] != NULL )
  102. {
  103. (m_pList[ m_cAlloced ])->Release( );
  104. }
  105. }
  106. TraceFree( m_pList );
  107. }
  108. InterlockedDecrement( &g_cObjects );
  109. TraceFuncExit();
  110. } // ~CEnumManageableResources( )
  111. // ************************************************************************
  112. //
  113. // IUnknown
  114. //
  115. // ************************************************************************
  116. //////////////////////////////////////////////////////////////////////////////
  117. //
  118. // STDMETHODIMP
  119. // CEnumManageableResources::QueryInterface(
  120. // REFIID riid,
  121. // LPVOID *ppv
  122. // )
  123. //
  124. //////////////////////////////////////////////////////////////////////////////
  125. STDMETHODIMP
  126. CEnumManageableResources::QueryInterface(
  127. REFIID riid,
  128. LPVOID *ppv
  129. )
  130. {
  131. TraceQIFunc( riid, ppv );
  132. HRESULT hr = E_NOINTERFACE;
  133. if ( IsEqualIID( riid, IID_IUnknown ) )
  134. {
  135. *ppv = static_cast< IEnumClusCfgManagedResources * >( this );
  136. hr = S_OK;
  137. } // if: IUnknown
  138. else if ( IsEqualIID( riid, IID_IEnumClusCfgManagedResources ) )
  139. {
  140. *ppv = TraceInterface( __THISCLASS__, IEnumClusCfgManagedResources, this, 0 );
  141. hr = S_OK;
  142. } // else if: IEnumClusCfgManagedResources
  143. else if ( IsEqualIID( riid, IID_IExtendObjectManager ) )
  144. {
  145. *ppv = TraceInterface( __THISCLASS__, IExtendObjectManager, this, 0 );
  146. hr = S_OK;
  147. } // else if: IExtendObjectManager
  148. if ( SUCCEEDED( hr ) )
  149. {
  150. ((IUnknown*) *ppv)->AddRef( );
  151. } // if: success
  152. QIRETURN_IGNORESTDMARSHALLING( hr, riid );
  153. } // QueryInterface( )
  154. //////////////////////////////////////////////////////////////////////////////
  155. //
  156. // STDMETHODIMP_(ULONG)
  157. // CEnumManageableResources::AddRef( void )
  158. //
  159. //////////////////////////////////////////////////////////////////////////////
  160. STDMETHODIMP_(ULONG)
  161. CEnumManageableResources::AddRef( void )
  162. {
  163. TraceFunc( "[IUnknown]" );
  164. InterlockedIncrement( &m_cRef );
  165. RETURN( m_cRef );
  166. } // AddRef( )
  167. //////////////////////////////////////////////////////////////////////////////
  168. //
  169. // STDMETHODIMP_(ULONG)
  170. // CEnumManageableResources::Release( void )
  171. //
  172. //////////////////////////////////////////////////////////////////////////////
  173. STDMETHODIMP_(ULONG)
  174. CEnumManageableResources::Release( void )
  175. {
  176. TraceFunc( "[IUnknown]" );
  177. InterlockedDecrement( &m_cRef );
  178. if ( m_cRef )
  179. RETURN( m_cRef );
  180. TraceDo( delete this );
  181. RETURN(0);
  182. } // Release( )
  183. // ************************************************************************
  184. //
  185. // IExtendObjectManager
  186. //
  187. // ************************************************************************
  188. //////////////////////////////////////////////////////////////////////////////
  189. //
  190. // STDMETHODIMP
  191. // CEnumManageableResources::FindObject(
  192. // OBJECTCOOKIE cookieIn
  193. // , REFCLSID rclsidTypeIn
  194. // , LPCWSTR pcszNameIn
  195. // , LPUNKNOWN * punkOut
  196. // )
  197. //
  198. //////////////////////////////////////////////////////////////////////////////
  199. STDMETHODIMP
  200. CEnumManageableResources::FindObject(
  201. OBJECTCOOKIE cookieIn
  202. , REFCLSID rclsidTypeIn
  203. , LPCWSTR pcszNameIn
  204. , LPUNKNOWN * ppunkOut
  205. )
  206. {
  207. TraceFunc( "[IExtendObjectManager]" );
  208. OBJECTCOOKIE cookie;
  209. OBJECTCOOKIE cookieParent;
  210. IServiceProvider * psp;
  211. HRESULT hr = E_UNEXPECTED;
  212. IObjectManager * pom = NULL;
  213. IStandardInfo * psi = NULL;
  214. IEnumCookies * pec = NULL;
  215. DWORD objectCount = 0;
  216. //
  217. // Check arguments
  218. //
  219. if ( cookieIn == 0 )
  220. goto InvalidArg;
  221. if ( rclsidTypeIn != CLSID_ManagedResourceType )
  222. goto InvalidType;
  223. if ( ppunkOut == NULL )
  224. goto InvalidPointer;
  225. AssertMsg( pcszNameIn == NULL, "Enums shouldn't have names." );
  226. //
  227. // Grab the object manager.
  228. //
  229. hr = THR( CoCreateInstance( CLSID_ServiceManager,
  230. NULL,
  231. CLSCTX_INPROC_SERVER,
  232. TypeSafeParams( IServiceProvider, &psp )
  233. ) );
  234. if ( FAILED( hr ) )
  235. goto Cleanup;
  236. hr = THR( psp->TypeSafeQS( CLSID_ObjectManager,
  237. IObjectManager,
  238. &pom
  239. ) );
  240. psp->Release( ); // release promptly
  241. if ( FAILED( hr ) )
  242. goto Cleanup;
  243. //
  244. // Ask the Object Manager for information about our cookie so we can
  245. // get to the "parent" cookie.
  246. //
  247. hr = THR( pom->GetObject( DFGUID_StandardInfo,
  248. cookieIn,
  249. reinterpret_cast< IUnknown ** >( &psi )
  250. ) );
  251. if ( FAILED( hr ) )
  252. goto Cleanup;
  253. hr = STHR( psi->GetParent( &cookieParent ) );
  254. if ( FAILED( hr ) )
  255. goto Cleanup;
  256. //
  257. // Now ask the Object Manager for a cookie enumerator.
  258. //
  259. hr = THR( pom->FindObject( CLSID_ManagedResourceType,
  260. cookieParent,
  261. NULL,
  262. DFGUID_EnumCookies,
  263. NULL,
  264. reinterpret_cast< IUnknown ** >( &pec )
  265. ) );
  266. if ( FAILED( hr ) )
  267. goto Cleanup;
  268. pec = TraceInterface( L"CEnumClusCfgManagedResources!IEnumCookies", IEnumCookies, pec, 1 );
  269. hr = pec->Count( &objectCount );
  270. if ( FAILED( hr ) )
  271. goto Cleanup;
  272. m_cAlloced = objectCount;
  273. if ( m_cAlloced == 0 )
  274. goto ErrorNotFound;
  275. //
  276. // Allocate a buffer to store the punks.
  277. //
  278. m_pList = (IClusCfgManagedResourceInfo **) TraceAlloc( HEAP_ZERO_MEMORY, m_cAlloced * sizeof(IClusCfgManagedResourceInfo *) );
  279. if ( m_pList == NULL )
  280. goto OutOfMemory;
  281. //
  282. // Reset the enumerator.
  283. //
  284. hr = THR( pec->Reset( ) );
  285. if ( FAILED( hr ) )
  286. goto Cleanup;
  287. //
  288. // Now loop thru to collect the interfaces.
  289. //
  290. m_cIter = 0;
  291. while ( hr == S_OK && m_cIter < m_cAlloced )
  292. {
  293. hr = STHR( pec->Next( 1, &cookie, NULL ) );
  294. if ( FAILED( hr ) )
  295. goto Cleanup;
  296. if ( hr == S_FALSE )
  297. break; // exit condition
  298. hr = THR( pom->GetObject( DFGUID_ManagedResource,
  299. cookie,
  300. reinterpret_cast< IUnknown ** >( &m_pList[ m_cIter ] )
  301. ) );
  302. if ( FAILED( hr ) )
  303. goto Cleanup;
  304. m_cIter++;
  305. } // while: S_OK
  306. //
  307. // Reset the iter.
  308. //
  309. m_cIter = 0;
  310. //
  311. // Grab the interface.
  312. //
  313. hr = THR( QueryInterface( DFGUID_EnumManageableResources,
  314. reinterpret_cast< void ** >( ppunkOut )
  315. ) );
  316. if ( FAILED( hr ) )
  317. goto Cleanup;
  318. Cleanup:
  319. if ( pom != NULL )
  320. {
  321. pom->Release( );
  322. }
  323. if ( psi != NULL )
  324. {
  325. psi->Release( );
  326. }
  327. if ( pec != NULL )
  328. {
  329. pec->Release( );
  330. }
  331. HRETURN( hr );
  332. InvalidArg:
  333. hr = THR( E_INVALIDARG );
  334. goto Cleanup;
  335. InvalidPointer:
  336. hr = THR( E_POINTER );
  337. goto Cleanup;
  338. InvalidType:
  339. //
  340. // TODO: gpease 07-APR-2000
  341. // Come up with a better error code.
  342. //
  343. hr = THR( E_FAIL );
  344. goto Cleanup;
  345. ErrorNotFound:
  346. // The error text is better than the coding value.
  347. hr = THR( HRESULT_FROM_WIN32( ERROR_NOT_FOUND ) );
  348. goto Cleanup;
  349. OutOfMemory:
  350. hr = E_OUTOFMEMORY;
  351. goto Cleanup;
  352. } // FindObject( )
  353. //****************************************************************************
  354. //
  355. // IEnumClusCfgManagedResources
  356. //
  357. //****************************************************************************
  358. //////////////////////////////////////////////////////////////////////////////
  359. //
  360. // STDMETHODIMP
  361. // CEnumManageableResources::Next(
  362. // ULONG celt,
  363. // IClusCfgNetworkInfo ** rgOut,
  364. // ULONG * pceltFetchedOut
  365. // )
  366. //
  367. //////////////////////////////////////////////////////////////////////////////
  368. STDMETHODIMP
  369. CEnumManageableResources::Next(
  370. ULONG celt,
  371. IClusCfgManagedResourceInfo * rgOut[],
  372. ULONG * pceltFetchedOut
  373. )
  374. {
  375. TraceFunc( "[IEnumClusCfgManagedResources]" );
  376. ULONG celtFetched;
  377. HRESULT hr = E_UNEXPECTED;
  378. //
  379. // Check parameters
  380. //
  381. if ( rgOut == NULL || celt == 0 )
  382. goto InvalidPointer;
  383. //
  384. // Zero the return count.
  385. //
  386. if ( pceltFetchedOut != NULL )
  387. {
  388. *pceltFetchedOut = 0;
  389. }
  390. //
  391. // Clear the buffer
  392. //
  393. ZeroMemory( rgOut, celt * sizeof(rgOut[0]) );
  394. //
  395. // Loop thru copying the interfaces.
  396. //
  397. for( celtFetched = 0
  398. ; celtFetched + m_cIter < m_cAlloced && celtFetched < celt
  399. ; celtFetched ++
  400. )
  401. {
  402. hr = THR( m_pList[ m_cIter + celtFetched ]->TypeSafeQI( IClusCfgManagedResourceInfo, &rgOut[ celtFetched ] ) );
  403. if ( FAILED( hr ) )
  404. goto CleanupList;
  405. rgOut[ celtFetched ] = TraceInterface( L"CEnumManageableResources!IClusCfgManagedResourceInfo", IClusCfgManagedResourceInfo, rgOut[ celtFetched ], 1 );
  406. } // for: celtFetched
  407. if ( pceltFetchedOut != NULL )
  408. {
  409. *pceltFetchedOut = celtFetched;
  410. }
  411. m_cIter += celtFetched;
  412. if ( celtFetched != celt )
  413. {
  414. hr = S_FALSE;
  415. }
  416. else
  417. {
  418. hr = S_OK;
  419. }
  420. Cleanup:
  421. HRETURN( hr );
  422. CleanupList:
  423. for ( ; celtFetched != 0 ; )
  424. {
  425. celtFetched --;
  426. rgOut[ celtFetched ]->Release( );
  427. rgOut[ celtFetched ] = NULL;
  428. }
  429. goto Cleanup;
  430. InvalidPointer:
  431. hr = THR( E_POINTER );
  432. goto Cleanup;
  433. } // Next( )
  434. //////////////////////////////////////////////////////////////////////////////
  435. //
  436. // STDMETHODIMP
  437. // CEnumManageableResources::Skip(
  438. // ULONG celt
  439. // )
  440. //
  441. //////////////////////////////////////////////////////////////////////////////
  442. STDMETHODIMP
  443. CEnumManageableResources::Skip(
  444. ULONG celt
  445. )
  446. {
  447. TraceFunc( "[IEnumClusCfgManagedResources]" );
  448. HRESULT hr = S_OK;
  449. m_cIter += celt;
  450. if ( m_cIter > m_cAlloced )
  451. {
  452. m_cIter = m_cAlloced;
  453. hr = S_FALSE;
  454. }
  455. HRETURN( hr );
  456. } // Skip( )
  457. //////////////////////////////////////////////////////////////////////////////
  458. //
  459. // STDMETHODIMP
  460. // CEnumManageableResources::Reset( void )
  461. //
  462. //////////////////////////////////////////////////////////////////////////////
  463. STDMETHODIMP
  464. CEnumManageableResources::Reset( void )
  465. {
  466. TraceFunc( "[IEnumClusCfgManagedResources]" );
  467. HRESULT hr = S_OK;
  468. m_cIter = 0;
  469. HRETURN( hr );
  470. } // Reset( )
  471. //////////////////////////////////////////////////////////////////////////////
  472. //
  473. // STDMETHODIMP
  474. // CEnumManageableResources::Clone(
  475. // IEnumClusCfgManagedResources ** ppenumOut
  476. // )
  477. //
  478. //////////////////////////////////////////////////////////////////////////////
  479. STDMETHODIMP
  480. CEnumManageableResources::Clone(
  481. IEnumClusCfgManagedResources ** ppenumOut
  482. )
  483. {
  484. TraceFunc( "[IEnumClusCfgManagedResources]" );
  485. //
  486. // KB: not going to implement this method.
  487. //
  488. HRESULT hr = THR( E_NOTIMPL );
  489. HRETURN( hr );
  490. } // Clone( )
  491. //////////////////////////////////////////////////////////////////////////////
  492. //
  493. // STDMETHODIMP
  494. // CEnumManageableResources::Count(
  495. // DWORD * pnCountOut
  496. // )
  497. //
  498. //////////////////////////////////////////////////////////////////////////////
  499. STDMETHODIMP
  500. CEnumManageableResources::Count(
  501. DWORD * pnCountOut
  502. )
  503. {
  504. TraceFunc( "[IEnumClusCfgManagedResources]" );
  505. HRESULT hr = S_OK;
  506. if ( pnCountOut == NULL )
  507. {
  508. hr = THR( E_POINTER );
  509. goto Cleanup;
  510. }
  511. *pnCountOut = m_cAlloced;
  512. Cleanup:
  513. HRETURN( hr );
  514. }// Count( )