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.

215 lines
5.2 KiB

  1. //***************************************************************************
  2. //
  3. // Copyright (c) 1998-2000 Microsoft Corporation
  4. //
  5. // CLASSFAC.CPP
  6. //
  7. // alanbos 13-Feb-98 Created.
  8. //
  9. // Contains the class factory.
  10. //
  11. //***************************************************************************
  12. #include "precomp.h"
  13. extern CWbemErrorCache *g_pErrorCache;
  14. extern CRITICAL_SECTION g_csErrorCache;
  15. //***************************************************************************
  16. //
  17. // CSWbemFactory::CSWbemFactory
  18. //
  19. // DESCRIPTION:
  20. //
  21. // Constructor
  22. //
  23. //***************************************************************************
  24. CSWbemFactory::CSWbemFactory(int iType)
  25. {
  26. m_cRef=0L;
  27. m_iType = iType;
  28. return;
  29. }
  30. //***************************************************************************
  31. //
  32. // CSWbemFactory::~CSWbemFactory
  33. //
  34. // DESCRIPTION:
  35. //
  36. // Destructor
  37. //
  38. //***************************************************************************
  39. CSWbemFactory::~CSWbemFactory(void)
  40. {
  41. return;
  42. }
  43. //***************************************************************************
  44. //
  45. // CSWbemFactory::QueryInterface
  46. // CSWbemFactory::AddRef
  47. // CSWbemFactory::Release
  48. //
  49. // Purpose: Standard Ole routines needed for all interfaces
  50. //
  51. //***************************************************************************
  52. STDMETHODIMP CSWbemFactory::QueryInterface(REFIID riid
  53. , LPVOID *ppv)
  54. {
  55. *ppv=NULL;
  56. if (IID_IUnknown==riid || IID_IClassFactory==riid)
  57. *ppv=this;
  58. if (NULL!=*ppv)
  59. {
  60. ((LPUNKNOWN)*ppv)->AddRef();
  61. return NOERROR;
  62. }
  63. return ResultFromScode(E_NOINTERFACE);
  64. }
  65. STDMETHODIMP_(ULONG) CSWbemFactory::AddRef(void)
  66. {
  67. InterlockedIncrement(&m_cRef);
  68. return m_cRef;
  69. }
  70. STDMETHODIMP_(ULONG) CSWbemFactory::Release(void)
  71. {
  72. LONG cRef = InterlockedDecrement(&m_cRef);
  73. if (0 != cRef)
  74. {
  75. _ASSERT(cRef > 0);
  76. return cRef;
  77. }
  78. delete this;
  79. return 0;
  80. }
  81. //***************************************************************************
  82. //
  83. // SCODE CSWbemFactory::CreateInstance
  84. //
  85. // Description:
  86. //
  87. // Instantiates a Translator object returning an interface pointer.
  88. //
  89. // Parameters:
  90. //
  91. // pUnkOuter LPUNKNOWN to the controlling IUnknown if we are
  92. // being used in an aggregation.
  93. // riid REFIID identifying the interface the caller
  94. // desires to have for the new object.
  95. // ppvObj PPVOID in which to store the desired
  96. // interface pointer for the new object.
  97. //
  98. // Return Value:
  99. // HRESULT NOERROR if successful, otherwise E_NOINTERFACE
  100. // if we cannot support the requested interface.
  101. //***************************************************************************
  102. STDMETHODIMP CSWbemFactory::CreateInstance (
  103. IN LPUNKNOWN pUnkOuter,
  104. IN REFIID riid,
  105. OUT PPVOID ppvObj
  106. )
  107. {
  108. IUnknown * pObj = NULL;
  109. HRESULT hr = E_FAIL;
  110. // A good place to ensure everything is initialized OK
  111. EnsureGlobalsInitialized () ;
  112. *ppvObj=NULL;
  113. // This object doesnt support aggregation.
  114. if (NULL!=pUnkOuter)
  115. return CLASS_E_NOAGGREGATION;
  116. if (m_iType == LOCATOR)
  117. pObj = (ISWbemLocator *) new CSWbemLocator;
  118. else if (m_iType == SINK)
  119. {
  120. CSWbemSink *pSWbemSink = new CSWbemSink;
  121. if(pSWbemSink == NULL)
  122. return E_OUTOFMEMORY;
  123. // QueryInterface probably for IID_IUNKNOWN
  124. return pSWbemSink->QueryInterface(riid, ppvObj);
  125. }
  126. else if (m_iType == CONTEXT)
  127. pObj = (ISWbemNamedValueSet *) new CSWbemNamedValueSet;
  128. else if (m_iType == OBJECTPATH)
  129. pObj = (ISWbemObjectPath *) new CSWbemObjectPath;
  130. else if (m_iType == PARSEDN)
  131. pObj = new CWbemParseDN;
  132. else if (m_iType == DATETIME)
  133. pObj = (ISWbemDateTime *) new CSWbemDateTime;
  134. else if (m_iType == REFRESHER)
  135. pObj = (ISWbemRefresher *) new CSWbemRefresher;
  136. else if (m_iType == LASTERROR)
  137. {
  138. EnterCriticalSection (&g_csErrorCache);
  139. if (g_pErrorCache)
  140. pObj = (ISWbemObject* ) g_pErrorCache->GetAndResetCurrentThreadError ();
  141. LeaveCriticalSection (&g_csErrorCache);
  142. }
  143. if (NULL == pObj)
  144. return hr;
  145. hr = pObj->QueryInterface(riid, ppvObj);
  146. //Kill the object if initial creation or Init failed.
  147. if ( FAILED(hr) )
  148. delete pObj;
  149. return hr;
  150. }
  151. //***************************************************************************
  152. //
  153. // SCODE CSWbemFactory::LockServer
  154. //
  155. // Description:
  156. //
  157. // Increments or decrements the lock count of the DLL. If the
  158. // lock count goes to zero and there are no objects, the DLL
  159. // is allowed to unload. See DllCanUnloadNow.
  160. //
  161. // Parameters:
  162. //
  163. // fLock BOOL specifying whether to increment or
  164. // decrement the lock count.
  165. //
  166. // Return Value:
  167. //
  168. // HRESULT NOERROR always.
  169. //***************************************************************************
  170. STDMETHODIMP CSWbemFactory::LockServer(IN BOOL fLock)
  171. {
  172. if (fLock)
  173. InterlockedIncrement((long *)&g_cLock);
  174. else
  175. InterlockedDecrement((long *)&g_cLock);
  176. return NOERROR;
  177. }