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.

885 lines
26 KiB

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