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.

339 lines
8.3 KiB

  1. //***************************************************************************
  2. //
  3. // Copyright (c) 1997-2001 Microsoft Corporation, All Rights Reserved
  4. //
  5. // CLSFCTRY.CPP
  6. //
  7. // Purpose: Implementation of CWbemGlueFactory class
  8. //
  9. //***************************************************************************
  10. #include "precomp.h"
  11. #include <BrodCast.h>
  12. #include <assertbreak.h>
  13. #define DUPLICATE_RELEASE 0
  14. /////////////////////////////////////////////////////////////////////
  15. //
  16. // Function: CWbemGlueFactory::CWbemGlueFactory
  17. //
  18. // Class CTor. This is the class factory for the Wbem Provider
  19. // framework.
  20. //
  21. // Inputs: None.
  22. //
  23. // Outputs: None.
  24. //
  25. // Returns: None.
  26. //
  27. // Comments: This is the backward compatibility constructor. It
  28. // uses CLSID_NULL, which it will share with all
  29. // old-fashioned providers.
  30. //
  31. /////////////////////////////////////////////////////////////////////
  32. CWbemGlueFactory::CWbemGlueFactory()
  33. : m_lRefCount( 0 )
  34. {
  35. LogMessage2(L"CWbemGlueFactory::CWbemGlueFactory(NULL) %p", this);
  36. CWbemProviderGlue::AddToFactoryMap(this, NULL);
  37. CWbemProviderGlue::IncrementMapCount(this);
  38. }
  39. /////////////////////////////////////////////////////////////////////
  40. //
  41. // Function: CWbemGlueFactory::CWbemGlueFactory
  42. //
  43. // Class CTor. This is the class factory for the Wbem Provider
  44. // framework.
  45. //
  46. // Inputs: None.
  47. //
  48. // Outputs: None.
  49. //
  50. // Returns: None.
  51. //
  52. // Comments:
  53. //
  54. /////////////////////////////////////////////////////////////////////
  55. CWbemGlueFactory::CWbemGlueFactory(PLONG pLong)
  56. : m_lRefCount( 0 )
  57. {
  58. LogMessage3(L"CWbemGlueFactory::CWbemGlueFactory(%p) %p", pLong, this);
  59. CWbemProviderGlue::AddToFactoryMap(this, pLong);
  60. CWbemProviderGlue::IncrementMapCount(this);
  61. }
  62. /////////////////////////////////////////////////////////////////////
  63. //
  64. // Function: CWbemGlueFactory::~CWbemGlueFactory
  65. //
  66. // Class DTor.
  67. //
  68. // Inputs: None.
  69. //
  70. // Outputs: None.
  71. //
  72. // Returns: None.
  73. //
  74. // Comments: None.
  75. //
  76. /////////////////////////////////////////////////////////////////////
  77. CWbemGlueFactory::~CWbemGlueFactory(void)
  78. {
  79. try
  80. {
  81. LogMessage2(L"CWbemGlueFactory::~CWbemGlueFactory(%p)", this);
  82. }
  83. catch ( ... )
  84. {
  85. }
  86. CWbemProviderGlue::DecrementMapCount(this);
  87. CWbemProviderGlue::RemoveFromFactoryMap(this);
  88. }
  89. /////////////////////////////////////////////////////////////////////
  90. //
  91. // Function: CWbemGlueFactory::QueryInterface
  92. //
  93. // COM function called to ask us if we support a particular
  94. // face type. If so, we addref ourselves and return the
  95. // ourselves as an LPVOID.
  96. //
  97. // Inputs: REFIID riid - Interface being queried for.
  98. //
  99. // Outputs: LPVOID FAR* ppvObj - Interface pointer.
  100. //
  101. // Returns: None.
  102. //
  103. // Comments: The only interfaces we support are IID_IUnknown and
  104. // IID_IClassFactory.
  105. //
  106. /////////////////////////////////////////////////////////////////////
  107. STDMETHODIMP CWbemGlueFactory::QueryInterface(REFIID riid, PPVOID ppv)
  108. {
  109. *ppv=NULL;
  110. if (IID_IUnknown==riid || IID_IClassFactory==riid) {
  111. *ppv=this;
  112. }
  113. if (NULL!=*ppv)
  114. {
  115. AddRef();
  116. try
  117. {
  118. LogMessage(L"CWbemGlueFactory::QueryInterface");
  119. }
  120. catch ( ... )
  121. {
  122. }
  123. return NOERROR;
  124. }
  125. else
  126. {
  127. try
  128. {
  129. LogErrorMessage(L"CWbemGlueFactory::QueryInterface FAILED!");
  130. }
  131. catch ( ... )
  132. {
  133. }
  134. }
  135. return ResultFromScode(E_NOINTERFACE);
  136. }
  137. /////////////////////////////////////////////////////////////////////
  138. //
  139. // Function: CWbemGlueFactory::AddRef
  140. //
  141. // Increments the reference count on this object.
  142. //
  143. // Inputs: None.
  144. //
  145. // Outputs: None.
  146. //
  147. // Returns: ULONG - Our Reference Count.
  148. //
  149. // Comments: Requires that a correponding call to Release be
  150. // performed.
  151. //
  152. /////////////////////////////////////////////////////////////////////
  153. STDMETHODIMP_(ULONG) CWbemGlueFactory::AddRef(void)
  154. {
  155. try
  156. {
  157. LogMessage(L"CWbemGlueFactory::AddRef()");
  158. }
  159. catch ( ... )
  160. {
  161. }
  162. // InterlockedIncrement does not necessarily return the
  163. // correct value, only whether the value is <, =, > 0.
  164. // However it is guaranteed threadsafe.
  165. return InterlockedIncrement( &m_lRefCount );
  166. }
  167. /////////////////////////////////////////////////////////////////////
  168. //
  169. // Function: CWbemGlueFactory::Release
  170. //
  171. // Decrements the reference count on this object.
  172. //
  173. // Inputs: None.
  174. //
  175. // Outputs: None.
  176. //
  177. // Returns: ULONG - Our Reference Count.
  178. //
  179. // Comments: When the ref count hits zero, the object is deleted.
  180. //
  181. /////////////////////////////////////////////////////////////////////
  182. STDMETHODIMP_(ULONG) CWbemGlueFactory::Release(void)
  183. {
  184. try
  185. {
  186. LogMessage(L"CWbemGlueFactory::Release()");
  187. }
  188. catch ( ... )
  189. {
  190. }
  191. // InterlockedDecrement does not necessarily return the
  192. // correct value, only whether the value is <, =, > 0.
  193. // However it is guaranteed threadsafe.
  194. // We want to hold the value locally in case two threads
  195. // Release at the same time and one gets a final release,
  196. // and deletes, leaving a potential window in which a thread
  197. // deletes the object before the other returns and tries to
  198. // reference the value from within the deleted object.
  199. ULONG nRet = InterlockedDecrement( &m_lRefCount );
  200. if( 0 == nRet )
  201. {
  202. delete this ;
  203. }
  204. else if (nRet > 0x80000000)
  205. {
  206. ASSERT_BREAK(DUPLICATE_RELEASE);
  207. LogErrorMessage(L"Duplicate WbemGlueFactory Release()");
  208. }
  209. return nRet;
  210. }
  211. /////////////////////////////////////////////////////////////////////
  212. //
  213. // Function: CWbemGlueFactory::CreateInstance
  214. //
  215. // Creates an instance of a locator object from which a provider
  216. // can be instantiated.
  217. //
  218. // Inputs: LPUNKNOWN pUnkOuter - to the controlling IUnknown if we are
  219. // being used in an aggregation.
  220. // REFIID riid - REFIID identifying the interface the caller
  221. // desires to have for the new object.
  222. //
  223. // Outputs: PPVOID ppvObj - in which to store the desired
  224. // interface pointer for the new object.
  225. //
  226. // Returns: HRESULT NOERROR if successful,
  227. // otherwise E_NOINTERFACE if we cannot support the requested interface.
  228. //
  229. // Comments: When the ref count hits zero, the object is deleted.
  230. //
  231. /////////////////////////////////////////////////////////////////////
  232. STDMETHODIMP CWbemGlueFactory::CreateInstance(LPUNKNOWN pUnkOuter , REFIID riid, PPVOID ppvObj)
  233. {
  234. *ppvObj=NULL;
  235. HRESULT hr = ResultFromScode(E_OUTOFMEMORY);
  236. // This object doesn't support aggregation.
  237. if (NULL!=pUnkOuter)
  238. {
  239. return ResultFromScode(CLASS_E_NOAGGREGATION);
  240. }
  241. try
  242. {
  243. IWbemServices *pObj= new CWbemProviderGlue(CWbemProviderGlue::GetMapCountPtr(this));
  244. if (pObj)
  245. {
  246. hr=pObj->QueryInterface(riid, ppvObj);
  247. }
  248. if (SUCCEEDED(hr))
  249. {
  250. LogMessage(L"CWbemGlueFactory::CreateInstance() - Succeeded");
  251. }
  252. else
  253. {
  254. delete pObj;
  255. LogMessage2(L"CWbemGlueFactory::CreateInstance() - Failed (%x)", hr);
  256. }
  257. }
  258. catch ( ... )
  259. {
  260. }
  261. return hr;
  262. }
  263. /////////////////////////////////////////////////////////////////////
  264. //
  265. // Function: CWbemGlueFactory::LockServer
  266. //
  267. // Increment/Decrements the lock count on this DLL.
  268. //
  269. // Inputs: BOOL fLock - Lock/Unlock
  270. //
  271. // Outputs: None.
  272. //
  273. // Returns: HRESULT - NOERROR at this time.
  274. //
  275. // Comments: When the ref count hits zero, the object is deleted.
  276. //
  277. /////////////////////////////////////////////////////////////////////
  278. STDMETHODIMP CWbemGlueFactory::LockServer(BOOL fLock)
  279. {
  280. try
  281. {
  282. if (IsVerboseLoggingEnabled())
  283. {
  284. CHString str;
  285. if (fLock)
  286. {
  287. LogMessage(L"CWbemGlueFactory::LockServer(TRUE)");
  288. }
  289. else
  290. {
  291. LogMessage(L"CWbemGlueFactory::LockServer(FALSE)");
  292. }
  293. }
  294. }
  295. catch ( ... )
  296. {
  297. }
  298. return CoLockObjectExternal((IUnknown *)this, fLock, FALSE);
  299. }