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.

205 lines
5.2 KiB

  1. ////////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // CLASSFAC.CPP
  4. //
  5. // Purpose: Contains the class factory. This creates objects when
  6. // connections are requested.
  7. //
  8. // Copyright (c) 1997-2002 Microsoft Corporation, All Rights Reserved
  9. //
  10. ////////////////////////////////////////////////////////////////////////////////////////
  11. #include "precomp.h"
  12. #include "wdmdefs.h"
  13. ////////////////////////////////////////////////////////////////////////////////////////
  14. //
  15. // Constructor
  16. //
  17. ////////////////////////////////////////////////////////////////////////////////////////
  18. CProvFactory::CProvFactory(const CLSID & ClsId)
  19. {
  20. m_cRef=0L;
  21. InterlockedIncrement((LONG *) &g_cObj);
  22. m_ClsId = ClsId;
  23. }
  24. ////////////////////////////////////////////////////////////////////////////////////////
  25. //
  26. // Destructor
  27. //
  28. ////////////////////////////////////////////////////////////////////////////////////////
  29. CProvFactory::~CProvFactory(void)
  30. {
  31. InterlockedDecrement((LONG *) &g_cObj);
  32. }
  33. ////////////////////////////////////////////////////////////////////////////////////////
  34. //
  35. // Standard Ole routines needed for all interfaces
  36. //
  37. ////////////////////////////////////////////////////////////////////////////////////////
  38. STDMETHODIMP CProvFactory::QueryInterface(REFIID riid, PPVOID ppv)
  39. {
  40. HRESULT hr = E_NOINTERFACE;
  41. *ppv=NULL;
  42. if (IID_IUnknown==riid || IID_IClassFactory==riid)
  43. {
  44. *ppv=this;
  45. }
  46. if (NULL!=*ppv)
  47. {
  48. AddRef();
  49. hr = NOERROR;
  50. }
  51. return hr;
  52. }
  53. /////////////////////////////////////////////////////////////////////////
  54. STDMETHODIMP_(ULONG) CProvFactory::AddRef(void)
  55. {
  56. return InterlockedIncrement((long*)&m_cRef);
  57. }
  58. /////////////////////////////////////////////////////////////////////////
  59. STDMETHODIMP_(ULONG) CProvFactory::Release(void)
  60. {
  61. ULONG cRef = InterlockedDecrement( (long*) &m_cRef);
  62. if ( !cRef ){
  63. delete this;
  64. return 0;
  65. }
  66. return cRef;
  67. }
  68. ////////////////////////////////////////////////////////////////////////////////////////
  69. //
  70. // Instantiates an object returning an interface pointer.
  71. //
  72. ////////////////////////////////////////////////////////////////////////////////////////
  73. STDMETHODIMP CProvFactory::CreateInstance(LPUNKNOWN pUnkOuter, REFIID riid, PPVOID ppvObj)
  74. {
  75. HRESULT hr = E_OUTOFMEMORY;
  76. IUnknown* pObj = NULL;
  77. *ppvObj=NULL;
  78. //==================================================================
  79. // This object doesnt support aggregation.
  80. //==================================================================
  81. try
  82. {
  83. if (NULL!=pUnkOuter)
  84. {
  85. hr = CLASS_E_NOAGGREGATION;
  86. }
  87. else
  88. {
  89. //==============================================================
  90. //Create the object passing function to notify on destruction.
  91. //==============================================================
  92. if (m_ClsId == CLSID_WMIProvider)
  93. {
  94. CWMI_Prov * ptr = new CWMI_Prov () ;
  95. if( ptr )
  96. {
  97. if ( FALSE == ptr->Initialized () )
  98. {
  99. delete ptr ;
  100. ptr = NULL ;
  101. hr = E_FAIL ;
  102. }
  103. else
  104. {
  105. if ( FAILED ( hr = ptr->QueryInterface ( __uuidof ( IUnknown ), ( void ** ) &pObj ) ) )
  106. {
  107. delete ptr ;
  108. ptr = NULL ;
  109. }
  110. }
  111. }
  112. }
  113. else if (m_ClsId == CLSID_WMIEventProvider)
  114. {
  115. CWMIEventProvider *ptr = new CWMIEventProvider ( WMIEVENT ) ;
  116. if( ptr )
  117. {
  118. if ( FALSE == ptr->Initialized () )
  119. {
  120. delete ptr ;
  121. ptr = NULL ;
  122. hr = E_FAIL ;
  123. }
  124. else
  125. {
  126. if ( FAILED ( hr = ptr->QueryInterface ( __uuidof ( IUnknown ), ( void ** ) &pObj ) ) )
  127. {
  128. delete ptr ;
  129. ptr = NULL ;
  130. }
  131. }
  132. }
  133. }
  134. else if (m_ClsId == CLSID_WMIHiPerfProvider)
  135. {
  136. CWMIHiPerfProvider *ptr = new CWMIHiPerfProvider ( ) ;
  137. if( ptr )
  138. {
  139. if ( FALSE == ptr->Initialized () )
  140. {
  141. delete ptr ;
  142. ptr = NULL ;
  143. hr = E_FAIL ;
  144. }
  145. else
  146. {
  147. if ( FAILED ( hr = ptr->QueryInterface ( __uuidof ( IUnknown ), ( void ** ) &pObj ) ) )
  148. {
  149. delete ptr ;
  150. ptr = NULL ;
  151. }
  152. }
  153. }
  154. }
  155. }
  156. }
  157. catch ( Heap_Exception & e )
  158. {
  159. }
  160. if ( pObj )
  161. {
  162. hr = pObj->QueryInterface(riid, ppvObj);
  163. pObj->Release () ;
  164. pObj = NULL ;
  165. }
  166. return hr;
  167. }
  168. ////////////////////////////////////////////////////////////////////////////////////////
  169. //
  170. // Increments or decrements the lock count of the DLL. If the
  171. // lock count goes to zero and there are no objects, the DLL
  172. // is allowed to unload. See DllCanUnloadNow.
  173. //
  174. ////////////////////////////////////////////////////////////////////////////////////////
  175. STDMETHODIMP CProvFactory::LockServer(BOOL fLock)
  176. {
  177. if (fLock)
  178. InterlockedIncrement(&g_cLock);
  179. else
  180. InterlockedDecrement(&g_cLock);
  181. return NOERROR;
  182. }