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.

1103 lines
22 KiB

  1. //***************************************************************************
  2. //
  3. // File:
  4. //
  5. // Module: MS SNMP Provider
  6. //
  7. // Purpose:
  8. //
  9. // Copyright (c) 1997-2001 Microsoft Corporation, All Rights Reserved
  10. //
  11. //***************************************************************************
  12. #include "precomp.h"
  13. #include <provexpt.h>
  14. #include <snmpstd.h>
  15. #include <snmpmt.h>
  16. #include <snmptempl.h>
  17. #include <objbase.h>
  18. // this redefines the DEFINE_GUID() macro to do allocation.
  19. //
  20. #include <initguid.h>
  21. #ifndef INITGUID
  22. #define INITGUID
  23. #endif
  24. #include <wbemidl.h>
  25. #include <snmpcont.h>
  26. #include <instpath.h>
  27. #include <snmpevt.h>
  28. #include <snmpthrd.h>
  29. #include <snmplog.h>
  30. #include <snmpcl.h>
  31. #include <snmptype.h>
  32. #include <snmpobj.h>
  33. #include <smir.h>
  34. #include <classfac.h>
  35. #include "clasprov.h"
  36. #include "propprov.h"
  37. #include "guids.h"
  38. #include <notify.h>
  39. #include <evtdefs.h>
  40. #include <evtthrd.h>
  41. #include <evtmap.h>
  42. #include <evtprov.h>
  43. LONG CClasProvClassFactory :: objectsInProgress = 0 ;
  44. LONG CClasProvClassFactory :: locksInProgress = 0 ;
  45. LONG CPropProvClassFactory :: objectsInProgress = 0 ;
  46. LONG CPropProvClassFactory :: locksInProgress = 0 ;
  47. LONG CSNMPEventProviderClassFactory :: objectsInProgress = 0 ;
  48. LONG CSNMPEventProviderClassFactory :: locksInProgress = 0 ;
  49. extern CEventProviderThread* g_pProvThrd;
  50. extern CEventProviderWorkerThread* g_pWorkerThread;
  51. extern CRITICAL_SECTION s_ProviderCriticalSection ;
  52. extern void ProviderStartup () ;
  53. extern void ProviderClosedown () ;
  54. //***************************************************************************
  55. //
  56. // CClasProvClassFactory::CClasProvClassFactory
  57. // CClasProvClassFactory::~CClasProvClassFactory
  58. //
  59. // Constructor Parameters:
  60. // None
  61. //***************************************************************************
  62. CClasProvClassFactory :: CClasProvClassFactory ()
  63. {
  64. InterlockedIncrement ( & objectsInProgress ) ;
  65. m_referenceCount = 0 ;
  66. }
  67. CClasProvClassFactory::~CClasProvClassFactory ()
  68. {
  69. InterlockedDecrement ( & objectsInProgress ) ;
  70. }
  71. //***************************************************************************
  72. //
  73. // CClasProvClassFactory::QueryInterface
  74. // CClasProvClassFactory::AddRef
  75. // CClasProvClassFactory::Release
  76. //
  77. // Purpose: Standard Ole routines needed for all interfaces
  78. //
  79. //***************************************************************************
  80. STDMETHODIMP CClasProvClassFactory::QueryInterface (
  81. REFIID iid ,
  82. LPVOID FAR *iplpv
  83. )
  84. {
  85. SetStructuredExceptionHandler seh;
  86. try
  87. {
  88. *iplpv = NULL ;
  89. if ( iid == IID_IUnknown )
  90. {
  91. *iplpv = ( LPVOID ) this ;
  92. }
  93. else if ( iid == IID_IClassFactory )
  94. {
  95. *iplpv = ( LPVOID ) this ;
  96. }
  97. if ( *iplpv )
  98. {
  99. ( ( LPUNKNOWN ) *iplpv )->AddRef () ;
  100. return ResultFromScode ( S_OK ) ;
  101. }
  102. else
  103. {
  104. return ResultFromScode ( E_NOINTERFACE ) ;
  105. }
  106. }
  107. catch(Structured_Exception e_SE)
  108. {
  109. return E_UNEXPECTED;
  110. }
  111. catch(Heap_Exception e_HE)
  112. {
  113. return E_OUTOFMEMORY;
  114. }
  115. catch(...)
  116. {
  117. return E_UNEXPECTED;
  118. }
  119. }
  120. STDMETHODIMP_( ULONG ) CClasProvClassFactory :: AddRef ()
  121. {
  122. SetStructuredExceptionHandler seh;
  123. try
  124. {
  125. return InterlockedIncrement ( & m_referenceCount ) ;
  126. }
  127. catch(Structured_Exception e_SE)
  128. {
  129. return 0;
  130. }
  131. catch(Heap_Exception e_HE)
  132. {
  133. return 0;
  134. }
  135. catch(...)
  136. {
  137. return 0;
  138. }
  139. }
  140. STDMETHODIMP_(ULONG) CClasProvClassFactory :: Release ()
  141. {
  142. SetStructuredExceptionHandler seh;
  143. try
  144. {
  145. LONG ref ;
  146. if ( ( ref = InterlockedDecrement ( & m_referenceCount ) ) == 0 )
  147. {
  148. delete this ;
  149. return 0 ;
  150. }
  151. else
  152. {
  153. return ref ;
  154. }
  155. }
  156. catch(Structured_Exception e_SE)
  157. {
  158. return 0;
  159. }
  160. catch(Heap_Exception e_HE)
  161. {
  162. return 0;
  163. }
  164. catch(...)
  165. {
  166. return 0;
  167. }
  168. }
  169. //***************************************************************************
  170. //
  171. // CClasProvClassFactory::CreateInstance
  172. //
  173. // Purpose: Instantiates a Provider object returning an interface pointer.
  174. //
  175. // Parameters:
  176. // pUnkOuter LPUNKNOWN to the controlling IUnknown if we are
  177. // being used in an aggregation.
  178. // riid REFIID identifying the interface the caller
  179. // desires to have for the new object.
  180. // ppvObj PPVOID in which to store the desired
  181. // interface pointer for the new object.
  182. //
  183. // Return Value:
  184. // HRESULT NOERROR if successful, otherwise E_NOINTERFACE
  185. // if we cannot support the requested interface.
  186. //***************************************************************************
  187. STDMETHODIMP CClasProvClassFactory :: CreateInstance (
  188. LPUNKNOWN pUnkOuter ,
  189. REFIID riid ,
  190. LPVOID FAR * ppvObject
  191. )
  192. {
  193. SetStructuredExceptionHandler seh;
  194. try
  195. {
  196. HRESULT status = S_OK ;
  197. if ( pUnkOuter )
  198. {
  199. status = CLASS_E_NOAGGREGATION ;
  200. }
  201. else
  202. {
  203. IWbemServices *lpunk = ( IWbemServices * ) new CImpClasProv ;
  204. if ( lpunk == NULL )
  205. {
  206. status = E_OUTOFMEMORY ;
  207. }
  208. else
  209. {
  210. status = lpunk->QueryInterface ( riid , ppvObject ) ;
  211. if ( FAILED ( status ) )
  212. {
  213. delete lpunk ;
  214. }
  215. else
  216. {
  217. }
  218. }
  219. }
  220. return status ;
  221. }
  222. catch(Structured_Exception e_SE)
  223. {
  224. return E_UNEXPECTED;
  225. }
  226. catch(Heap_Exception e_HE)
  227. {
  228. return E_OUTOFMEMORY;
  229. }
  230. }
  231. //***************************************************************************
  232. //
  233. // CClasProvClassFactory::LockServer
  234. //
  235. // Purpose:
  236. // Increments or decrements the lock count of the DLL. If the
  237. // lock count goes to zero and there are no objects, the DLL
  238. // is allowed to unload. See DllCanUnloadNow.
  239. //
  240. // Parameters:
  241. // fLock BOOL specifying whether to increment or
  242. // decrement the lock count.
  243. //
  244. // Return Value:
  245. // HRESULT NOERROR always.
  246. //***************************************************************************
  247. STDMETHODIMP CClasProvClassFactory :: LockServer ( BOOL fLock )
  248. {
  249. SetStructuredExceptionHandler seh;
  250. try
  251. {
  252. /*
  253. * Place code in critical section
  254. */
  255. if ( fLock )
  256. {
  257. InterlockedIncrement ( & locksInProgress ) ;
  258. }
  259. else
  260. {
  261. InterlockedDecrement ( & locksInProgress ) ;
  262. }
  263. return S_OK ;
  264. }
  265. catch(Structured_Exception e_SE)
  266. {
  267. return E_UNEXPECTED;
  268. }
  269. catch(Heap_Exception e_HE)
  270. {
  271. return E_OUTOFMEMORY;
  272. }
  273. catch(...)
  274. {
  275. return E_UNEXPECTED;
  276. }
  277. }
  278. //***************************************************************************
  279. //
  280. // CPropProvClassFactory::CPropProvClassFactory
  281. // CPropProvClassFactory::~CPropProvClassFactory
  282. //
  283. // Constructor Parameters:
  284. // None
  285. //***************************************************************************
  286. CPropProvClassFactory :: CPropProvClassFactory ()
  287. {
  288. InterlockedIncrement ( & objectsInProgress ) ;
  289. m_referenceCount = 0 ;
  290. }
  291. CPropProvClassFactory::~CPropProvClassFactory ()
  292. {
  293. InterlockedDecrement ( & objectsInProgress ) ;
  294. }
  295. //***************************************************************************
  296. //
  297. // CPropProvClassFactory::QueryInterface
  298. // CPropProvClassFactory::AddRef
  299. // CPropProvClassFactory::Release
  300. //
  301. // Purpose: Standard Ole routines needed for all interfaces
  302. //
  303. //***************************************************************************
  304. STDMETHODIMP CPropProvClassFactory::QueryInterface (
  305. REFIID iid ,
  306. LPVOID FAR *iplpv
  307. )
  308. {
  309. SetStructuredExceptionHandler seh;
  310. try
  311. {
  312. *iplpv = NULL ;
  313. if ( iid == IID_IUnknown )
  314. {
  315. *iplpv = ( LPVOID ) this ;
  316. }
  317. else if ( iid == IID_IClassFactory )
  318. {
  319. *iplpv = ( LPVOID ) this ;
  320. }
  321. if ( *iplpv )
  322. {
  323. ( ( LPUNKNOWN ) *iplpv )->AddRef () ;
  324. return ResultFromScode ( S_OK ) ;
  325. }
  326. else
  327. {
  328. return ResultFromScode ( E_NOINTERFACE ) ;
  329. }
  330. }
  331. catch(Structured_Exception e_SE)
  332. {
  333. return E_UNEXPECTED;
  334. }
  335. catch(Heap_Exception e_HE)
  336. {
  337. return E_OUTOFMEMORY;
  338. }
  339. catch(...)
  340. {
  341. return E_UNEXPECTED;
  342. }
  343. }
  344. STDMETHODIMP_( ULONG ) CPropProvClassFactory :: AddRef ()
  345. {
  346. SetStructuredExceptionHandler seh;
  347. try
  348. {
  349. return InterlockedIncrement ( & m_referenceCount ) ;
  350. }
  351. catch(Structured_Exception e_SE)
  352. {
  353. return 0;
  354. }
  355. catch(Heap_Exception e_HE)
  356. {
  357. return 0;
  358. }
  359. catch(...)
  360. {
  361. return 0;
  362. }
  363. }
  364. STDMETHODIMP_(ULONG) CPropProvClassFactory :: Release ()
  365. {
  366. SetStructuredExceptionHandler seh;
  367. try
  368. {
  369. LONG ref ;
  370. if ( ( ref = InterlockedDecrement ( & m_referenceCount ) ) == 0 )
  371. {
  372. delete this ;
  373. return 0 ;
  374. }
  375. else
  376. {
  377. return ref ;
  378. }
  379. }
  380. catch(Structured_Exception e_SE)
  381. {
  382. return 0;
  383. }
  384. catch(Heap_Exception e_HE)
  385. {
  386. return 0;
  387. }
  388. catch(...)
  389. {
  390. return 0;
  391. }
  392. }
  393. //***************************************************************************
  394. //
  395. // CPropProvClassFactory::CreateInstance
  396. //
  397. // Purpose: Instantiates a Provider object returning an interface pointer.
  398. //
  399. // Parameters:
  400. // pUnkOuter LPUNKNOWN to the controlling IUnknown if we are
  401. // being used in an aggregation.
  402. // riid REFIID identifying the interface the caller
  403. // desires to have for the new object.
  404. // ppvObj PPVOID in which to store the desired
  405. // interface pointer for the new object.
  406. //
  407. // Return Value:
  408. // HRESULT NOERROR if successful, otherwise E_NOINTERFACE
  409. // if we cannot support the requested interface.
  410. //***************************************************************************
  411. STDMETHODIMP CPropProvClassFactory :: CreateInstance (
  412. LPUNKNOWN pUnkOuter ,
  413. REFIID riid ,
  414. LPVOID FAR * ppvObject
  415. )
  416. {
  417. SetStructuredExceptionHandler seh;
  418. try
  419. {
  420. HRESULT status = S_OK ;
  421. if ( pUnkOuter )
  422. {
  423. status = CLASS_E_NOAGGREGATION ;
  424. }
  425. else
  426. {
  427. IWbemServices *lpunk = ( IWbemServices * ) new CImpPropProv ;
  428. if ( lpunk == NULL )
  429. {
  430. status = E_OUTOFMEMORY ;
  431. }
  432. else
  433. {
  434. status = lpunk->QueryInterface ( riid , ppvObject ) ;
  435. if ( FAILED ( status ) )
  436. {
  437. delete lpunk ;
  438. }
  439. else
  440. {
  441. }
  442. }
  443. }
  444. return status ;
  445. }
  446. catch(Structured_Exception e_SE)
  447. {
  448. return E_UNEXPECTED;
  449. }
  450. catch(Heap_Exception e_HE)
  451. {
  452. return E_OUTOFMEMORY;
  453. }
  454. catch(...)
  455. {
  456. return E_UNEXPECTED;
  457. }
  458. }
  459. //***************************************************************************
  460. //
  461. // CPropProvClassFactory::LockServer
  462. //
  463. // Purpose:
  464. // Increments or decrements the lock count of the DLL. If the
  465. // lock count goes to zero and there are no objects, the DLL
  466. // is allowed to unload. See DllCanUnloadNow.
  467. //
  468. // Parameters:
  469. // fLock BOOL specifying whether to increment or
  470. // decrement the lock count.
  471. //
  472. // Return Value:
  473. // HRESULT NOERROR always.
  474. //***************************************************************************
  475. STDMETHODIMP CPropProvClassFactory :: LockServer ( BOOL fLock )
  476. {
  477. SetStructuredExceptionHandler seh;
  478. try
  479. {
  480. /*
  481. * Place code in critical section
  482. */
  483. if ( fLock )
  484. {
  485. InterlockedIncrement ( & locksInProgress ) ;
  486. }
  487. else
  488. {
  489. InterlockedDecrement ( & locksInProgress ) ;
  490. }
  491. return S_OK ;
  492. }
  493. catch(Structured_Exception e_SE)
  494. {
  495. return E_UNEXPECTED;
  496. }
  497. catch(Heap_Exception e_HE)
  498. {
  499. return E_OUTOFMEMORY;
  500. }
  501. catch(...)
  502. {
  503. return E_UNEXPECTED;
  504. }
  505. }
  506. //***************************************************************************
  507. //
  508. // CSNMPEventProviderClassFactory::CSNMPEventProviderClassFactory
  509. // CSNMPEventProviderClassFactory::~CSNMPEventProviderClassFactory
  510. //
  511. // Constructor Parameters:
  512. // None
  513. //***************************************************************************
  514. CSNMPEventProviderClassFactory :: CSNMPEventProviderClassFactory ()
  515. {
  516. m_referenceCount = 0 ;
  517. }
  518. CSNMPEventProviderClassFactory::~CSNMPEventProviderClassFactory ()
  519. {
  520. }
  521. //***************************************************************************
  522. //
  523. // CSNMPEventProviderClassFactory::QueryInterface
  524. // CSNMPEventProviderClassFactory::AddRef
  525. // CSNMPEventProviderClassFactory::Release
  526. //
  527. // Purpose: Standard Ole routines needed for all interfaces
  528. //
  529. //***************************************************************************
  530. STDMETHODIMP CSNMPEventProviderClassFactory::QueryInterface (
  531. REFIID iid ,
  532. LPVOID FAR *iplpv
  533. )
  534. {
  535. SetStructuredExceptionHandler seh;
  536. try
  537. {
  538. *iplpv = NULL ;
  539. if ( iid == IID_IUnknown )
  540. {
  541. *iplpv = ( LPVOID ) this ;
  542. }
  543. else if ( iid == IID_IClassFactory )
  544. {
  545. *iplpv = ( LPVOID ) this ;
  546. }
  547. if ( *iplpv )
  548. {
  549. ( ( LPUNKNOWN ) *iplpv )->AddRef () ;
  550. return ResultFromScode ( S_OK ) ;
  551. }
  552. else
  553. {
  554. return ResultFromScode ( E_NOINTERFACE ) ;
  555. }
  556. }
  557. catch(Structured_Exception e_SE)
  558. {
  559. return E_UNEXPECTED;
  560. }
  561. catch(Heap_Exception e_HE)
  562. {
  563. return E_OUTOFMEMORY;
  564. }
  565. catch(...)
  566. {
  567. return E_UNEXPECTED;
  568. }
  569. }
  570. STDMETHODIMP_( ULONG ) CSNMPEventProviderClassFactory :: AddRef ()
  571. {
  572. SetStructuredExceptionHandler seh;
  573. try
  574. {
  575. InterlockedIncrement(&objectsInProgress);
  576. return InterlockedIncrement ( &m_referenceCount ) ;
  577. }
  578. catch(Structured_Exception e_SE)
  579. {
  580. return 0;
  581. }
  582. catch(Heap_Exception e_HE)
  583. {
  584. return 0;
  585. }
  586. catch(...)
  587. {
  588. return 0;
  589. }
  590. }
  591. STDMETHODIMP_(ULONG) CSNMPEventProviderClassFactory :: Release ()
  592. {
  593. SetStructuredExceptionHandler seh;
  594. try
  595. {
  596. LONG ref ;
  597. if ( ( ref = InterlockedDecrement ( & m_referenceCount ) ) == 0 )
  598. {
  599. delete this ;
  600. InterlockedDecrement(&objectsInProgress);
  601. return 0 ;
  602. }
  603. else
  604. {
  605. InterlockedDecrement(&objectsInProgress);
  606. return ref ;
  607. }
  608. }
  609. catch(Structured_Exception e_SE)
  610. {
  611. return 0;
  612. }
  613. catch(Heap_Exception e_HE)
  614. {
  615. return 0;
  616. }
  617. catch(...)
  618. {
  619. return 0;
  620. }
  621. }
  622. //***************************************************************************
  623. //
  624. // CSNMPEventProviderClassFactory::LockServer
  625. //
  626. // Purpose:
  627. // Increments or decrements the lock count of the DLL. If the
  628. // lock count goes to zero and there are no objects, the DLL
  629. // is allowed to unload. See DllCanUnloadNow.
  630. //
  631. // Parameters:
  632. // fLock BOOL specifying whether to increment or
  633. // decrement the lock count.
  634. //
  635. // Return Value:
  636. // HRESULT NOERROR always.
  637. //***************************************************************************
  638. STDMETHODIMP CSNMPEventProviderClassFactory :: LockServer ( BOOL fLock )
  639. {
  640. SetStructuredExceptionHandler seh;
  641. try
  642. {
  643. if ( fLock )
  644. {
  645. InterlockedIncrement ( & locksInProgress ) ;
  646. }
  647. else
  648. {
  649. InterlockedDecrement ( & locksInProgress ) ;
  650. }
  651. return S_OK ;
  652. }
  653. catch(Structured_Exception e_SE)
  654. {
  655. return E_UNEXPECTED;
  656. }
  657. catch(Heap_Exception e_HE)
  658. {
  659. return E_OUTOFMEMORY;
  660. }
  661. catch(...)
  662. {
  663. return E_UNEXPECTED;
  664. }
  665. }
  666. //***************************************************************************
  667. //
  668. // CSNMPEncapEventProviderClassFactory::CreateInstance
  669. //
  670. // Purpose: Instantiates a Provider object returning an interface pointer.
  671. //
  672. // Parameters:
  673. // pUnkOuter LPUNKNOWN to the controlling IUnknown if we are
  674. // being used in an aggregation.
  675. // riid REFIID identifying the interface the caller
  676. // desires to have for the new object.
  677. // ppvObj PPVOID in which to store the desired
  678. // interface pointer for the new object.
  679. //
  680. // Return Value:
  681. // HRESULT NOERROR if successful, otherwise E_NOINTERFACE
  682. // if we cannot support the requested interface.
  683. //***************************************************************************
  684. STDMETHODIMP CSNMPEncapEventProviderClassFactory :: CreateInstance(LPUNKNOWN pUnkOuter ,
  685. REFIID riid,
  686. LPVOID FAR * ppvObject
  687. )
  688. {
  689. SetStructuredExceptionHandler seh;
  690. BOOL bEnteredCritSec = FALSE;
  691. BOOL bContinue = FALSE;
  692. try
  693. {
  694. HRESULT status = E_FAIL;
  695. if ( pUnkOuter )
  696. {
  697. status = CLASS_E_NOAGGREGATION;
  698. }
  699. else
  700. {
  701. EnterCriticalSection ( & s_ProviderCriticalSection ) ;
  702. bEnteredCritSec = TRUE;
  703. if (NULL == g_pProvThrd)
  704. {
  705. ProviderStartup () ;
  706. SnmpThreadObject :: Startup () ;
  707. try
  708. {
  709. SnmpDebugLog :: Startup () ;
  710. }
  711. catch ( ... )
  712. {
  713. // we do not want to left s_Reference count up
  714. SnmpThreadObject :: Closedown () ;
  715. ProviderClosedown () ;
  716. throw;
  717. }
  718. BOOL bStatus = FALSE;
  719. try
  720. {
  721. bStatus = SnmpClassLibrary :: Startup () ;
  722. }
  723. catch ( ... )
  724. {
  725. // we do not want to left s_Reference count up
  726. SnmpDebugLog :: Closedown () ;
  727. SnmpThreadObject :: Closedown () ;
  728. ProviderClosedown () ;
  729. throw;
  730. }
  731. if ( bStatus == FALSE )
  732. {
  733. SnmpDebugLog :: Closedown () ;
  734. SnmpThreadObject :: Closedown () ;
  735. ProviderClosedown () ;
  736. }
  737. else
  738. {
  739. try
  740. {
  741. g_pWorkerThread = new CEventProviderWorkerThread;
  742. g_pWorkerThread->BeginThread();
  743. g_pWorkerThread->WaitForStartup();
  744. g_pWorkerThread->CreateServerWrap () ;
  745. g_pProvThrd = new CEventProviderThread;
  746. bContinue = TRUE;
  747. }
  748. catch ( ... )
  749. {
  750. if ( g_pWorkerThread )
  751. {
  752. g_pWorkerThread->SignalThreadShutdown();
  753. g_pWorkerThread = NULL;
  754. }
  755. g_pProvThrd = NULL;
  756. SnmpClassLibrary :: Closedown () ;
  757. SnmpDebugLog :: Closedown () ;
  758. SnmpThreadObject :: Closedown () ;
  759. ProviderClosedown () ;
  760. throw;
  761. }
  762. }
  763. }
  764. else
  765. {
  766. bContinue = TRUE;
  767. }
  768. if ( bContinue )
  769. {
  770. CTrapEventProvider* prov = new CTrapEventProvider(CMapToEvent::EMappingType::ENCAPSULATED_MAPPER, g_pProvThrd);
  771. LeaveCriticalSection ( & s_ProviderCriticalSection ) ;
  772. bEnteredCritSec = FALSE;
  773. status = prov->QueryInterface (riid, ppvObject);
  774. if (NOERROR != status)
  775. {
  776. delete prov;
  777. }
  778. }
  779. if ( bEnteredCritSec )
  780. {
  781. LeaveCriticalSection ( & s_ProviderCriticalSection ) ;
  782. bEnteredCritSec = FALSE;
  783. }
  784. }
  785. return status ;
  786. }
  787. catch(Structured_Exception e_SE)
  788. {
  789. if ( bEnteredCritSec )
  790. {
  791. LeaveCriticalSection ( & s_ProviderCriticalSection ) ;
  792. bEnteredCritSec = FALSE;
  793. }
  794. return E_UNEXPECTED;
  795. }
  796. catch(Heap_Exception e_HE)
  797. {
  798. if ( bEnteredCritSec )
  799. {
  800. LeaveCriticalSection ( & s_ProviderCriticalSection ) ;
  801. bEnteredCritSec = FALSE;
  802. }
  803. return E_OUTOFMEMORY;
  804. }
  805. catch(...)
  806. {
  807. if ( bEnteredCritSec )
  808. {
  809. LeaveCriticalSection ( & s_ProviderCriticalSection ) ;
  810. bEnteredCritSec = FALSE;
  811. }
  812. return E_UNEXPECTED;
  813. }
  814. }
  815. //***************************************************************************
  816. //
  817. // CSNMPRefEventProviderClassFactory::CreateInstance
  818. //
  819. // Purpose: Instantiates a Provider object returning an interface pointer.
  820. //
  821. // Parameters:
  822. // pUnkOuter LPUNKNOWN to the controlling IUnknown if we are
  823. // being used in an aggregation.
  824. // riid REFIID identifying the interface the caller
  825. // desires to have for the new object.
  826. // ppvObj PPVOID in which to store the desired
  827. // interface pointer for the new object.
  828. //
  829. // Return Value:
  830. // HRESULT NOERROR if successful, otherwise E_NOINTERFACE
  831. // if we cannot support the requested interface.
  832. //***************************************************************************
  833. STDMETHODIMP CSNMPRefEventProviderClassFactory :: CreateInstance(LPUNKNOWN pUnkOuter ,
  834. REFIID riid,
  835. LPVOID FAR * ppvObject
  836. )
  837. {
  838. SetStructuredExceptionHandler seh;
  839. BOOL bEnteredCritSec = FALSE;
  840. BOOL bContinue = FALSE;
  841. try
  842. {
  843. HRESULT status = E_FAIL;
  844. if ( pUnkOuter )
  845. {
  846. status = CLASS_E_NOAGGREGATION;
  847. }
  848. else
  849. {
  850. EnterCriticalSection ( & s_ProviderCriticalSection ) ;
  851. bEnteredCritSec = TRUE;
  852. if (NULL == g_pProvThrd)
  853. {
  854. ProviderStartup () ;
  855. SnmpThreadObject :: Startup () ;
  856. try
  857. {
  858. SnmpDebugLog :: Startup () ;
  859. }
  860. catch ( ... )
  861. {
  862. // we do not want to left s_Reference count up
  863. SnmpThreadObject :: Closedown () ;
  864. ProviderClosedown () ;
  865. throw;
  866. }
  867. BOOL bStatus = FALSE;
  868. try
  869. {
  870. bStatus = SnmpClassLibrary :: Startup () ;
  871. }
  872. catch ( ... )
  873. {
  874. // we do not want to left s_Reference count up
  875. SnmpDebugLog :: Closedown () ;
  876. SnmpThreadObject :: Closedown () ;
  877. ProviderClosedown () ;
  878. throw;
  879. }
  880. if ( bStatus == FALSE )
  881. {
  882. SnmpDebugLog :: Closedown () ;
  883. SnmpThreadObject :: Closedown () ;
  884. ProviderClosedown () ;
  885. }
  886. else
  887. {
  888. try
  889. {
  890. g_pWorkerThread = new CEventProviderWorkerThread;
  891. g_pWorkerThread->BeginThread();
  892. g_pWorkerThread->WaitForStartup();
  893. g_pWorkerThread->CreateServerWrap () ;
  894. g_pProvThrd = new CEventProviderThread;
  895. bContinue = TRUE;
  896. }
  897. catch ( ... )
  898. {
  899. if ( g_pWorkerThread )
  900. {
  901. g_pWorkerThread->SignalThreadShutdown();
  902. g_pWorkerThread = NULL;
  903. }
  904. SnmpClassLibrary :: Closedown () ;
  905. SnmpDebugLog :: Closedown () ;
  906. SnmpThreadObject :: Closedown () ;
  907. ProviderClosedown () ;
  908. throw;
  909. }
  910. }
  911. }
  912. else
  913. {
  914. bContinue = TRUE;
  915. }
  916. if ( bContinue )
  917. {
  918. CTrapEventProvider* prov = new CTrapEventProvider(CMapToEvent::EMappingType::REFERENT_MAPPER, g_pProvThrd);
  919. LeaveCriticalSection ( & s_ProviderCriticalSection ) ;
  920. bEnteredCritSec = FALSE;
  921. status = prov->QueryInterface (riid, ppvObject);
  922. if (NOERROR != status)
  923. {
  924. delete prov;
  925. }
  926. }
  927. if ( bEnteredCritSec )
  928. {
  929. LeaveCriticalSection ( & s_ProviderCriticalSection ) ;
  930. bEnteredCritSec = FALSE;
  931. }
  932. }
  933. return status ;
  934. }
  935. catch(Structured_Exception e_SE)
  936. {
  937. if ( bEnteredCritSec )
  938. {
  939. LeaveCriticalSection ( & s_ProviderCriticalSection ) ;
  940. bEnteredCritSec = FALSE;
  941. }
  942. return E_UNEXPECTED;
  943. }
  944. catch(Heap_Exception e_HE)
  945. {
  946. if ( bEnteredCritSec )
  947. {
  948. LeaveCriticalSection ( & s_ProviderCriticalSection ) ;
  949. bEnteredCritSec = FALSE;
  950. }
  951. return E_OUTOFMEMORY;
  952. }
  953. catch(...)
  954. {
  955. if ( bEnteredCritSec )
  956. {
  957. LeaveCriticalSection ( & s_ProviderCriticalSection ) ;
  958. bEnteredCritSec = FALSE;
  959. }
  960. return E_UNEXPECTED;
  961. }
  962. }