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.

857 lines
26 KiB

  1. //***************************************************************************
  2. //
  3. // Copyright (c) 1997-2002 Microsoft Corporation, All Rights Reserved
  4. //
  5. //***************************************************************************
  6. #include "wmicom.h"
  7. #include "wdmshell.h"
  8. #include "wmimof.h"
  9. #include "wmimap.h"
  10. #include <stdlib.h>
  11. #include <winerror.h>
  12. //=============================================================================================================
  13. //*************************************************************************************************************
  14. //
  15. //
  16. // CWMIStandardShell
  17. //
  18. //
  19. //*************************************************************************************************************
  20. //=============================================================================================================
  21. CWMIStandardShell::CWMIStandardShell()
  22. {
  23. m_pClass = NULL;
  24. m_pWDM = NULL;
  25. m_fInit = FALSE;
  26. }
  27. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  28. CWMIStandardShell::~CWMIStandardShell()
  29. {
  30. SAFE_DELETE_PTR(m_pWDM);
  31. SAFE_DELETE_PTR(m_pClass);
  32. m_fInit = FALSE;
  33. }
  34. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  35. HRESULT CWMIStandardShell::Initialize(WCHAR * wcsClass, BOOL fInternalEvent, CHandleMap * pList,
  36. BOOL fUpdate, ULONG uDesiredAccess,
  37. IWbemServices __RPC_FAR * pServices,
  38. IWbemServices __RPC_FAR * pRepository,
  39. IWbemObjectSink __RPC_FAR * pHandler,
  40. IWbemContext __RPC_FAR *pCtx)
  41. {
  42. HRESULT hr = WBEM_E_FAILED;
  43. if( !m_fInit )
  44. {
  45. m_pClass = new CWMIProcessClass(0);
  46. if( m_pClass )
  47. {
  48. hr = m_pClass->Initialize();
  49. if( S_OK == hr )
  50. {
  51. m_pClass->WMI()->SetWMIPointers(pList,pServices,pRepository,pHandler,pCtx);
  52. m_pClass->SetHiPerf(FALSE);
  53. if( !fInternalEvent )
  54. {
  55. if( wcsClass )
  56. {
  57. hr = m_pClass->SetClass(wcsClass);
  58. if( SUCCEEDED(hr))
  59. {
  60. if( !m_pClass->ValidClass())
  61. {
  62. hr = WBEM_E_INVALID_OBJECT;
  63. }
  64. }
  65. }
  66. }
  67. else
  68. {
  69. if( wcsClass )
  70. {
  71. hr = m_pClass->SetClassName(wcsClass);
  72. }
  73. }
  74. if( hr == S_OK )
  75. {
  76. m_pWDM = new CProcessStandardDataBlock();
  77. if( m_pWDM )
  78. {
  79. m_pWDM->SetDesiredAccess(uDesiredAccess);
  80. m_pWDM->SetClassProcessPtr(m_pClass);
  81. m_pWDM->UpdateNamespace(fUpdate);
  82. m_fInit = TRUE;
  83. }
  84. }
  85. }
  86. }
  87. }
  88. return hr;
  89. }
  90. //////////////////////////////////////////////////////////////////////////////////////
  91. HRESULT CWMIStandardShell::SetGuidForEvent( WORD wType, WCHAR * wcsGuid, int cchSize )
  92. {
  93. HRESULT hRes = WBEM_E_FAILED;
  94. if( m_fInit )
  95. {
  96. // ==============================================
  97. // Inform the WMI we want to register for the
  98. // event
  99. // ==============================================
  100. memset(wcsGuid,NULL,cchSize);
  101. switch( wType ){
  102. case MOF_ADDED:
  103. hRes = S_OK;
  104. m_pClass->SetHardCodedGuidType(wType);
  105. hRes = StringCchCopyW ( wcsGuid, cchSize, WMI_RESOURCE_MOF_ADDED_GUID );
  106. break;
  107. case MOF_DELETED:
  108. hRes = S_OK;
  109. m_pClass->SetHardCodedGuidType(wType);
  110. hRes = StringCchCopyW ( wcsGuid, cchSize, WMI_RESOURCE_MOF_REMOVED_GUID );
  111. break;
  112. case STANDARD_EVENT:
  113. hRes = m_pClass->GetQualifierString( NULL, L"guid", wcsGuid, cchSize );
  114. break;
  115. }
  116. }
  117. return hRes;
  118. }
  119. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  120. HRESULT CWMIStandardShell::ProcessSingleInstance( WCHAR * wcsInstanceName/*, PWNODE_SINGLE_INSTANCE pwSingle */)
  121. {
  122. HRESULT hr = WBEM_E_FAILED;
  123. if( m_fInit )
  124. {
  125. //======================================================
  126. // If we are not working with a specific guy, then
  127. // query WMI to get it, if, of course it is valid
  128. //======================================================
  129. if( m_pClass->ValidClass() ){
  130. /* if( pwSingle){
  131. hr = m_pWDM->SetSingleInstancePtr((PWNODE_SINGLE_INSTANCE)pwSingle);
  132. }
  133. else{*/
  134. hr = m_pWDM->OpenWMI();
  135. if( hr == S_OK ){
  136. hr = m_pWDM->QuerySingleInstance(wcsInstanceName);
  137. }
  138. // }
  139. //======================================================
  140. // If we got the data and a valid class, the process it
  141. //======================================================
  142. if( hr == S_OK )
  143. {
  144. hr = m_pWDM->ReadWMIDataBlockAndPutIntoWbemInstance();
  145. if( SUCCEEDED(hr))
  146. {
  147. hr = m_pClass->SendInstanceBack();
  148. }
  149. }
  150. }
  151. }
  152. return hr;
  153. }
  154. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  155. HRESULT CWMIStandardShell::ProcessAllInstances( )
  156. {
  157. HRESULT hr = WBEM_E_FAILED;
  158. if( m_fInit )
  159. {
  160. //======================================================
  161. // If we are not working with a specific guy, then
  162. // query WMI to get it
  163. //======================================================
  164. // if( pwAllNode ){
  165. // hr = m_pWDM->SetAllInstancePtr((PWNODE_ALL_DATA)pwAllNode);
  166. // }
  167. // else{
  168. hr = m_pWDM->OpenWMI();
  169. if( hr == S_OK ){
  170. hr = m_pWDM->QueryAllData();
  171. }
  172. // }
  173. //======================================================
  174. // If we got the data then process it
  175. //======================================================
  176. if( hr == S_OK ){
  177. while( TRUE ){
  178. hr = m_pWDM->ReadWMIDataBlockAndPutIntoWbemInstance();
  179. if( hr == S_OK ){
  180. hr = m_pClass->SendInstanceBack();
  181. }
  182. if( hr != S_OK ){
  183. break;
  184. }
  185. if( !m_pWDM->MoreToProcess() ){
  186. break;
  187. }
  188. }
  189. }
  190. }
  191. return hr;
  192. }
  193. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  194. HRESULT CWMIStandardShell::ExecuteMethod( WCHAR * wcsInstance, WCHAR * MethodName, IWbemClassObject * pParentClass,
  195. IWbemClassObject * pInClassData,
  196. IWbemClassObject * pInClass,
  197. IWbemClassObject * pOutClass)
  198. {
  199. HRESULT hr = WBEM_E_FAILED;
  200. if( m_fInit )
  201. {
  202. CWMIProcessClass MethodInput(0);
  203. CWMIProcessClass MethodOutput(0);
  204. if( SUCCEEDED( MethodInput.Initialize() ) && SUCCEEDED( MethodOutput.Initialize() ) )
  205. {
  206. MethodInput.SetWMIPointers(m_pClass);
  207. MethodOutput.SetWMIPointers(m_pClass);
  208. //======================================================
  209. // Initialize all of the necessary stuff and get the
  210. // definition of the class we are working with
  211. //======================================================
  212. if( pInClass )
  213. {
  214. hr = MethodInput.SetClass(pInClass);
  215. if( hr != S_OK ){
  216. return hr;
  217. }
  218. MethodInput.SetClassPointerOnly(pInClassData);
  219. }
  220. if( pOutClass ){
  221. hr = MethodOutput.SetClass(pOutClass, MethodInput.GetNumberOfProperties () );
  222. if( hr != S_OK ){
  223. return hr;
  224. }
  225. }
  226. //======================================================
  227. // Notify WMI which class we are going to be executing
  228. // methods on
  229. //======================================================
  230. hr = m_pWDM->OpenWMI();
  231. if( hr == S_OK ){
  232. m_pWDM->SetMethodInput(&MethodInput);
  233. m_pWDM->SetMethodOutput(&MethodOutput);
  234. m_pClass->SetClassPointerOnly(pParentClass);
  235. // Create in Param Block
  236. // ========================
  237. BYTE * InputBuffer=NULL;
  238. ULONG uInputBufferSize=0L;
  239. hr = m_pWDM->CreateInParameterBlockForMethods(InputBuffer,uInputBufferSize);
  240. if( hr == S_OK ){
  241. // Allocate Out Param Block
  242. // ========================
  243. ULONG WMIMethodId = m_pClass->GetMethodId(MethodName);
  244. //======================================================
  245. // If we got the data then process it
  246. //======================================================
  247. hr = m_pWDM->CreateOutParameterBlockForMethods();
  248. if( hr == S_OK ){
  249. hr = m_pWDM->ExecuteMethod( WMIMethodId,wcsInstance, uInputBufferSize,InputBuffer);
  250. }
  251. }
  252. SAFE_DELETE_ARRAY(InputBuffer);
  253. }
  254. }
  255. }
  256. return hr;
  257. }
  258. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  259. HRESULT CWMIStandardShell::ProcessEvent( WORD wBinaryMofType, PWNODE_HEADER WnodeHeader)
  260. {
  261. HRESULT hr = S_OK;
  262. if( m_fInit )
  263. {
  264. //===================================================
  265. // If the image path is empty, it is a binary guid
  266. // which we need to process
  267. //===================================================
  268. if( wBinaryMofType ){
  269. hr = ProcessBinaryGuidsViaEvent( WnodeHeader, wBinaryMofType );
  270. }
  271. else{
  272. //=======================================================
  273. // Process the data event
  274. //=======================================================
  275. if( WnodeHeader->Flags & WNODE_FLAG_ALL_DATA ){
  276. hr = m_pWDM->SetAllInstancePtr((PWNODE_ALL_DATA)WnodeHeader);
  277. }
  278. else if( WnodeHeader->Flags & WNODE_FLAG_SINGLE_INSTANCE ){
  279. hr = m_pWDM->SetSingleInstancePtr((PWNODE_SINGLE_INSTANCE)WnodeHeader);
  280. }
  281. if( hr == S_OK ){
  282. //===================================================================
  283. // Process all wnodes.
  284. //===================================================================
  285. while( TRUE ){
  286. if( S_OK == ( hr = m_pWDM->ReadWMIDataBlockAndPutIntoWbemInstance()) ){
  287. //===========================================================
  288. // Now, send it to all consumers registered for this event
  289. //===========================================================
  290. hr = m_pClass->SendInstanceBack();
  291. }
  292. //===============================================================
  293. // If we errored out,we don't know that any of the pointers
  294. // are ok, so get out of there.
  295. //===============================================================
  296. else{
  297. break;
  298. }
  299. //===============================================================
  300. // Process all of the instances for this event
  301. //===============================================================
  302. if( !m_pWDM->MoreToProcess() ){
  303. break;
  304. }
  305. }
  306. }
  307. }
  308. //============================================================================
  309. // Since we never allocated anything, just used the incoming stuff,
  310. // for cleanliness sake, init ptrs to null
  311. //============================================================================
  312. m_pWDM->InitDataBufferToNull();
  313. }
  314. return hr;
  315. }
  316. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  317. HRESULT CWMIStandardShell::FillInAndSubmitWMIDataBlob( IWbemClassObject * pIClass, int nTypeOfPut, CVARIANT & vList)
  318. {
  319. HRESULT hr = WBEM_E_FAILED;
  320. if( m_fInit )
  321. {
  322. hr = m_pWDM->OpenWMI();
  323. if( hr == S_OK ){
  324. // Now, work with the class we want to write
  325. if( SUCCEEDED(m_pClass->SetClassPointerOnly(pIClass))){
  326. if( nTypeOfPut == PUT_WHOLE_INSTANCE ){
  327. hr = m_pWDM->ConstructDataBlock(TRUE) ;
  328. if( S_OK == hr ){
  329. hr = m_pWDM->SetSingleInstance();
  330. }
  331. }
  332. else{
  333. if(m_pWDM->GetListOfPropertiesToPut(nTypeOfPut,vList)){
  334. hr = m_pWDM->PutSingleProperties();
  335. }
  336. else{
  337. hr = WBEM_E_INVALID_CONTEXT;
  338. }
  339. }
  340. }
  341. }
  342. }
  343. return(hr);
  344. }
  345. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  346. HRESULT CWMIStandardShell::QueryAndProcessAllBinaryGuidInstances(CNamespaceManagement & Namespace, BOOL & fMofHasChanged,
  347. KeyList * pArrDriversInRegistry)
  348. {
  349. HRESULT hr = WBEM_E_FAILED;
  350. if( m_fInit )
  351. {
  352. hr = WBEM_E_OUT_OF_MEMORY;
  353. CAutoWChar wcsTmpKey(MAX_PATH*3);
  354. if( wcsTmpKey.Valid() )
  355. {
  356. m_pClass->SetHardCodedGuidType(MOF_ADDED);
  357. m_pClass->GetGuid();
  358. hr = m_pWDM->OpenWMI();
  359. if( hr == S_OK )
  360. {
  361. hr = m_pWDM->QueryAllData();
  362. //======================================================
  363. // If we got the data then process it
  364. //======================================================
  365. if( hr == S_OK )
  366. {
  367. while( SUCCEEDED ( hr ) )
  368. {
  369. hr = m_pWDM->ProcessNameBlock(FALSE);
  370. if( hr == S_OK )
  371. {
  372. hr = m_pWDM->ProcessBinaryMofDataBlock(CVARIANT(m_pClass->GetClassName()),wcsTmpKey, MAX_PATH*3);
  373. if( hr == S_OK )
  374. {
  375. hr = Namespace.UpdateQuery(L" and Name != ",wcsTmpKey);
  376. if( pArrDriversInRegistry )
  377. {
  378. pArrDriversInRegistry->Remove(wcsTmpKey);
  379. }
  380. }
  381. if( !m_pWDM->MoreToProcess() )
  382. {
  383. break;
  384. }
  385. }
  386. }
  387. }
  388. }
  389. }
  390. }
  391. return hr;
  392. }
  393. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  394. HRESULT CWMIStandardShell::ProcessBinaryGuidsViaEvent( PWNODE_HEADER WnodeHeader, WORD wType )
  395. {
  396. HRESULT hr = WBEM_E_FAILED;
  397. if( m_fInit )
  398. {
  399. //======================================================
  400. // Initialize all of the necessary stuff and get the
  401. // definition of the class we are working with
  402. //======================================================
  403. m_pClass->SetHardCodedGuidType(wType);
  404. //==================================================================
  405. // We are working with a specific guy, so we need to find out
  406. // if it is a Binary Mof Guid to do a query all data on, or
  407. // if it is a Resource Name and File to open up and extract
  408. //==================================================================
  409. if( WnodeHeader->Flags & WNODE_FLAG_ALL_DATA )
  410. {
  411. hr = m_pWDM->SetAllInstancePtr((PWNODE_ALL_DATA)WnodeHeader);
  412. if(hr == S_OK)
  413. {
  414. hr = m_pWDM->ReadWMIDataBlockAndPutIntoWbemInstance();
  415. }
  416. }
  417. else if( WnodeHeader->Flags & WNODE_FLAG_SINGLE_INSTANCE )
  418. {
  419. hr = m_pWDM->SetSingleInstancePtr((PWNODE_SINGLE_INSTANCE)WnodeHeader);
  420. if( hr == S_OK )
  421. {
  422. hr = m_pWDM->ProcessDataBlock();
  423. }
  424. }
  425. else
  426. {
  427. hr = WBEM_E_INVALID_PARAMETER;
  428. }
  429. }
  430. return hr;
  431. }
  432. //************************************************************************************************************
  433. //============================================================================================================
  434. //
  435. // CWMIHiPerfShell
  436. //
  437. //============================================================================================================
  438. //************************************************************************************************************
  439. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  440. CWMIHiPerfShell::CWMIHiPerfShell(BOOL fAutoCleanup)
  441. {
  442. m_fAutoCleanup = fAutoCleanup;
  443. m_pWDM = NULL;
  444. m_pClass = NULL;
  445. m_fInit = FALSE;
  446. }
  447. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  448. HRESULT CWMIHiPerfShell::Initialize (
  449. BOOL fUpdate,
  450. ULONG uDesiredAccess,
  451. CHandleMap * pList,WCHAR * wcs,
  452. IWbemServices __RPC_FAR * pServices,
  453. IWbemServices __RPC_FAR * pRepository,
  454. IWbemObjectSink __RPC_FAR * pHandler,
  455. IWbemContext __RPC_FAR *pCtx
  456. )
  457. {
  458. HRESULT hr = WBEM_E_FAILED;
  459. m_pClass = new CWMIProcessClass(0);
  460. if( m_pClass )
  461. {
  462. hr = m_pClass->Initialize();
  463. if( S_OK == hr )
  464. {
  465. m_pClass->WMI()->SetWMIPointers(pList,pServices,pRepository,pHandler,pCtx);
  466. m_pClass->SetHiPerf(TRUE);
  467. m_pClass->SetClass(wcs);
  468. m_pWDM = new CProcessHiPerfDataBlock;
  469. if( m_pWDM )
  470. {
  471. m_pWDM->SetDesiredAccess(uDesiredAccess);
  472. m_pWDM->UpdateNamespace(fUpdate);
  473. m_pWDM->SetClassProcessPtr(m_pClass);
  474. m_fInit = TRUE;
  475. }
  476. hr = S_OK;
  477. }
  478. }
  479. return hr;
  480. }
  481. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  482. CWMIHiPerfShell::~CWMIHiPerfShell()
  483. {
  484. if( m_fAutoCleanup )
  485. {
  486. SAFE_DELETE_PTR(m_pClass);
  487. }
  488. SAFE_DELETE_PTR(m_pWDM);
  489. }
  490. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  491. HRESULT CWMIHiPerfShell::QueryAllHiPerfData()
  492. {
  493. HRESULT hr = WBEM_E_FAILED;
  494. if( m_fInit )
  495. {
  496. //===============================================================
  497. // There is only one handle for this class, so see if we can
  498. // get it
  499. //===============================================================
  500. CAutoBlock(m_pHiPerfMap->GetCriticalSection());
  501. HANDLE WMIHandle = 0;
  502. hr = m_pWDM->GetWMIHandle(WMIHandle);
  503. if( SUCCEEDED(hr))
  504. {
  505. // =====================================================
  506. // Query for all of the objects for this class
  507. // Add all the objects at once into the enumerator
  508. // Handles are guaranteed to be open at this time
  509. //======================================================
  510. hr = QueryAllInstances(WMIHandle,NULL);
  511. }
  512. }
  513. return hr;
  514. }
  515. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  516. HRESULT CWMIHiPerfShell::RefreshCompleteList()
  517. {
  518. //=======================================================================
  519. // Go through all the enumerators and instances to refresh everything
  520. //=======================================================================
  521. HRESULT hr = WBEM_E_FAILED;
  522. if( m_fInit )
  523. {
  524. CAutoBlock(m_pHiPerfMap->GetCriticalSection());
  525. HANDLE WMIHandle = 0;
  526. IWbemHiPerfEnum * pEnum = NULL;
  527. CWMIProcessClass * pSavedClass = m_pClass;
  528. //==============================================================
  529. //
  530. //==============================================================
  531. hr = m_pHiPerfMap->GetFirstHandle( WMIHandle, m_pClass, pEnum);
  532. while( hr == S_OK )
  533. {
  534. if( WMIHandle )
  535. {
  536. m_pWDM->SetClassProcessPtr(m_pClass);
  537. if( pEnum )
  538. {
  539. hr = QueryAllInstances(WMIHandle,pEnum);
  540. }
  541. else
  542. {
  543. hr = QuerySingleInstance(WMIHandle);
  544. }
  545. if(SUCCEEDED(hr))
  546. {
  547. hr = m_pHiPerfMap->GetNextHandle(WMIHandle,m_pClass,pEnum);
  548. }
  549. }
  550. if( hr == WBEM_S_NO_MORE_DATA )
  551. {
  552. hr = S_OK;
  553. break;
  554. }
  555. }
  556. //================================================================
  557. m_pClass = pSavedClass;
  558. }
  559. return hr;
  560. }
  561. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  562. HRESULT CWMIHiPerfShell::HiPerfQuerySingleInstance(WCHAR * wcsInstance)
  563. {
  564. HRESULT hr = WBEM_E_FAILED;
  565. if( m_fInit )
  566. {
  567. //======================================================
  568. // Go through the list of handles and get the handles
  569. // to send and how many there are, also, get the
  570. // instance names
  571. //======================================================
  572. CAutoBlock(m_pHiPerfMap->GetCriticalSection());
  573. IWbemObjectAccess * pAccess = NULL;
  574. CWMIProcessClass * pClass = NULL;
  575. HANDLE WMIHandle = 0;
  576. ULONG_PTR lTmp = (ULONG_PTR)pAccess;
  577. hr = m_pHiPerfMap->FindHandleAndGetClassPtr(WMIHandle,lTmp, pClass);
  578. if( SUCCEEDED(hr))
  579. {
  580. hr = QuerySingleInstance(WMIHandle);
  581. }
  582. }
  583. return hr;
  584. }
  585. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  586. HRESULT CWMIHiPerfShell::AddAccessObjectToRefresher(IWbemObjectAccess *pAccess,
  587. IWbemObjectAccess ** ppRefreshable,
  588. ULONG_PTR *plId)
  589. {
  590. HRESULT hr = WBEM_E_FAILED;
  591. if( m_fInit )
  592. {
  593. //======================================================
  594. // Get the definition of the class we are working with
  595. //======================================================
  596. hr = m_pClass->SetAccess(pAccess);
  597. if( SUCCEEDED(hr))
  598. {
  599. CAutoBlock(m_pHiPerfMap->GetCriticalSection());
  600. HANDLE WMIHandle = 0;
  601. CLSID Guid;
  602. hr = m_pWDM->GetWMIHandle(WMIHandle);
  603. if( SUCCEEDED(hr))
  604. {
  605. // =======================================================
  606. // We have the WMI Handle, now add it to the hi perf map
  607. // for this refresher
  608. // =======================================================
  609. if( m_pClass->GetANewAccessInstance() )
  610. {
  611. //====================================================
  612. // Set the flag so we don't get a new instance for
  613. // this anymore
  614. //====================================================
  615. m_pClass->GetNewInstance(FALSE);
  616. hr = m_pClass->SetKeyFromAccessPointer();
  617. if( SUCCEEDED(hr))
  618. {
  619. *ppRefreshable = m_pClass->GetAccessInstancePtr();
  620. *plId = (ULONG_PTR)(*ppRefreshable);
  621. hr = m_pHiPerfMap->Add(WMIHandle, *plId, m_pClass, NULL );
  622. }
  623. }
  624. }
  625. }
  626. }
  627. return hr;
  628. }
  629. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  630. HRESULT CWMIHiPerfShell::AddEnumeratorObjectToRefresher(IWbemHiPerfEnum* pHiPerfEnum, ULONG_PTR *plId)
  631. {
  632. HRESULT hr = WBEM_E_FAILED;
  633. if( m_fInit )
  634. {
  635. //===============================================================
  636. // There is only one handle for this class, so see if we can
  637. // get it
  638. //===============================================================
  639. HANDLE WMIHandle = 0;
  640. CLSID Guid;
  641. CAutoBlock(m_pHiPerfMap->GetCriticalSection());
  642. hr = m_pWDM->GetWMIHandle(WMIHandle);
  643. if( SUCCEEDED(hr))
  644. {
  645. *plId = (ULONG_PTR)pHiPerfEnum;
  646. hr = m_pHiPerfMap->Add(WMIHandle, *plId, m_pClass,pHiPerfEnum);
  647. }
  648. }
  649. return hr;
  650. }
  651. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  652. HRESULT CWMIHiPerfShell::RemoveObjectFromHandleMap(ULONG_PTR lHiPerfId)
  653. {
  654. HRESULT hr = WBEM_E_FAILED;
  655. if( m_fInit )
  656. {
  657. HANDLE hHandle = 0;
  658. CHandleMap *pHandleMap = m_pClass->WMI()->HandleMap();
  659. CAutoBlock(m_pHiPerfMap->GetCriticalSection());
  660. //==============================================================
  661. // First, delete the object from the map
  662. //==============================================================
  663. hr = m_pHiPerfMap->Delete( hHandle, lHiPerfId );
  664. if( SUCCEEDED(hr))
  665. {
  666. //==========================================================
  667. // If we got a handle back, then we know it is an access
  668. // instance and we need to release the WMI Handle
  669. //==========================================================
  670. if( hHandle ){
  671. hr = pHandleMap->ReleaseHandle(hHandle);
  672. }
  673. }
  674. }
  675. return hr;
  676. }
  677. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  678. // PRIVATE
  679. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  680. HRESULT CWMIHiPerfShell::QueryAllInstances(HANDLE WMIHandle,IWbemHiPerfEnum* pHiPerfEnum)
  681. {
  682. HRESULT hr = WBEM_E_FAILED;
  683. if( m_fInit )
  684. {
  685. long lCount = 0;
  686. HandleList ids;
  687. //==================================================
  688. // Collect all of the WDM Handles to query all at
  689. // once.
  690. //==================================================
  691. // 170635
  692. if(SUCCEEDED(hr = ids.Add(WMIHandle)))
  693. {
  694. // =====================================================
  695. // Query for all of the objects for this class
  696. // Add all the objects at once into the enumerator
  697. // Handles are guaranteed to be open at this time
  698. //======================================================
  699. HANDLE * pHandles = (HANDLE*)*(ids.List());
  700. hr = m_pWDM->HiPerfQueryAllData(pHandles,ids.Size());
  701. //======================================================
  702. // If we got the data then process it
  703. //======================================================
  704. if( SUCCEEDED(hr))
  705. {
  706. //======================================================
  707. // Get the list of ptrs
  708. //======================================================
  709. AccessList AccessList;
  710. while( TRUE )
  711. {
  712. hr = m_pWDM->ReadWMIDataBlockAndPutIntoWbemInstance();
  713. if( hr == S_OK )
  714. {
  715. IWbemObjectAccess * p = m_pClass->GetAccessInstancePtr();
  716. if(SUCCEEDED(hr = AccessList.Add(p)))
  717. {
  718. lCount++;
  719. }
  720. }
  721. if( hr != S_OK )
  722. {
  723. break;
  724. }
  725. if( !m_pWDM->MoreToProcess() )
  726. {
  727. break;
  728. }
  729. if( !pHiPerfEnum )
  730. {
  731. m_pClass->SendInstanceBack();
  732. }
  733. }
  734. //======================================================
  735. // Now, once we have collected them, send them off
  736. // if asked to
  737. //======================================================
  738. if( pHiPerfEnum )
  739. {
  740. if( lCount > 0 )
  741. {
  742. long * pLong = new long[lCount];
  743. if(pLong)
  744. {
  745. for(long l = 0; l < lCount; l++)
  746. {
  747. pLong[l] = l;
  748. }
  749. IWbemObjectAccess ** pAccess = (IWbemObjectAccess**)AccessList.List();
  750. // Remove all the objects in the enumerator before adding the object
  751. pHiPerfEnum->RemoveAll(0);
  752. hr = pHiPerfEnum->AddObjects( 0L, AccessList.Size(), pLong, pAccess);
  753. SAFE_DELETE_ARRAY(pLong);
  754. }
  755. else
  756. {
  757. hr = E_OUTOFMEMORY;
  758. }
  759. }
  760. }
  761. }
  762. }
  763. }
  764. return hr;
  765. }
  766. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  767. HRESULT CWMIHiPerfShell::QuerySingleInstance(HANDLE WMIHandle)
  768. {
  769. HRESULT hr = WBEM_E_FAILED;
  770. if( m_fInit )
  771. {
  772. CVARIANT varName;
  773. hr = m_pClass->GetKeyFromAccessPointer((CVARIANT *)&varName);
  774. if(SUCCEEDED(hr))
  775. {
  776. WCHAR * p = varName.GetStr();
  777. if(p)
  778. {
  779. hr = m_pWDM->HiPerfQuerySingleInstance(&WMIHandle, &p, 1,1);
  780. //======================================================
  781. // If we got the data, process it
  782. //======================================================
  783. if( SUCCEEDED(hr))
  784. {
  785. hr = m_pWDM->ReadWMIDataBlockAndPutIntoWbemInstance();
  786. // if( hr == S_OK )
  787. // {
  788. // m_pClass->SendInstanceBack();
  789. // }
  790. }
  791. }
  792. else
  793. {
  794. hr = WBEM_E_FAILED;
  795. }
  796. }
  797. }
  798. return hr;
  799. }