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.

1146 lines
24 KiB

  1. /*++
  2. Copyright (C) 1996-2001 Microsoft Corporation
  3. Module Name:
  4. ProvFact.cpp
  5. Abstract:
  6. History:
  7. --*/
  8. #include <PreComp.h>
  9. #include <wbemint.h>
  10. #include <CGlobals.h>
  11. #include <DateTime.h>
  12. #include <ProvRegInfo.h>
  13. #include <ProvRegDecoupled.h>
  14. #include "Globals.h"
  15. #include "Guids.h"
  16. #include "ProvInterceptor.h"
  17. #include "ProvRegistrar.h"
  18. /******************************************************************************
  19. *
  20. * Name:
  21. *
  22. *
  23. * Description:
  24. *
  25. *
  26. *****************************************************************************/
  27. CServerObject_ProviderRegistrar_Base :: CServerObject_ProviderRegistrar_Base (
  28. WmiAllocator &a_Allocator
  29. ) :
  30. m_Allocator ( a_Allocator ) ,
  31. m_Clsid ( NULL ) ,
  32. m_Provider ( NULL ) ,
  33. m_User ( NULL ) ,
  34. m_Locale ( NULL ) ,
  35. m_Scope ( NULL ) ,
  36. m_Registration ( NULL ) ,
  37. m_Registered ( FALSE ) ,
  38. m_MarshaledProxy ( NULL ) ,
  39. m_MarshaledProxyLength ( 0 ) ,
  40. m_registrationEvent(NULL)
  41. {
  42. ZeroMemory ( & m_Identity , sizeof ( m_Identity ) ) ;
  43. }
  44. HRESULT CServerObject_ProviderRegistrar_Base :: AcquireLock(void)
  45. {
  46. DWORD index = 0;
  47. HRESULT result = CoWaitForMultipleHandles(COWAIT_WAITALL , DEFAULT_PROVIDER_TIMEOUT, 1, &m_registrationEvent, &index);
  48. return result;
  49. }
  50. /******************************************************************************
  51. *
  52. * Name:
  53. *
  54. *
  55. * Description:
  56. *
  57. *
  58. *****************************************************************************/
  59. CServerObject_ProviderRegistrar_Base::~CServerObject_ProviderRegistrar_Base ()
  60. {
  61. if ( m_Provider )
  62. {
  63. m_Provider->Release () ;
  64. }
  65. if ( m_Clsid )
  66. {
  67. SysFreeString ( m_Clsid ) ;
  68. }
  69. if ( m_User )
  70. {
  71. SysFreeString ( m_User ) ;
  72. }
  73. if ( m_Locale )
  74. {
  75. SysFreeString ( m_Locale ) ;
  76. }
  77. if ( m_Scope )
  78. {
  79. SysFreeString ( m_Scope ) ;
  80. }
  81. if ( m_Registration )
  82. {
  83. SysFreeString ( m_Registration ) ;
  84. }
  85. if ( m_MarshaledProxy )
  86. {
  87. ProviderSubSystem_Common_Globals :: ReleaseRegistration (
  88. m_MarshaledProxy ,
  89. m_MarshaledProxyLength
  90. ) ;
  91. delete [] m_MarshaledProxy ;
  92. m_MarshaledProxy = NULL ;
  93. m_MarshaledProxyLength = 0 ;
  94. }
  95. CloseHandle(m_registrationEvent);
  96. }
  97. /******************************************************************************
  98. *
  99. * Name:
  100. *
  101. *
  102. * Description:
  103. *
  104. *
  105. *****************************************************************************/
  106. HRESULT CServerObject_ProviderRegistrar_Base :: Initialize ()
  107. {
  108. m_registrationEvent = CreateMutex(0, FALSE, 0);
  109. if ( NULL != m_registrationEvent)
  110. {
  111. return S_OK ;
  112. }
  113. else
  114. {
  115. return WBEM_E_OUT_OF_MEMORY ;
  116. }
  117. }
  118. /******************************************************************************
  119. *
  120. * Name:
  121. *
  122. *
  123. * Description:
  124. *
  125. *
  126. *****************************************************************************/
  127. HRESULT CServerObject_ProviderRegistrar_Base :: SaveToRegistry (
  128. long a_Flags ,
  129. IWbemContext *a_Context ,
  130. LPCWSTR a_User ,
  131. LPCWSTR a_Locale ,
  132. LPCWSTR a_Registration ,
  133. LPCWSTR a_Scope ,
  134. IUnknown *a_Unknown ,
  135. BYTE *a_MarshaledProxy ,
  136. DWORD a_MarshaledProxyLength
  137. )
  138. {
  139. HRESULT t_Result = S_OK ;
  140. CServerObject_DecoupledClientRegistration_Element t_Element ;
  141. BSTR t_CreationTime = NULL ;
  142. FILETIME t_CreationFileTime ;
  143. FILETIME t_ExitFileTime ;
  144. FILETIME t_KernelFileTime ;
  145. FILETIME t_UserFileTime ;
  146. BOOL t_Status = GetProcessTimes (
  147. GetCurrentProcess (),
  148. & t_CreationFileTime,
  149. & t_ExitFileTime,
  150. & t_KernelFileTime,
  151. & t_UserFileTime
  152. );
  153. if ( t_Status )
  154. {
  155. CWbemDateTime t_Time ;
  156. t_Time.SetFileTimeDate ( t_CreationFileTime , VARIANT_FALSE ) ;
  157. t_Result = t_Time.GetValue ( & t_CreationTime ) ;
  158. if ( SUCCEEDED ( t_Result ) )
  159. {
  160. t_Result = t_Element.SetProcessIdentifier ( GetCurrentProcessId () ) ;
  161. if ( SUCCEEDED ( t_Result ) )
  162. {
  163. if ( a_Locale )
  164. {
  165. t_Result = t_Element.SetLocale ( ( BSTR ) a_Locale ) ;
  166. }
  167. }
  168. if ( SUCCEEDED ( t_Result ) )
  169. {
  170. if ( a_User )
  171. {
  172. t_Result = t_Element.SetUser ( ( BSTR ) a_User ) ;
  173. }
  174. }
  175. if ( SUCCEEDED ( t_Result ) )
  176. {
  177. t_Result = t_Element.SetProvider ( ( BSTR ) a_Registration ) ;
  178. }
  179. if ( SUCCEEDED ( t_Result ) )
  180. {
  181. t_Result = t_Element.SetScope ( ( BSTR ) a_Scope ) ;
  182. }
  183. if ( SUCCEEDED ( t_Result ) )
  184. {
  185. t_Result = t_Element.SetCreationTime ( ( BSTR ) t_CreationTime ) ;
  186. }
  187. if ( SUCCEEDED ( t_Result ) )
  188. {
  189. t_Result = t_Element.SetMarshaledProxy ( a_MarshaledProxy , a_MarshaledProxyLength ) ;
  190. }
  191. if ( SUCCEEDED ( t_Result ) )
  192. {
  193. t_Result = t_Element.Save ( m_Clsid ) ;
  194. }
  195. SysFreeString ( t_CreationTime ) ;
  196. }
  197. else
  198. {
  199. t_Result = WBEM_E_UNEXPECTED ;
  200. }
  201. }
  202. else
  203. {
  204. t_Result = WBEM_E_UNEXPECTED ;
  205. }
  206. return t_Result ;
  207. }
  208. /******************************************************************************
  209. *
  210. * Name:
  211. *
  212. *
  213. * Description:
  214. *
  215. *
  216. *****************************************************************************/
  217. HRESULT CServerObject_ProviderRegistrar_Base :: DirectRegister (
  218. GUID &a_Identity ,
  219. long a_Flags ,
  220. IWbemContext *a_Context ,
  221. LPCWSTR a_User ,
  222. LPCWSTR a_Locale ,
  223. LPCWSTR a_Registration ,
  224. LPCWSTR a_Scope ,
  225. IUnknown *a_Unknown ,
  226. BYTE *a_MarshaledProxy ,
  227. DWORD a_MarshaledProxyLength
  228. )
  229. {
  230. HRESULT t_Result = S_OK ;
  231. CServerObject_DecoupledServerRegistration t_Element ( m_Allocator ) ;
  232. BSTR t_CreationTime = NULL ;
  233. FILETIME t_CreationFileTime ;
  234. FILETIME t_ExitFileTime ;
  235. FILETIME t_KernelFileTime ;
  236. FILETIME t_UserFileTime ;
  237. BOOL t_Status = GetProcessTimes (
  238. GetCurrentProcess (),
  239. & t_CreationFileTime,
  240. & t_ExitFileTime,
  241. & t_KernelFileTime,
  242. & t_UserFileTime
  243. );
  244. if ( t_Status )
  245. {
  246. CWbemDateTime t_Time ;
  247. t_Time.SetFileTimeDate ( t_CreationFileTime , VARIANT_FALSE ) ;
  248. t_Result = t_Time.GetValue ( & t_CreationTime ) ;
  249. if ( SUCCEEDED ( t_Result ) )
  250. {
  251. t_Result = t_Element.Load () ;
  252. if ( SUCCEEDED ( t_Result ) )
  253. {
  254. BSTR t_ServerCreationTime = t_Element.GetCreationTime () ;
  255. DWORD t_ProcessIdentifier = t_Element.GetProcessIdentifier () ;
  256. BYTE *t_MarshaledProxy = t_Element.GetMarshaledProxy () ;
  257. DWORD t_MarshaledProxyLength = t_Element.GetMarshaledProxyLength () ;
  258. IUnknown *t_Unknown = NULL ;
  259. HRESULT t_TempResult = ProviderSubSystem_Common_Globals :: UnMarshalRegistration ( & t_Unknown , t_MarshaledProxy , t_MarshaledProxyLength ) ;
  260. if ( SUCCEEDED ( t_TempResult ) )
  261. {
  262. _IWmiProviderSubsystemRegistrar *t_Registrar = NULL ;
  263. t_Result = t_Unknown->QueryInterface ( IID__IWmiProviderSubsystemRegistrar , ( void ** ) & t_Registrar ) ;
  264. if ( SUCCEEDED ( t_Result ) )
  265. {
  266. BOOL t_Impersonating = FALSE ;
  267. IUnknown *t_OldContext = NULL ;
  268. IServerSecurity *t_OldSecurity = NULL ;
  269. t_Result = ProviderSubSystem_Common_Globals :: BeginCallbackImpersonation ( t_OldContext , t_OldSecurity , t_Impersonating ) ;
  270. if ( SUCCEEDED ( t_Result ) )
  271. {
  272. BOOL t_Revert = FALSE ;
  273. IUnknown *t_Proxy = NULL ;
  274. t_Result = ProviderSubSystem_Common_Globals :: SetProxyState ( IID__IWmiProviderSubsystemRegistrar , t_Registrar , t_Proxy , t_Revert ) ;
  275. if ( t_Result == WBEM_E_NOT_FOUND )
  276. {
  277. try
  278. {
  279. t_Result = t_Registrar->Register (
  280. 0 ,
  281. a_Context ,
  282. a_User ,
  283. a_Locale ,
  284. a_Scope ,
  285. a_Registration ,
  286. GetCurrentProcessId () ,
  287. a_Unknown ,
  288. a_Identity
  289. ) ;
  290. }
  291. catch ( ... )
  292. {
  293. t_Result = WBEM_E_PROVIDER_FAILURE ;
  294. }
  295. }
  296. else
  297. {
  298. if ( SUCCEEDED ( t_Result ) )
  299. {
  300. _IWmiProviderSubsystemRegistrar *t_RegistrarProxy = ( _IWmiProviderSubsystemRegistrar * ) t_Proxy ;
  301. // Set cloaking on the proxy
  302. // =========================
  303. DWORD t_ImpersonationLevel = ProviderSubSystem_Common_Globals :: GetCurrentImpersonationLevel () ;
  304. t_Result = ProviderSubSystem_Common_Globals :: SetCloaking (
  305. t_RegistrarProxy ,
  306. RPC_C_AUTHN_LEVEL_DEFAULT ,
  307. t_ImpersonationLevel
  308. ) ;
  309. if ( SUCCEEDED ( t_Result ) )
  310. {
  311. t_Result = CoImpersonateClient () ;
  312. if ( SUCCEEDED ( t_Result ) )
  313. {
  314. try
  315. {
  316. t_Result = t_RegistrarProxy->Register (
  317. 0 ,
  318. a_Context ,
  319. a_User ,
  320. a_Locale ,
  321. a_Scope ,
  322. a_Registration ,
  323. GetCurrentProcessId () ,
  324. a_Unknown ,
  325. a_Identity
  326. ) ;
  327. }
  328. catch ( ... )
  329. {
  330. t_Result = WBEM_E_PROVIDER_FAILURE ;
  331. }
  332. CoRevertToSelf () ;
  333. }
  334. else
  335. {
  336. t_Result = WBEM_E_ACCESS_DENIED ;
  337. }
  338. }
  339. ProviderSubSystem_Common_Globals :: RevertProxyState ( t_Proxy , t_Revert ) ;
  340. }
  341. }
  342. ProviderSubSystem_Common_Globals :: EndImpersonation ( t_OldContext , t_OldSecurity , t_Impersonating ) ;
  343. }
  344. t_Registrar->Release () ;
  345. }
  346. t_Unknown->Release () ;
  347. }
  348. }
  349. else
  350. {
  351. t_Result = S_OK ;
  352. }
  353. SysFreeString ( t_CreationTime ) ;
  354. }
  355. else
  356. {
  357. t_Result = WBEM_E_UNEXPECTED ;
  358. }
  359. }
  360. else
  361. {
  362. t_Result = WBEM_E_UNEXPECTED ;
  363. }
  364. return t_Result ;
  365. }
  366. /******************************************************************************
  367. *
  368. * Name:
  369. *
  370. *
  371. * Description:
  372. *
  373. *
  374. *****************************************************************************/
  375. HRESULT CServerObject_ProviderRegistrar_Base :: DirectUnRegister (
  376. long a_Flags ,
  377. IWbemContext *a_Context ,
  378. LPCWSTR a_User ,
  379. LPCWSTR a_Locale ,
  380. LPCWSTR a_Registration ,
  381. LPCWSTR a_Scope ,
  382. GUID &a_Identity
  383. )
  384. {
  385. HRESULT t_Result = S_OK ;
  386. CServerObject_DecoupledServerRegistration t_Element ( m_Allocator ) ;
  387. FILETIME t_CreationFileTime ;
  388. FILETIME t_ExitFileTime ;
  389. FILETIME t_KernelFileTime ;
  390. FILETIME t_UserFileTime ;
  391. BOOL t_Status = GetProcessTimes (
  392. GetCurrentProcess (),
  393. & t_CreationFileTime,
  394. & t_ExitFileTime,
  395. & t_KernelFileTime,
  396. & t_UserFileTime
  397. );
  398. if ( t_Status )
  399. {
  400. t_Result = t_Element.Load () ;
  401. if ( SUCCEEDED ( t_Result ) )
  402. {
  403. BSTR const t_CreationTime = t_Element.GetCreationTime () ;
  404. DWORD t_ProcessIdentifier = t_Element.GetProcessIdentifier () ;
  405. BYTE *t_MarshaledProxy = t_Element.GetMarshaledProxy () ;
  406. DWORD t_MarshaledProxyLength = t_Element.GetMarshaledProxyLength () ;
  407. IUnknown *t_Unknown = NULL ;
  408. t_Result = ProviderSubSystem_Common_Globals :: UnMarshalRegistration ( & t_Unknown , t_MarshaledProxy , t_MarshaledProxyLength ) ;
  409. if ( SUCCEEDED ( t_Result ) )
  410. {
  411. _IWmiProviderSubsystemRegistrar *t_Registrar = NULL ;
  412. t_Result = t_Unknown->QueryInterface ( IID__IWmiProviderSubsystemRegistrar , ( void ** ) & t_Registrar ) ;
  413. if ( SUCCEEDED ( t_Result ) )
  414. {
  415. BOOL t_Impersonating = FALSE ;
  416. IUnknown *t_OldContext = NULL ;
  417. IServerSecurity *t_OldSecurity = NULL ;
  418. t_Result = ProviderSubSystem_Common_Globals :: BeginCallbackImpersonation ( t_OldContext , t_OldSecurity , t_Impersonating ) ;
  419. if ( SUCCEEDED ( t_Result ) )
  420. {
  421. BOOL t_Revert = FALSE ;
  422. IUnknown *t_Proxy = NULL ;
  423. t_Result = ProviderSubSystem_Common_Globals :: SetProxyState ( IID__IWmiProviderSubsystemRegistrar , t_Registrar , t_Proxy , t_Revert ) ;
  424. if ( t_Result == WBEM_E_NOT_FOUND )
  425. {
  426. try
  427. {
  428. t_Result = t_Registrar->UnRegister (
  429. 0 ,
  430. a_Context ,
  431. a_User ,
  432. a_Locale ,
  433. a_Scope ,
  434. a_Registration ,
  435. a_Identity
  436. ) ;
  437. }
  438. catch ( ... )
  439. {
  440. t_Result = WBEM_E_PROVIDER_FAILURE ;
  441. }
  442. }
  443. else
  444. {
  445. if ( SUCCEEDED ( t_Result ) )
  446. {
  447. _IWmiProviderSubsystemRegistrar *t_RegistrarProxy = ( _IWmiProviderSubsystemRegistrar * ) t_Proxy ;
  448. // Set cloaking on the proxy
  449. // =========================
  450. DWORD t_ImpersonationLevel = ProviderSubSystem_Common_Globals :: GetCurrentImpersonationLevel () ;
  451. t_Result = ProviderSubSystem_Common_Globals :: SetCloaking (
  452. t_RegistrarProxy ,
  453. RPC_C_AUTHN_LEVEL_DEFAULT ,
  454. t_ImpersonationLevel
  455. ) ;
  456. if ( SUCCEEDED ( t_Result ) )
  457. {
  458. t_Result = CoImpersonateClient () ;
  459. if ( SUCCEEDED ( t_Result ) )
  460. {
  461. try
  462. {
  463. t_Result = t_RegistrarProxy->UnRegister (
  464. 0 ,
  465. a_Context ,
  466. a_User ,
  467. a_Locale ,
  468. a_Scope ,
  469. a_Registration ,
  470. a_Identity
  471. ) ;
  472. }
  473. catch ( ... )
  474. {
  475. t_Result = WBEM_E_PROVIDER_FAILURE ;
  476. }
  477. CoRevertToSelf () ;
  478. }
  479. else
  480. {
  481. t_Result = WBEM_E_ACCESS_DENIED ;
  482. }
  483. }
  484. HRESULT t_TempResult = ProviderSubSystem_Common_Globals :: RevertProxyState ( t_Proxy , t_Revert ) ;
  485. }
  486. }
  487. ProviderSubSystem_Common_Globals :: EndImpersonation ( t_OldContext , t_OldSecurity , t_Impersonating ) ;
  488. }
  489. t_Registrar->Release () ;
  490. }
  491. t_Unknown->Release () ;
  492. }
  493. }
  494. if ( SUCCEEDED ( t_Result ) )
  495. {
  496. ProviderSubSystem_Common_Globals :: ReleaseRegistration (
  497. m_MarshaledProxy ,
  498. m_MarshaledProxyLength
  499. ) ;
  500. delete [] m_MarshaledProxy ;
  501. m_MarshaledProxy = NULL ;
  502. m_MarshaledProxyLength = 0 ;
  503. }
  504. }
  505. else
  506. {
  507. t_Result = WBEM_E_UNEXPECTED ;
  508. }
  509. return t_Result ;
  510. }
  511. /******************************************************************************
  512. *
  513. * Name:
  514. *
  515. *
  516. * Description:
  517. *
  518. *
  519. *****************************************************************************/
  520. HRESULT CServerObject_ProviderRegistrar_Base :: CreateInterceptor (
  521. IWbemContext *a_Context ,
  522. IUnknown *a_Unknown ,
  523. BYTE *&a_MarshaledProxy ,
  524. DWORD &a_MarshaledProxyLength ,
  525. IUnknown *&a_MarshaledUnknown
  526. )
  527. {
  528. IWbemLocator *t_Locator = NULL ;
  529. IWbemServices *t_Service = NULL ;
  530. CServerObject_ProviderRegistrationV1 *t_Registration = NULL ;
  531. HRESULT t_Result = CoCreateInstance (
  532. CLSID_WbemLocator ,
  533. NULL ,
  534. CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER ,
  535. IID_IUnknown ,
  536. ( void ** ) & t_Locator
  537. );
  538. if ( SUCCEEDED ( t_Result ) )
  539. {
  540. t_Result = t_Locator->ConnectServer (
  541. m_Scope ,
  542. NULL ,
  543. NULL,
  544. NULL ,
  545. 0 ,
  546. NULL,
  547. NULL,
  548. & t_Service
  549. ) ;
  550. t_Locator->Release () ;
  551. }
  552. if ( SUCCEEDED ( t_Result ) )
  553. {
  554. t_Registration = new CServerObject_ProviderRegistrationV1 ;
  555. if ( t_Registration )
  556. {
  557. t_Registration->AddRef () ;
  558. IWbemPath *t_NamespacePath = NULL ;
  559. t_Result = CoCreateInstance (
  560. CLSID_WbemDefPath ,
  561. NULL ,
  562. CLSCTX_INPROC_SERVER ,
  563. IID_IWbemPath ,
  564. ( void ** ) & t_NamespacePath
  565. ) ;
  566. if ( SUCCEEDED ( t_Result ) )
  567. {
  568. t_Result = t_NamespacePath->SetText ( WBEMPATH_TREAT_SINGLE_IDENT_AS_NS | WBEMPATH_CREATE_ACCEPT_ALL , m_Scope ) ;
  569. }
  570. if ( SUCCEEDED( t_Result ) )
  571. {
  572. t_Result = t_Registration->SetContext (
  573. a_Context ,
  574. t_NamespacePath ,
  575. t_Service
  576. ) ;
  577. if ( SUCCEEDED ( t_Result ) )
  578. {
  579. t_Result = t_Registration->Load (
  580. e_All ,
  581. NULL ,
  582. m_Registration
  583. ) ;
  584. if ( t_Result == WBEM_E_NOT_FOUND )
  585. {
  586. t_Result = WBEM_E_PROVIDER_NOT_FOUND ;
  587. }
  588. }
  589. }
  590. if ( t_NamespacePath )
  591. {
  592. t_NamespacePath->Release () ;
  593. }
  594. }
  595. else
  596. {
  597. t_Result = WBEM_E_OUT_OF_MEMORY ;
  598. }
  599. }
  600. if ( SUCCEEDED ( t_Result ) )
  601. {
  602. CInterceptor_DecoupledClient *t_Provider = new CInterceptor_DecoupledClient (
  603. m_Allocator ,
  604. a_Unknown ,
  605. t_Service ,
  606. *t_Registration
  607. ) ;
  608. if ( t_Provider )
  609. {
  610. t_Provider->AddRef () ;
  611. t_Result = t_Provider->ProviderInitialize () ;
  612. if ( SUCCEEDED ( t_Result ) )
  613. {
  614. t_Result = t_Provider->QueryInterface ( IID_IUnknown , ( void ** ) & a_MarshaledUnknown ) ;
  615. if ( SUCCEEDED ( t_Result ) )
  616. {
  617. t_Result = ProviderSubSystem_Common_Globals :: MarshalRegistration (
  618. a_MarshaledUnknown ,
  619. a_MarshaledProxy ,
  620. a_MarshaledProxyLength
  621. ) ;
  622. if ( FAILED ( t_Result ) )
  623. {
  624. a_MarshaledUnknown->Release () ;
  625. a_MarshaledUnknown = NULL ;
  626. }
  627. }
  628. }
  629. if ( SUCCEEDED ( t_Result ) )
  630. {
  631. m_Provider = t_Provider ;
  632. }
  633. else
  634. {
  635. t_Provider->Release () ;
  636. }
  637. }
  638. }
  639. if ( t_Registration )
  640. {
  641. t_Registration->Release () ;
  642. }
  643. if ( t_Service )
  644. {
  645. t_Service->Release () ;
  646. }
  647. return t_Result ;
  648. }
  649. /******************************************************************************
  650. *
  651. * Name:
  652. *
  653. *
  654. * Description:
  655. *
  656. *
  657. *****************************************************************************/
  658. HRESULT CServerObject_ProviderRegistrar_Base :: Register (
  659. long a_Flags ,
  660. IWbemContext *a_Context ,
  661. LPCWSTR a_User ,
  662. LPCWSTR a_Locale ,
  663. LPCWSTR a_Scope ,
  664. LPCWSTR a_Registration ,
  665. IUnknown *a_Unknown
  666. )
  667. {
  668. HRESULT t_Result = S_OK ;
  669. if ( a_Scope == NULL || a_Registration == NULL || a_Unknown == NULL )
  670. {
  671. return WBEM_E_INVALID_PARAMETER ;
  672. }
  673. if (FAILED(t_Result = AcquireLock()))
  674. {
  675. return t_Result;
  676. };
  677. try
  678. {
  679. if ( m_Registered == FALSE )
  680. {
  681. if ( SUCCEEDED ( t_Result ) )
  682. {
  683. t_Result = CoCreateGuid ( & m_Identity ) ;
  684. }
  685. if ( SUCCEEDED ( t_Result ) )
  686. {
  687. BSTR t_Clsid = NULL ;
  688. t_Result = StringFromCLSID (
  689. m_Identity ,
  690. & t_Clsid
  691. ) ;
  692. if ( SUCCEEDED ( t_Result ) )
  693. {
  694. if ( m_Clsid )
  695. {
  696. SysFreeString ( m_Clsid ) ;
  697. m_Clsid = NULL ;
  698. }
  699. if ( m_User )
  700. {
  701. SysFreeString ( m_User ) ;
  702. m_User = NULL ;
  703. }
  704. if ( m_Locale )
  705. {
  706. SysFreeString ( m_Locale ) ;
  707. m_Locale = NULL ;
  708. }
  709. if ( m_Scope )
  710. {
  711. SysFreeString ( m_Scope ) ;
  712. m_Scope = NULL ;
  713. }
  714. if ( m_Registration )
  715. {
  716. SysFreeString ( m_Registration ) ;
  717. m_Registration = NULL ;
  718. }
  719. m_Clsid = SysAllocString ( t_Clsid ) ;
  720. if ( m_Clsid == NULL )
  721. {
  722. t_Result = WBEM_E_OUT_OF_MEMORY ;
  723. }
  724. if ( a_User )
  725. {
  726. m_User = SysAllocString ( a_User ) ;
  727. if ( m_User == NULL )
  728. {
  729. t_Result = WBEM_E_OUT_OF_MEMORY ;
  730. }
  731. }
  732. if ( m_Locale )
  733. {
  734. m_Locale = SysAllocString ( a_Locale ) ;
  735. if ( m_Locale == NULL )
  736. {
  737. t_Result = WBEM_E_OUT_OF_MEMORY ;
  738. }
  739. }
  740. m_Scope = SysAllocString ( a_Scope ) ;
  741. if ( m_Scope == NULL )
  742. {
  743. t_Result = WBEM_E_OUT_OF_MEMORY ;
  744. }
  745. m_Registration = SysAllocString ( a_Registration ) ;
  746. if ( m_Registration == NULL )
  747. {
  748. t_Result = WBEM_E_OUT_OF_MEMORY ;
  749. }
  750. if ( SUCCEEDED ( t_Result ) )
  751. {
  752. IUnknown *t_MarshaledUnknown = NULL ;
  753. t_Result = CreateInterceptor (
  754. a_Context ,
  755. a_Unknown ,
  756. m_MarshaledProxy ,
  757. m_MarshaledProxyLength ,
  758. t_MarshaledUnknown
  759. ) ;
  760. if ( SUCCEEDED ( t_Result ) )
  761. {
  762. t_Result = DirectRegister (
  763. m_Identity ,
  764. a_Flags ,
  765. a_Context ,
  766. a_User ,
  767. a_Locale ,
  768. a_Registration ,
  769. a_Scope ,
  770. t_MarshaledUnknown ,
  771. m_MarshaledProxy ,
  772. m_MarshaledProxyLength
  773. ) ;
  774. }
  775. if ( t_MarshaledUnknown )
  776. {
  777. t_MarshaledUnknown->Release () ;
  778. }
  779. }
  780. CoTaskMemFree ( t_Clsid ) ;
  781. }
  782. else
  783. {
  784. t_Result = WBEM_E_OUT_OF_MEMORY ;
  785. }
  786. }
  787. else
  788. {
  789. t_Result = WBEM_E_UNEXPECTED ;
  790. }
  791. }
  792. else
  793. {
  794. t_Result = WBEM_E_PROVIDER_ALREADY_REGISTERED ;
  795. }
  796. }
  797. catch ( ... )
  798. {
  799. t_Result = WBEM_E_PROVIDER_FAILURE ;
  800. }
  801. if ( SUCCEEDED ( t_Result ) )
  802. {
  803. m_Registered = TRUE ;
  804. }
  805. ReleaseLock();
  806. return t_Result ;
  807. }
  808. /******************************************************************************
  809. *
  810. * Name:
  811. *
  812. *
  813. * Description:
  814. *
  815. *
  816. *****************************************************************************/
  817. HRESULT CServerObject_ProviderRegistrar_Base :: UnRegister ()
  818. {
  819. HRESULT t_Result = S_OK ;
  820. if (FAILED(t_Result = AcquireLock()))
  821. {
  822. return t_Result;
  823. };
  824. try
  825. {
  826. if ( m_Registered )
  827. {
  828. CServerObject_DecoupledClientRegistration_Element t_Element ;
  829. t_Result = t_Element.Load ( m_Clsid ) ;
  830. t_Result = DirectUnRegister (
  831. 0 ,
  832. NULL ,
  833. m_User ,
  834. m_Locale ,
  835. m_Registration ,
  836. m_Scope ,
  837. m_Identity
  838. ) ;
  839. if ( SUCCEEDED ( t_Result ) )
  840. {
  841. if ( m_Provider )
  842. {
  843. m_Provider->Release () ;
  844. m_Provider = NULL ;
  845. }
  846. m_Registered = FALSE ;
  847. }
  848. }
  849. else
  850. {
  851. t_Result = WBEM_E_PROVIDER_NOT_REGISTERED ;
  852. }
  853. }
  854. catch ( ... )
  855. {
  856. t_Result = WBEM_E_PROVIDER_FAILURE ;
  857. }
  858. ReleaseLock();
  859. return t_Result ;
  860. }
  861. /******************************************************************************
  862. *
  863. * Name:
  864. *
  865. *
  866. * Description:
  867. *
  868. *
  869. *****************************************************************************/
  870. CServerObject_ProviderRegistrar :: CServerObject_ProviderRegistrar (
  871. WmiAllocator &a_Allocator
  872. ) : CServerObject_ProviderRegistrar_Base ( a_Allocator ) ,
  873. m_ReferenceCount ( 0 )
  874. {
  875. InterlockedIncrement ( & DecoupledProviderSubSystem_Globals :: s_CServerObject_ProviderRegistrar_ObjectsInProgress ) ;
  876. InterlockedIncrement ( & DecoupledProviderSubSystem_Globals :: s_ObjectsInProgress ) ;
  877. }
  878. /******************************************************************************
  879. *
  880. * Name:
  881. *
  882. *
  883. * Description:
  884. *
  885. *
  886. *****************************************************************************/
  887. CServerObject_ProviderRegistrar::~CServerObject_ProviderRegistrar ()
  888. {
  889. InterlockedDecrement ( & DecoupledProviderSubSystem_Globals :: s_CServerObject_ProviderRegistrar_ObjectsInProgress ) ;
  890. InterlockedDecrement ( & DecoupledProviderSubSystem_Globals :: s_ObjectsInProgress ) ;
  891. }
  892. /******************************************************************************
  893. *
  894. * Name:
  895. *
  896. *
  897. * Description:
  898. *
  899. *
  900. *****************************************************************************/
  901. STDMETHODIMP CServerObject_ProviderRegistrar::QueryInterface (
  902. REFIID a_Riid ,
  903. LPVOID FAR *a_Void
  904. )
  905. {
  906. *a_Void = NULL ;
  907. if ( a_Riid == IID_IUnknown )
  908. {
  909. *a_Void = ( LPVOID ) this ;
  910. }
  911. else if ( a_Riid == IID_IWbemDecoupledRegistrar )
  912. {
  913. *a_Void = ( LPVOID ) ( IWbemDecoupledRegistrar * ) this ;
  914. }
  915. if ( *a_Void )
  916. {
  917. ( ( LPUNKNOWN ) *a_Void )->AddRef () ;
  918. return ResultFromScode ( S_OK ) ;
  919. }
  920. else
  921. {
  922. return ResultFromScode ( E_NOINTERFACE ) ;
  923. }
  924. }
  925. /******************************************************************************
  926. *
  927. * Name:
  928. *
  929. *
  930. * Description:
  931. *
  932. *
  933. *****************************************************************************/
  934. STDMETHODIMP_( ULONG ) CServerObject_ProviderRegistrar :: AddRef ()
  935. {
  936. return InterlockedIncrement ( & m_ReferenceCount ) ;
  937. }
  938. /******************************************************************************
  939. *
  940. * Name:
  941. *
  942. *
  943. * Description:
  944. *
  945. *
  946. *****************************************************************************/
  947. STDMETHODIMP_(ULONG) CServerObject_ProviderRegistrar :: Release ()
  948. {
  949. ULONG t_ReferenceCount = InterlockedDecrement ( & m_ReferenceCount ) ;
  950. if ( t_ReferenceCount == 0 )
  951. {
  952. delete this ;
  953. }
  954. return t_ReferenceCount ;
  955. }