Source code of Windows XP (NT5)
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.

284 lines
6.4 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // clsfctry.CPP
  4. //
  5. //
  6. // Purpose: Contains the class factory. This creates objects when
  7. // connections are requested.
  8. //
  9. // History:
  10. //
  11. // cvadai 4/1/99
  12. //
  13. // Copyright (c)1999-2001 Microsoft Corporation, All Rights Reserved
  14. ///////////////////////////////////////////////////////////////////////////////
  15. #define DEFINEGUID
  16. #include "precomp.h"
  17. #include <std.h>
  18. #include "clsfctry.h"
  19. #include "repdrvr.h"
  20. long g_cObj = 0; // Number of objects created
  21. long g_cLock = 0; // Number of locks on the DLL
  22. //-----------------------------------------------------------------------------
  23. // CControllerFactory::CControllerFactory
  24. // CControllerFactory::~CControllerFactory
  25. //
  26. // Constructor Parameters:
  27. // None
  28. //
  29. CControllerFactory::CControllerFactory()
  30. {
  31. m_cRef=0L;
  32. return;
  33. }
  34. CControllerFactory::~CControllerFactory(void)
  35. {
  36. return;
  37. }
  38. //-----------------------------------------------------------------------------
  39. // CControllerFactory::QueryInterface
  40. // CControllerFactory::AddRef
  41. // CControllerFactory::Release
  42. //
  43. // Purpose: Standard Ole routines needed for all interfaces
  44. //
  45. STDMETHODIMP CControllerFactory::QueryInterface(REFIID riid, void ** ppv)
  46. {
  47. *ppv=NULL;
  48. if (IID_IUnknown==riid || IID_IClassFactory==riid)
  49. *ppv=this;
  50. if (NULL!=*ppv)
  51. {
  52. ((LPUNKNOWN)*ppv)->AddRef();
  53. return NOERROR;
  54. }
  55. return ResultFromScode(E_NOINTERFACE);
  56. }
  57. STDMETHODIMP_(ULONG) CControllerFactory::AddRef(void)
  58. {
  59. return ++m_cRef;
  60. }
  61. STDMETHODIMP_(ULONG) CControllerFactory::Release(void)
  62. {
  63. if (0L!=--m_cRef)
  64. return m_cRef;
  65. delete this;
  66. return 0L;
  67. }
  68. //-----------------------------------------------------------------------------
  69. // CControllerFactory::CreateInstance
  70. //
  71. // Purpose: Instantiates a Locator object returning an interface pointer.
  72. //
  73. // Parameters:
  74. // pUnkOuter LPUNKNOWN to the controlling IUnknown if we are
  75. // being used in an aggregation.
  76. // riid REFIID identifying the interface the caller
  77. // desires to have for the new object.
  78. // ppvObj PPVOID in which to store the desired
  79. // interface pointer for the new object.
  80. //
  81. // Return Value:
  82. // HRESULT NOERROR if successful, otherwise E_NOINTERFACE
  83. // if we cannot support the requested interface.
  84. //
  85. //
  86. STDMETHODIMP CControllerFactory::CreateInstance
  87. (
  88. LPUNKNOWN pUnkOuter,
  89. REFIID riid,
  90. void ** ppvObj
  91. )
  92. {
  93. IUnknown * pObj = NULL;
  94. HRESULT hr;
  95. *ppvObj=NULL;
  96. hr=ResultFromScode(E_OUTOFMEMORY);
  97. // This object doesnt support aggregation.
  98. if (NULL!=pUnkOuter)
  99. return ResultFromScode(CLASS_E_NOAGGREGATION);
  100. //Create the object passing function to notify on destruction.
  101. pObj = new CWmiDbController();
  102. if (NULL==pObj)
  103. return hr;
  104. g_cObj++; // Only controllers should make the DLL stick around, right?
  105. hr=pObj->QueryInterface(riid, ppvObj);
  106. //Kill the object if initial creation or Init failed.
  107. if (FAILED(hr))
  108. delete pObj;
  109. return hr;
  110. }
  111. //-----------------------------------------------------------------------------
  112. // CControllerFactory::LockServer
  113. //
  114. // Purpose:
  115. // Increments or decrements the lock count of the DLL. If the
  116. // lock count goes to zero and there are no objects, the DLL
  117. // is allowed to unload. See DllCanUnloadNow.
  118. //
  119. // Parameters:
  120. // fLock BOOL specifying whether to increment or
  121. // decrement the lock count.
  122. //
  123. // Return Value:
  124. // HRESULT NOERROR always.
  125. //
  126. STDMETHODIMP CControllerFactory::LockServer(BOOL fLock)
  127. {
  128. if (fLock)
  129. InterlockedIncrement(&g_cLock);
  130. else
  131. InterlockedDecrement(&g_cLock);
  132. return NOERROR;
  133. }
  134. //-----------------------------------------------------------------------------
  135. // CQueryFactory::CQueryFactory
  136. // CQueryFactory::~CQueryFactory
  137. //
  138. // Constructor Parameters:
  139. // None
  140. //
  141. CQueryFactory::CQueryFactory()
  142. {
  143. m_cRef=0L;
  144. return;
  145. }
  146. CQueryFactory::~CQueryFactory(void)
  147. {
  148. return;
  149. }
  150. //-----------------------------------------------------------------------------
  151. // CQueryFactory::QueryInterface
  152. // CQueryFactory::AddRef
  153. // CQueryFactory::Release
  154. //
  155. // Purpose: Standard Ole routines needed for all interfaces
  156. //
  157. STDMETHODIMP CQueryFactory::QueryInterface(REFIID riid, void ** ppv)
  158. {
  159. *ppv=NULL;
  160. if (IID_IUnknown==riid || IID_IClassFactory==riid)
  161. *ppv=this;
  162. if (NULL!=*ppv)
  163. {
  164. ((LPUNKNOWN)*ppv)->AddRef();
  165. return NOERROR;
  166. }
  167. return ResultFromScode(E_NOINTERFACE);
  168. }
  169. STDMETHODIMP_(ULONG) CQueryFactory::AddRef(void)
  170. {
  171. return ++m_cRef;
  172. }
  173. STDMETHODIMP_(ULONG) CQueryFactory::Release(void)
  174. {
  175. if (0L!=--m_cRef)
  176. return m_cRef;
  177. delete this;
  178. return 0L;
  179. }
  180. //-----------------------------------------------------------------------------
  181. // CQueryFactory::CreateInstance
  182. //
  183. // Purpose: Instantiates a Locator object returning an interface pointer.
  184. //
  185. // Parameters:
  186. // pUnkOuter LPUNKNOWN to the controlling IUnknown if we are
  187. // being used in an aggregation.
  188. // riid REFIID identifying the interface the caller
  189. // desires to have for the new object.
  190. // ppvObj PPVOID in which to store the desired
  191. // interface pointer for the new object.
  192. //
  193. // Return Value:
  194. // HRESULT NOERROR if successful, otherwise E_NOINTERFACE
  195. // if we cannot support the requested interface.
  196. //
  197. //
  198. STDMETHODIMP CQueryFactory::CreateInstance
  199. (
  200. LPUNKNOWN pUnkOuter,
  201. REFIID riid,
  202. void ** ppvObj
  203. )
  204. {
  205. return E_FAIL;
  206. }
  207. //-----------------------------------------------------------------------------
  208. // CQueryFactory::LockServer
  209. //
  210. // Purpose:
  211. // Increments or decrements the lock count of the DLL. If the
  212. // lock count goes to zero and there are no objects, the DLL
  213. // is allowed to unload. See DllCanUnloadNow.
  214. //
  215. // Parameters:
  216. // fLock BOOL specifying whether to increment or
  217. // decrement the lock count.
  218. //
  219. // Return Value:
  220. // HRESULT NOERROR always.
  221. //
  222. STDMETHODIMP CQueryFactory::LockServer(BOOL fLock)
  223. {
  224. if (fLock)
  225. InterlockedIncrement(&g_cLock);
  226. else
  227. InterlockedDecrement(&g_cLock);
  228. return NOERROR;
  229. }