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.

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