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.

178 lines
4.4 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-2001 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. if (NULL!=pUnkOuter)
  82. {
  83. hr = CLASS_E_NOAGGREGATION;
  84. }
  85. else
  86. {
  87. //==============================================================
  88. //Create the object passing function to notify on destruction.
  89. //==============================================================
  90. if (m_ClsId == CLSID_WMIProvider)
  91. {
  92. try
  93. {
  94. pObj = (IWbemServices *) new CWMI_Prov;
  95. }
  96. catch(...)
  97. {
  98. hr = WBEM_E_UNEXPECTED;
  99. }
  100. }
  101. else if (m_ClsId == CLSID_WMIEventProvider)
  102. {
  103. try
  104. {
  105. pObj = (IWbemEventProvider *) new CWMIEventProvider(WMIEVENT);
  106. }
  107. catch(...)
  108. {
  109. hr = WBEM_E_UNEXPECTED;
  110. }
  111. }
  112. else if (m_ClsId == CLSID_WMIHiPerfProvider)
  113. {
  114. try
  115. {
  116. pObj = (IWbemEventProvider *) new CWMIHiPerfProvider;
  117. }
  118. catch(...)
  119. {
  120. hr = WBEM_E_UNEXPECTED;
  121. }
  122. }
  123. if(pObj)
  124. {
  125. hr = pObj->QueryInterface(riid, ppvObj);
  126. }
  127. }
  128. //===================================================================
  129. //Kill the object if initial creation or Init failed.
  130. //===================================================================
  131. if (FAILED(hr))
  132. {
  133. if( pObj )
  134. {
  135. SAFE_DELETE_PTR(pObj);
  136. }
  137. }
  138. return hr;
  139. }
  140. ////////////////////////////////////////////////////////////////////////////////////////
  141. //
  142. // Increments or decrements the lock count of the DLL. If the
  143. // lock count goes to zero and there are no objects, the DLL
  144. // is allowed to unload. See DllCanUnloadNow.
  145. //
  146. ////////////////////////////////////////////////////////////////////////////////////////
  147. STDMETHODIMP CProvFactory::LockServer(BOOL fLock)
  148. {
  149. if (fLock)
  150. InterlockedIncrement(&g_cLock);
  151. else
  152. InterlockedDecrement(&g_cLock);
  153. return NOERROR;
  154. }