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.

955 lines
27 KiB

  1. /////////////////////////////////////////////////////////////////////
  2. //
  3. // WMIProv.CPP
  4. //
  5. // Module: WMI Provider class methods
  6. //
  7. // Purpose: Provider class definition. An object of this class is
  8. // created by the class factory for each connection.
  9. //
  10. // Copyright (c) 1997-2002 Microsoft Corporation, All Rights Reserved
  11. //
  12. /////////////////////////////////////////////////////////////////////
  13. #include "precomp.h"
  14. extern long glInits;
  15. extern long glProvObj;
  16. extern long glEventsRegistered ;
  17. extern CWMIEvent * g_pBinaryMofEvent;
  18. extern CCriticalSection * g_pLoadUnloadCs ;
  19. #include "wmiguard.h"
  20. extern WmiGuard * pGuard;
  21. #include <helper.h>
  22. typedef OnDeleteObj0 <WmiGuard, HRESULT(WmiGuard:: *)(), WmiGuard::Leave> WmiGuardLeave;
  23. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  24. HRESULT VerifyLocalEventsAreRegistered()
  25. {
  26. HRESULT hr = E_FAIL ;
  27. if( !g_pBinaryMofEvent->RegisterForInternalEvents())
  28. {
  29. InterlockedCompareExchange (&glEventsRegistered, 0, glEventsRegistered);
  30. ERRORTRACE((THISPROVIDER,"Failed Registeration for Mof Events\n"));
  31. }
  32. else
  33. {
  34. DEBUGTRACE((THISPROVIDER,"Successfully Registered for Mof Events\n"));
  35. hr = S_OK ;
  36. }
  37. return hr ;
  38. }
  39. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  40. HRESULT ProcessAllBinaryMofs (
  41. CHandleMap * pMap,
  42. IWbemServices __RPC_FAR *pNamespace,
  43. IWbemServices __RPC_FAR *pRepository,
  44. IWbemContext __RPC_FAR *pCtx
  45. )
  46. {
  47. HRESULT hr = E_FAIL ;
  48. CWMIBinMof Mof;
  49. if( SUCCEEDED( hr = Mof.Initialize(pMap,TRUE,WMIGUID_EXECUTE|WMIGUID_QUERY,pNamespace,pRepository,NULL,pCtx)))
  50. {
  51. HRESULT t_TempResult = RevertToSelf();
  52. #ifdef DBG
  53. if ( FAILED ( t_TempResult ) )
  54. {
  55. DebugBreak();
  56. }
  57. #endif DBG
  58. Mof.ProcessListOfWMIBinaryMofsFromWMI();
  59. CheckImpersonationLevel();
  60. }
  61. return hr ;
  62. }
  63. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  64. HRESULT InitializeBinaryMofs (
  65. IWbemServices __RPC_FAR *pNamespace,
  66. IWbemServices __RPC_FAR *pRepository,
  67. IWbemContext __RPC_FAR *pCtx
  68. )
  69. {
  70. HRESULT hr = E_FAIL ;
  71. //==============================================================
  72. // Register for hardcoded event to be notified of WMI updates
  73. // make it a member var, so it stays around for the life
  74. // of the provider
  75. //==============================================================
  76. try
  77. {
  78. g_pBinaryMofEvent->SetEventServices(pNamespace);
  79. g_pBinaryMofEvent->SetEventRepository(pRepository);
  80. g_pBinaryMofEvent->SetEventContext(pCtx);
  81. hr = VerifyLocalEventsAreRegistered();
  82. }
  83. STANDARD_CATCH
  84. if ( FAILED ( hr ) )
  85. {
  86. //
  87. // must clear global object so next
  88. // initialization will have a chance
  89. //
  90. g_pBinaryMofEvent->ReleaseAllPointers () ;
  91. }
  92. return hr ;
  93. }
  94. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  95. //*********************************************************************
  96. // Check the impersonation level
  97. //*********************************************************************
  98. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  99. HRESULT CheckImpersonationLevel()
  100. {
  101. HRESULT hr = WBEM_E_ACCESS_DENIED;
  102. HANDLE hThreadTok;
  103. if (IsNT())
  104. {
  105. if( GetUserThreadToken(&hThreadTok) )
  106. {
  107. DWORD dwImp, dwBytesReturned;
  108. if (GetTokenInformation( hThreadTok, TokenImpersonationLevel, &dwImp, sizeof(DWORD), &dwBytesReturned))
  109. {
  110. // Is the impersonation level Impersonate?
  111. if ((dwImp == SecurityImpersonation) || ( dwImp == SecurityDelegation) )
  112. {
  113. hr = WBEM_S_NO_ERROR;
  114. }
  115. else
  116. {
  117. hr = WBEM_E_ACCESS_DENIED;
  118. ERRORTRACE((THISPROVIDER,IDS_ImpersonationFailed));
  119. }
  120. }
  121. else
  122. {
  123. hr = WBEM_E_FAILED;
  124. ERRORTRACE((THISPROVIDER,IDS_ImpersonationFailed));
  125. }
  126. // Done with this handle
  127. CloseHandle(hThreadTok);
  128. }
  129. }
  130. else
  131. {
  132. // let win9X in...
  133. hr = WBEM_S_NO_ERROR;
  134. }
  135. return hr;
  136. }
  137. /////////////////////////////////////////////////////////////////////
  138. HRESULT GetRepository (
  139. /* [in] */ LPWSTR pszNamespace,
  140. /* [in] */ LPWSTR pszLocale,
  141. /* [in] */ IWbemContext __RPC_FAR *pCtx,
  142. /* [out] */ IWbemServices __RPC_FAR ** pServices
  143. )
  144. {
  145. HRESULT hr = WBEM_E_INVALID_PARAMETER ;
  146. if ( pszNamespace )
  147. {
  148. IWbemLocator* pLocator = NULL ;
  149. hr = ::CoCreateInstance ( __uuidof ( WbemLocator ), NULL, CLSCTX_INPROC_SERVER, __uuidof ( IWbemLocator ), (void**) &pLocator ) ;
  150. if ( SUCCEEDED ( hr ) )
  151. {
  152. // release upon destruction
  153. OnDeleteObj0 <IWbemLocator, ULONG(__stdcall IWbemLocator:: *)(), IWbemLocator::Release> pLocatorRelease ( pLocator ) ;
  154. hr = pLocator->ConnectServer (
  155. pszNamespace ,
  156. NULL ,
  157. NULL ,
  158. pszLocale ,
  159. WBEM_FLAG_CONNECT_REPOSITORY_ONLY ,
  160. NULL ,
  161. pCtx ,
  162. pServices
  163. ) ;
  164. }
  165. }
  166. return hr ;
  167. }
  168. ////////////////////////////////////////////////////////////////////
  169. HRESULT InitializeProvider (
  170. /* [in] */ LPWSTR pszNamespace,
  171. /* [in] */ LPWSTR pszLocale,
  172. /* [in] */ IWbemServices __RPC_FAR *pNamespace,
  173. /* [in] */ IWbemContext __RPC_FAR *pCtx,
  174. /* [in] */ IWbemProviderInitSink __RPC_FAR *pInitSink,
  175. /* [in] */ CHandleMap * pMap,
  176. /* [out] */ IWbemServices ** ppServices,
  177. /* [out] */ IWbemServices ** ppRepository,
  178. /* [out] */ IWbemContext ** ppCtx,
  179. /* [in] */ BOOL bProcessMof
  180. )
  181. {
  182. //
  183. // avoid collision with un-initialization
  184. //
  185. g_pLoadUnloadCs->Enter ();
  186. InterlockedIncrement ( &glProvObj ) ;
  187. g_pLoadUnloadCs->Leave ();
  188. HRESULT hr = WBEM_E_INVALID_PARAMETER;
  189. if(pNamespace!=NULL)
  190. {
  191. //===============================================
  192. (*ppServices) = pNamespace;
  193. (*ppServices)->AddRef();
  194. if ( pCtx )
  195. {
  196. if ( ppCtx )
  197. {
  198. (*ppCtx) = pCtx;
  199. (*ppCtx)->AddRef();
  200. }
  201. }
  202. if ( SUCCEEDED ( hr = GetRepository ( pszNamespace, pszLocale, pCtx, ppRepository ) ) )
  203. {
  204. if ( S_OK == ( hr = pGuard->TryEnter () ) )
  205. {
  206. WmiGuardLeave wql ( pGuard ) ;
  207. if( 0 == InterlockedCompareExchange ( &glEventsRegistered, 1, 0 ) )
  208. {
  209. hr = InitializeBinaryMofs ( *ppServices, *ppRepository, pCtx ) ;
  210. }
  211. }
  212. if ( SUCCEEDED ( hr ) && bProcessMof )
  213. {
  214. if( 0 == InterlockedCompareExchange ( &glInits, 1, 0 ) )
  215. {
  216. try
  217. {
  218. hr = ProcessAllBinaryMofs ( pMap, *ppServices, *ppRepository, pCtx ) ;
  219. }
  220. STANDARD_CATCH
  221. if ( FAILED ( hr ) )
  222. {
  223. //
  224. // let's try to process mofs next time
  225. //
  226. InterlockedCompareExchange ( &glInits, 0, glInits ) ;
  227. }
  228. }
  229. }
  230. }
  231. if ( SUCCEEDED ( hr ) )
  232. {
  233. pInitSink->SetStatus ( WBEM_S_INITIALIZED, 0 ) ;
  234. }
  235. else
  236. {
  237. pInitSink->SetStatus ( WBEM_E_FAILED , 0 ) ;
  238. }
  239. }
  240. return hr;
  241. }
  242. /////////////////////////////////////////////////////////////////////////////////////////
  243. //
  244. // this function assumes that InitializeProvider (called from within IWbemProviderInit)
  245. // is always called. That basically means that IWbemProviderInit is always called.
  246. //
  247. /////////////////////////////////////////////////////////////////////////////////////////
  248. HRESULT UnInitializeProvider ( )
  249. {
  250. HRESULT hr = WBEM_S_FALSE ;
  251. BOOL bContinue = FALSE;
  252. {
  253. //
  254. // let's find out if this is
  255. // possibly last provider here
  256. //
  257. CAutoBlock block (g_pLoadUnloadCs);
  258. //
  259. // check to see if we did InitializeProvider
  260. //
  261. if ( 0 <= InterlockedCompareExchange ( &glProvObj, glProvObj, 0 ) )
  262. {
  263. if ( 0 == InterlockedDecrement ( &glProvObj ) )
  264. {
  265. bContinue = TRUE ;
  266. }
  267. }
  268. else
  269. {
  270. #ifdef _DEBUG
  271. DebugBreak () ;
  272. #endif
  273. }
  274. }
  275. if ( bContinue )
  276. {
  277. //
  278. // registartion and unregistration must be exclusive
  279. //
  280. if ( SUCCEEDED ( pGuard->Enter() ) )
  281. {
  282. WmiGuardLeave wgl ( pGuard );
  283. //
  284. // verify that binary notifications were
  285. // successfully initialized
  286. //
  287. if ( 1 == InterlockedCompareExchange ( &glEventsRegistered, glEventsRegistered, 1 ) )
  288. {
  289. g_pBinaryMofEvent->DeleteBinaryMofResourceEvent();
  290. //
  291. // avoid collision with initialization
  292. //
  293. CAutoBlock block (g_pLoadUnloadCs);
  294. //
  295. // check to see there was not provider re-entrancy
  296. //
  297. if ( 0 == InterlockedCompareExchange ( &glProvObj, glProvObj, 0 ) )
  298. {
  299. g_pBinaryMofEvent->ReleaseAllPointers();
  300. InterlockedCompareExchange (&glEventsRegistered, 0, glEventsRegistered);
  301. DEBUGTRACE((THISPROVIDER,"No longer registered for Mof events\n"));
  302. //
  303. // we want to process binary mofs next initialize
  304. // as this was the last provider
  305. //
  306. InterlockedCompareExchange ( &glInits, 0, 1 );
  307. }
  308. else
  309. {
  310. //
  311. // seems like there was a callback instantiating
  312. // another provider as it was adding/deleting
  313. // classes
  314. //
  315. // as InitializeProvider now skipped InitializeBinaryMofs
  316. // we must re-register what was cancelled here
  317. //
  318. // this way we keep possibility to respond
  319. // to add/delete binary mofs
  320. //
  321. try
  322. {
  323. hr = VerifyLocalEventsAreRegistered();
  324. }
  325. STANDARD_CATCH
  326. if ( FAILED ( hr ) )
  327. {
  328. //
  329. // must clear global object so next
  330. // initialization will have a chance
  331. //
  332. g_pBinaryMofEvent->ReleaseAllPointers () ;
  333. //
  334. // let's try to process mofs next time
  335. //
  336. InterlockedCompareExchange ( &glInits, 0, glInits ) ;
  337. }
  338. }
  339. }
  340. }
  341. }
  342. return hr ;
  343. }
  344. ////////////////////////////////////////////////////////////////////
  345. //******************************************************************
  346. //
  347. // PUBLIC FUNCTIONS
  348. //
  349. //******************************************************************
  350. ////////////////////////////////////////////////////////////////////
  351. ////////////////////////////////////////////////////////////////////
  352. //
  353. // CWMI_Prov::CWMI_Prov
  354. //
  355. ////////////////////////////////////////////////////////////////////
  356. CWMI_Prov::CWMI_Prov() :
  357. m_cRef ( 0 ),
  358. m_pIWbemServices ( NULL ),
  359. m_pIWbemRepository ( NULL ),
  360. m_bInitialized ( FALSE )
  361. {
  362. if ( m_HandleMap.IsValid () )
  363. {
  364. #if defined(_WIN64)
  365. BOOL bAllocated = FALSE ;
  366. m_Allocator = NULL;
  367. m_HashTable = NULL;
  368. m_ID = 0;
  369. try
  370. {
  371. if ( TRUE == InitializeCriticalSectionAndSpinCount ( &m_CS, 0 ) )
  372. {
  373. WmiAllocator t_Allocator ;
  374. WmiStatusCode t_StatusCode = t_Allocator.New (
  375. ( void ** ) & m_Allocator ,
  376. sizeof ( WmiAllocator )
  377. ) ;
  378. if ( t_StatusCode == e_StatusCode_Success )
  379. {
  380. :: new ( ( void * ) m_Allocator ) WmiAllocator ;
  381. t_StatusCode = m_Allocator->Initialize () ;
  382. if ( t_StatusCode != e_StatusCode_Success )
  383. {
  384. t_Allocator.Delete ( ( void * ) m_Allocator ) ;
  385. m_Allocator = NULL;
  386. DeleteCriticalSection(&m_CS);
  387. }
  388. else
  389. {
  390. m_HashTable = ::new WmiHashTable <LONG, ULONG_PTR, 17> ( *m_Allocator ) ;
  391. t_StatusCode = m_HashTable->Initialize () ;
  392. if ( t_StatusCode != e_StatusCode_Success )
  393. {
  394. m_HashTable->UnInitialize () ;
  395. ::delete m_HashTable;
  396. m_HashTable = NULL;
  397. m_Allocator->UnInitialize ();
  398. t_Allocator.Delete ( ( void * ) m_Allocator ) ;
  399. m_Allocator = NULL;
  400. DeleteCriticalSection(&m_CS);
  401. }
  402. else
  403. {
  404. bAllocated = TRUE ;
  405. }
  406. }
  407. }
  408. else
  409. {
  410. m_Allocator = NULL;
  411. DeleteCriticalSection(&m_CS);
  412. }
  413. }
  414. }
  415. catch (...)
  416. {
  417. }
  418. if ( bAllocated )
  419. {
  420. #endif
  421. m_bInitialized = TRUE ;
  422. #if defined(_WIN64)
  423. }
  424. #endif
  425. }
  426. if ( m_bInitialized )
  427. {
  428. DEBUGTRACE((THISPROVIDER,"Instance Provider constructed\n"));
  429. InterlockedIncrement(&g_cObj);
  430. }
  431. else
  432. {
  433. ERRORTRACE((THISPROVIDER,"Instance Provider construction failed\n"));
  434. }
  435. }
  436. ////////////////////////////////////////////////////////////////////
  437. //
  438. // CWMI_Prov::~CWMI_Prov
  439. //
  440. ////////////////////////////////////////////////////////////////////
  441. CWMI_Prov::~CWMI_Prov(void)
  442. {
  443. if ( m_HandleMap.IsValid () )
  444. {
  445. #if defined(_WIN64)
  446. if (m_HashTable)
  447. {
  448. WmiAllocator t_Allocator ;
  449. m_HashTable->UnInitialize () ;
  450. ::delete m_HashTable;
  451. m_HashTable = NULL;
  452. m_Allocator->UnInitialize ();
  453. t_Allocator.Delete ( ( void * ) m_Allocator ) ;
  454. m_Allocator = NULL;
  455. DeleteCriticalSection(&m_CS);
  456. #endif
  457. if ( m_bInitialized )
  458. {
  459. UnInitializeProvider ( ) ;
  460. DEBUGTRACE((THISPROVIDER,"Instance Provider destructed\n"));
  461. InterlockedDecrement(&g_cObj);
  462. }
  463. SAFE_RELEASE_PTR( m_pIWbemServices );
  464. SAFE_RELEASE_PTR( m_pIWbemRepository );
  465. #if defined(_WIN64)
  466. }
  467. #endif
  468. }
  469. }
  470. ////////////////////////////////////////////////////////////////////
  471. //
  472. // QueryInterface
  473. //
  474. ////////////////////////////////////////////////////////////////////
  475. STDMETHODIMP CWMI_Prov::QueryInterface(REFIID riid, PPVOID ppvObj)
  476. {
  477. HRESULT hr = E_NOINTERFACE;
  478. *ppvObj = NULL;
  479. if (IsEqualIID(riid, IID_IUnknown))
  480. {
  481. *ppvObj =(IWbemServices *) this ;
  482. }
  483. else if (IsEqualIID(riid, IID_IWbemServices))
  484. {
  485. *ppvObj =(IWbemServices *) this ;
  486. }
  487. else if (IsEqualIID(riid, IID_IWbemProviderInit))
  488. {
  489. *ppvObj = (IWbemProviderInit *) this ;
  490. }
  491. else if(riid == IID_IWbemProviderInit)
  492. {
  493. *ppvObj = (LPVOID)(IWbemProviderInit*)this;
  494. }
  495. else if (riid == IID_IWbemHiPerfProvider)
  496. {
  497. *ppvObj = (LPVOID)(IWbemHiPerfProvider*)this;
  498. }
  499. if(*ppvObj)
  500. {
  501. AddRef();
  502. hr = NOERROR;
  503. }
  504. return hr;
  505. }
  506. /////////////////////////////////////////////////////////////////////
  507. HRESULT CWMI_Prov::Initialize(
  508. /* [in] */ LPWSTR pszUser,
  509. /* [in] */ LONG lFlags,
  510. /* [in] */ LPWSTR pszNamespace,
  511. /* [in] */ LPWSTR pszLocale,
  512. /* [in] */ IWbemServices __RPC_FAR *pNamespace,
  513. /* [in] */ IWbemContext __RPC_FAR *pCtx,
  514. /* [in] */ IWbemProviderInitSink __RPC_FAR *pInitSink)
  515. {
  516. return InitializeProvider (
  517. pszNamespace,
  518. pszLocale,
  519. pNamespace,
  520. pCtx,
  521. pInitSink,
  522. &m_HandleMap,
  523. &m_pIWbemServices,
  524. &m_pIWbemRepository
  525. ) ;
  526. }
  527. //////////////////////////////////////////////////////////////////////
  528. STDMETHODIMP_(ULONG) CWMI_Prov::AddRef(void)
  529. {
  530. return InterlockedIncrement((long*)&m_cRef);
  531. }
  532. STDMETHODIMP_(ULONG) CWMI_Prov::Release(void)
  533. {
  534. ULONG cRef = InterlockedDecrement( (long*) &m_cRef);
  535. if ( !cRef ){
  536. delete this;
  537. return 0;
  538. }
  539. return cRef;
  540. }
  541. ////////////////////////////////////////////////////////////////////
  542. //
  543. // CWMI_Prov::OpenNamespace
  544. //
  545. ////////////////////////////////////////////////////////////////////
  546. HRESULT CWMI_Prov::OpenNamespace(
  547. /* [in] */ BSTR Namespace,
  548. /* [in] */ long lFlags,
  549. /* [in] */ IWbemContext __RPC_FAR *pCtx,
  550. /* [unique][in][out] */ IWbemServices __RPC_FAR *__RPC_FAR *ppWorkingNamespace,
  551. /* [unique][in][out] */ IWbemCallResult __RPC_FAR *__RPC_FAR *ppResult)
  552. {
  553. return WBEM_E_PROVIDER_NOT_CAPABLE ;
  554. }
  555. ////////////////////////////////////////////////////////////////////
  556. //
  557. // CWMI_Prov::CreateInstanceEnumAsync
  558. //
  559. // Purpose: Asynchronously enumerates the instances of the
  560. // given class.
  561. //
  562. ////////////////////////////////////////////////////////////////
  563. HRESULT CWMI_Prov::CreateInstanceEnumAsync(BSTR wcsClass,
  564. long lFlags,
  565. IWbemContext __RPC_FAR *pCtx,
  566. IWbemObjectSink __RPC_FAR * pHandler)
  567. {
  568. HRESULT hr = WBEM_E_FAILED;
  569. SetStructuredExceptionHandler seh;
  570. CWMIStandardShell WMI;
  571. if( SUCCEEDED(WMI.Initialize(wcsClass,FALSE,&m_HandleMap,TRUE,WMIGUID_QUERY,m_pIWbemServices,m_pIWbemRepository,pHandler,pCtx)))
  572. {
  573. if (SUCCEEDED(hr = CheckImpersonationLevel()))
  574. {
  575. //============================================================
  576. // Init and get the WMI Data block
  577. //============================================================
  578. if( pHandler != NULL )
  579. {
  580. //============================================================
  581. // Parse through all of it
  582. //============================================================
  583. try
  584. {
  585. hr = WMI.ProcessAllInstances();
  586. }
  587. STANDARD_CATCH
  588. }
  589. }
  590. WMI.SetErrorMessage(hr);
  591. }
  592. return hr;
  593. }
  594. //***************************************************************************
  595. HRESULT CWMI_Prov::ExecQueryAsync( BSTR QueryLanguage,
  596. BSTR Query,
  597. long lFlags,
  598. IWbemContext __RPC_FAR *pCtx,
  599. IWbemObjectSink __RPC_FAR *pHandler)
  600. {
  601. WCHAR wcsClass[_MAX_PATH+2];
  602. HRESULT hr = WBEM_E_FAILED;
  603. SetStructuredExceptionHandler seh;
  604. BOOL fRc = FALSE;
  605. //============================================================
  606. // Do a check of arguments and make sure we have pointers
  607. //============================================================
  608. if( pHandler == NULL )
  609. {
  610. return WBEM_E_INVALID_PARAMETER;
  611. }
  612. try
  613. {
  614. //============================================================
  615. // Get the properties and class to get
  616. //============================================================
  617. wcsClass [ 0 ] = 0;
  618. fRc = GetParsedPropertiesAndClass(Query,wcsClass,_MAX_PATH+2);
  619. }
  620. STANDARD_CATCH
  621. if ( fRc )
  622. {
  623. CWMIStandardShell WMI;
  624. if( SUCCEEDED(WMI.Initialize(wcsClass,FALSE,&m_HandleMap,TRUE,WMIGUID_NOTIFICATION|WMIGUID_QUERY,m_pIWbemServices,m_pIWbemRepository,pHandler,pCtx)))
  625. {
  626. if( fRc )
  627. {
  628. hr = CheckImpersonationLevel();
  629. }
  630. hr = WMI.SetErrorMessage(hr);
  631. }
  632. }
  633. return hr;
  634. }
  635. //***************************************************************************
  636. //
  637. // CWMI_Prov::GetObjectAsync
  638. //
  639. // Purpose: Asynchronously creates an instance given a particular path value.
  640. //
  641. // NOTE 1: If there is an instance name in the returned WNODE, then this is a
  642. // dynamic instance. You can tell because the pWNSI->OffsetInstanceName
  643. // field will not be blank. If this is the case, then the name will not
  644. // be contained within the datablock, but must instead be retrieved
  645. // from the WNODE. See NOTE 1, below.
  646. //
  647. //***************************************************************************
  648. HRESULT CWMI_Prov::GetObjectAsync(BSTR ObjectPath, long lFlags,
  649. IWbemContext __RPC_FAR *pCtx,
  650. IWbemObjectSink __RPC_FAR * pHandler )
  651. {
  652. HRESULT hr = WBEM_E_NOT_FOUND;
  653. SetStructuredExceptionHandler seh;
  654. WCHAR wcsClass[_MAX_PATH*2];
  655. WCHAR wcsInstance[_MAX_PATH*2];
  656. //============================================================
  657. // Do a check of arguments and make sure we have pointers
  658. //============================================================
  659. if(ObjectPath == NULL || pHandler == NULL )
  660. {
  661. return WBEM_E_INVALID_PARAMETER;
  662. }
  663. try
  664. {
  665. //============================================================
  666. // Get the path and instance name
  667. //============================================================
  668. wcsClass [ 0 ] = 0;
  669. wcsInstance [ 0 ] = 0;
  670. hr = GetParsedPath( ObjectPath,wcsClass,_MAX_PATH*2,wcsInstance,_MAX_PATH*2,m_pIWbemServices );
  671. }
  672. STANDARD_CATCH
  673. if( SUCCEEDED(hr) )
  674. {
  675. if (SUCCEEDED(hr = CheckImpersonationLevel()))
  676. {
  677. try
  678. {
  679. CWMIStandardShell WMI;
  680. if( SUCCEEDED(WMI.Initialize(wcsClass,FALSE,&m_HandleMap,TRUE,WMIGUID_QUERY,m_pIWbemServices,m_pIWbemRepository,pHandler,pCtx)))
  681. {
  682. //============================================================
  683. // Get the WMI Block
  684. //============================================================
  685. hr = WMI.ProcessSingleInstance(wcsInstance);
  686. hr = WMI.SetErrorMessage(hr);
  687. }
  688. }
  689. STANDARD_CATCH
  690. }
  691. }
  692. return hr;
  693. }
  694. //***************************************************************************
  695. //
  696. // CWMI_Prov::PutInstanceAsync
  697. //
  698. // Purpose: Asynchronously put an instance.
  699. //
  700. //***************************************************************************
  701. HRESULT CWMI_Prov::PutInstanceAsync(IWbemClassObject __RPC_FAR * pIWbemClassObject,
  702. long lFlags,
  703. IWbemContext __RPC_FAR *pCtx,
  704. IWbemObjectSink __RPC_FAR *pHandler )
  705. {
  706. HRESULT hr = WBEM_E_FAILED;
  707. SetStructuredExceptionHandler seh;
  708. if(pIWbemClassObject == NULL || pHandler == NULL )
  709. {
  710. return WBEM_E_INVALID_PARAMETER;
  711. }
  712. //===========================================================
  713. // Get the class name
  714. //===========================================================
  715. CVARIANT vName;
  716. hr = pIWbemClassObject->Get(L"__CLASS", 0, &vName, NULL, NULL);
  717. if( SUCCEEDED(hr))
  718. {
  719. CWMIStandardShell WMI;
  720. if( SUCCEEDED(WMI.Initialize(vName.GetStr(),FALSE,&m_HandleMap,TRUE,WMIGUID_SET|WMIGUID_QUERY,m_pIWbemServices,m_pIWbemRepository,pHandler,pCtx)))
  721. {
  722. if (SUCCEEDED(hr = CheckImpersonationLevel()))
  723. {
  724. //=======================================================
  725. // If there is not a context object, then we know we are
  726. // supposed to put the whole thing, otherwise we are
  727. // supposed to put only the properties specified.
  728. //=======================================================
  729. try
  730. {
  731. if( !pCtx )
  732. {
  733. hr = WMI.FillInAndSubmitWMIDataBlob(pIWbemClassObject,PUT_WHOLE_INSTANCE,vName);
  734. }
  735. else
  736. {
  737. //===================================================
  738. // If we have a ctx object and the __PUT_EXTENSIONS
  739. // property is not specified, then we know we are
  740. // supposed to put the whole thing
  741. //===================================================
  742. CVARIANT vPut;
  743. if( SUCCEEDED(pCtx->GetValue(L"__PUT_EXT_PROPERTIES", 0, &vPut)))
  744. {
  745. hr = WMI.FillInAndSubmitWMIDataBlob(pIWbemClassObject,PUT_PROPERTIES_IN_LIST_ONLY,vPut);
  746. }
  747. else
  748. {
  749. hr = WMI.FillInAndSubmitWMIDataBlob(pIWbemClassObject,PUT_WHOLE_INSTANCE,vPut);
  750. }
  751. }
  752. }
  753. STANDARD_CATCH
  754. }
  755. }
  756. hr = WMI.SetErrorMessage(hr);
  757. }
  758. return hr;
  759. }
  760. /************************************************************************
  761. * *
  762. *CWMIMethod::ExecMethodAsync *
  763. * *
  764. *Purpose: This is the Async function implementation *
  765. ************************************************************************/
  766. STDMETHODIMP CWMI_Prov::ExecMethodAsync(BSTR ObjectPath,
  767. BSTR MethodName,
  768. long lFlags,
  769. IWbemContext __RPC_FAR * pCtx,
  770. IWbemClassObject __RPC_FAR * pIWbemClassObject,
  771. IWbemObjectSink __RPC_FAR * pHandler)
  772. {
  773. CVARIANT vName;
  774. HRESULT hr = WBEM_E_FAILED;
  775. IWbemClassObject * pClass = NULL; //This is an IWbemClassObject.
  776. WCHAR wcsClass[_MAX_PATH*2];
  777. WCHAR wcsInstance[_MAX_PATH*2];
  778. SetStructuredExceptionHandler seh;
  779. try
  780. {
  781. //============================================================
  782. // Get the path and instance name and check to make sure it
  783. // is valid
  784. //============================================================
  785. wcsClass [ 0 ] = 0;
  786. wcsInstance [ 0 ] = 0;
  787. hr = GetParsedPath( ObjectPath,wcsClass,_MAX_PATH*2,wcsInstance,_MAX_PATH*2,m_pIWbemServices);
  788. }
  789. STANDARD_CATCH
  790. if ( SUCCEEDED ( hr ) )
  791. {
  792. CWMIStandardShell WMI;
  793. if( SUCCEEDED(WMI.Initialize(wcsClass,FALSE,&m_HandleMap,TRUE,WMIGUID_EXECUTE|WMIGUID_QUERY,m_pIWbemServices,m_pIWbemRepository,pHandler,pCtx)))
  794. {
  795. if (SUCCEEDED(hr = CheckImpersonationLevel()))
  796. {
  797. //================================================================
  798. // We are ok, so proceed
  799. //================================================================
  800. hr = m_pIWbemServices->GetObject(wcsClass, 0, pCtx, &pClass, NULL);
  801. if( SUCCEEDED(hr) )
  802. {
  803. //==========================================================
  804. // Now, get the list of Input and Output parameters
  805. //==========================================================
  806. IWbemClassObject * pOutClass = NULL; //This is an IWbemClassObject.
  807. IWbemClassObject * pInClass = NULL; //This is an IWbemClassObject.
  808. hr = pClass->GetMethod(MethodName, 0, &pInClass, &pOutClass);
  809. if( SUCCEEDED(hr) )
  810. {
  811. try
  812. {
  813. hr = WMI.ExecuteMethod( wcsInstance, MethodName,pClass, pIWbemClassObject,pInClass, pOutClass);
  814. }
  815. STANDARD_CATCH
  816. }
  817. SAFE_RELEASE_PTR( pOutClass );
  818. SAFE_RELEASE_PTR( pInClass );
  819. SAFE_RELEASE_PTR( pClass );
  820. }
  821. }
  822. hr = WMI.SetErrorMessage(hr);
  823. }
  824. }
  825. return hr;
  826. }
  827. /////////////////////////////////////////////////////////////////////
  828. HRESULT CWMIHiPerfProvider::Initialize(
  829. /* [in] */ LPWSTR pszUser,
  830. /* [in] */ LONG lFlags,
  831. /* [in] */ LPWSTR pszNamespace,
  832. /* [in] */ LPWSTR pszLocale,
  833. /* [in] */ IWbemServices __RPC_FAR *pNamespace,
  834. /* [in] */ IWbemContext __RPC_FAR *pCtx,
  835. /* [in] */ IWbemProviderInitSink __RPC_FAR *pInitSink)
  836. {
  837. return InitializeProvider (
  838. pszNamespace,
  839. pszLocale,
  840. pNamespace,
  841. pCtx,
  842. pInitSink,
  843. &m_HandleMap,
  844. &m_pIWbemServices,
  845. &m_pIWbemRepository,
  846. NULL,
  847. FALSE
  848. ) ;
  849. }
  850. ////////////////////////////////////////////////////////////////////
  851. //******************************************************************
  852. //
  853. // PRIVATE FUNCTIONS
  854. //
  855. //******************************************************************
  856. ////////////////////////////////////////////////////////////////////