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.

212 lines
4.9 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. InterlockedDecrement(&m_cRef);
  73. if (0L!=m_cRef)
  74. return m_cRef;
  75. delete this;
  76. return 0L;
  77. }
  78. //***************************************************************************
  79. //
  80. // SCODE CSWbemFactory::CreateInstance
  81. //
  82. // Description:
  83. //
  84. // Instantiates a Translator object returning an interface pointer.
  85. //
  86. // Parameters:
  87. //
  88. // pUnkOuter LPUNKNOWN to the controlling IUnknown if we are
  89. // being used in an aggregation.
  90. // riid REFIID identifying the interface the caller
  91. // desires to have for the new object.
  92. // ppvObj PPVOID in which to store the desired
  93. // interface pointer for the new object.
  94. //
  95. // Return Value:
  96. // HRESULT NOERROR if successful, otherwise E_NOINTERFACE
  97. // if we cannot support the requested interface.
  98. //***************************************************************************
  99. STDMETHODIMP CSWbemFactory::CreateInstance (
  100. IN LPUNKNOWN pUnkOuter,
  101. IN REFIID riid,
  102. OUT PPVOID ppvObj
  103. )
  104. {
  105. IUnknown * pObj = NULL;
  106. HRESULT hr = E_FAIL;
  107. // A good place to ensure everything is initialized OK
  108. EnsureGlobalsInitialized () ;
  109. *ppvObj=NULL;
  110. // This object doesnt support aggregation.
  111. if (NULL!=pUnkOuter)
  112. return CLASS_E_NOAGGREGATION;
  113. if (m_iType == LOCATOR)
  114. pObj = (ISWbemLocator *) new CSWbemLocator;
  115. else if (m_iType == SINK)
  116. {
  117. CSWbemSink *pSWbemSink = new CSWbemSink;
  118. if(pSWbemSink == NULL)
  119. return E_OUTOFMEMORY;
  120. // QueryInterface probably for IID_IUNKNOWN
  121. return pSWbemSink->QueryInterface(riid, ppvObj);
  122. }
  123. else if (m_iType == CONTEXT)
  124. pObj = (ISWbemNamedValueSet *) new CSWbemNamedValueSet;
  125. else if (m_iType == OBJECTPATH)
  126. pObj = (ISWbemObjectPath *) new CSWbemObjectPath;
  127. else if (m_iType == PARSEDN)
  128. pObj = new CWbemParseDN;
  129. else if (m_iType == DATETIME)
  130. pObj = (ISWbemDateTime *) new CSWbemDateTime;
  131. else if (m_iType == REFRESHER)
  132. pObj = (ISWbemRefresher *) new CSWbemRefresher;
  133. else if (m_iType == LASTERROR)
  134. {
  135. EnterCriticalSection (&g_csErrorCache);
  136. if (g_pErrorCache)
  137. pObj = (ISWbemObject* ) g_pErrorCache->GetAndResetCurrentThreadError ();
  138. LeaveCriticalSection (&g_csErrorCache);
  139. }
  140. if (NULL == pObj)
  141. return hr;
  142. hr = pObj->QueryInterface(riid, ppvObj);
  143. //Kill the object if initial creation or Init failed.
  144. if ( FAILED(hr) )
  145. delete pObj;
  146. return hr;
  147. }
  148. //***************************************************************************
  149. //
  150. // SCODE CSWbemFactory::LockServer
  151. //
  152. // Description:
  153. //
  154. // Increments or decrements the lock count of the DLL. If the
  155. // lock count goes to zero and there are no objects, the DLL
  156. // is allowed to unload. See DllCanUnloadNow.
  157. //
  158. // Parameters:
  159. //
  160. // fLock BOOL specifying whether to increment or
  161. // decrement the lock count.
  162. //
  163. // Return Value:
  164. //
  165. // HRESULT NOERROR always.
  166. //***************************************************************************
  167. STDMETHODIMP CSWbemFactory::LockServer(IN BOOL fLock)
  168. {
  169. if (fLock)
  170. InterlockedIncrement((long *)&g_cLock);
  171. else
  172. InterlockedDecrement((long *)&g_cLock);
  173. return NOERROR;
  174. }