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.

159 lines
3.9 KiB

  1. //***************************************************************************
  2. //
  3. // CLASSFAC.CPP
  4. //
  5. // Module: WBEM Instance provider sample code
  6. //
  7. // Purpose: Contains the class factory. This creates objects when
  8. // connections are requested.
  9. //
  10. // Copyright (c) 1997-2001 Microsoft Corporation, All Rights Reserved
  11. //
  12. //***************************************************************************
  13. #include "precomp.h"
  14. #include <objbase.h>
  15. #include "classfac.h"
  16. //***************************************************************************
  17. //
  18. // CProvFactory::CProvFactory
  19. // CProvFactory::~CProvFactory
  20. //
  21. // Constructor Parameters:
  22. // None
  23. //***************************************************************************
  24. CProvFactory::CProvFactory()
  25. {
  26. InterlockedIncrement(&g_cObj);
  27. m_cRef=0L;
  28. return;
  29. }
  30. CProvFactory::~CProvFactory(void)
  31. {
  32. InterlockedDecrement(&g_cObj);
  33. return;
  34. }
  35. //***************************************************************************
  36. //
  37. // CProvFactory::QueryInterface
  38. // CProvFactory::AddRef
  39. // CProvFactory::Release
  40. //
  41. // Purpose: Standard Ole routines needed for all interfaces
  42. //
  43. //***************************************************************************
  44. STDMETHODIMP CProvFactory::QueryInterface(REFIID riid
  45. , LPVOID * ppv)
  46. {
  47. *ppv=NULL;
  48. if (IID_IUnknown==riid || IID_IClassFactory==riid) *ppv=this;
  49. if (NULL!=*ppv){
  50. ((LPUNKNOWN)*ppv)->AddRef();
  51. return NOERROR;
  52. }
  53. return E_NOINTERFACE;
  54. }
  55. STDMETHODIMP_(ULONG) CProvFactory::AddRef(void)
  56. {
  57. return ++m_cRef;
  58. }
  59. STDMETHODIMP_(ULONG) CProvFactory::Release(void)
  60. {
  61. ULONG nNewCount = InterlockedDecrement((long *)&m_cRef);
  62. if (0L == nNewCount) delete this;
  63. return nNewCount;
  64. }
  65. //***************************************************************************
  66. //
  67. // CProvFactory::CreateInstance
  68. //
  69. // Purpose: Instantiates a Locator object returning an interface pointer.
  70. //
  71. // Parameters:
  72. // pUnkOuter LPUNKNOWN to the controlling IUnknown if we are
  73. // being used in an aggregation.
  74. // riid REFIID identifying the interface the caller
  75. // desires to have for the new object.
  76. // ppvObj PPVOID in which to store the desired
  77. // interface pointer for the new object.
  78. //
  79. // Return Value:
  80. // HRESULT NOERROR if successful, otherwise E_NOINTERFACE
  81. // if we cannot support the requested interface.
  82. //***************************************************************************
  83. STDMETHODIMP CProvFactory::CreateInstance(LPUNKNOWN pUnkOuter
  84. , REFIID riid, LPVOID * ppvObj)
  85. {
  86. CMSIProv *pObj;
  87. HRESULT hr = WBEM_S_NO_ERROR;
  88. *ppvObj=NULL;
  89. hr = E_OUTOFMEMORY;
  90. // This object doesnt support aggregation.
  91. if(NULL!=pUnkOuter) return CLASS_E_NOAGGREGATION;
  92. // Create the locator object.
  93. pObj=new CMSIProv();
  94. if (NULL==pObj)
  95. return E_OUTOFMEMORY;
  96. hr=pObj->QueryInterface(riid, ppvObj);
  97. //Kill the object if initial creation or Init failed.
  98. if(FAILED(hr)) delete pObj;
  99. return hr;
  100. }
  101. //***************************************************************************
  102. //
  103. // CProvFactory::LockServer
  104. //
  105. // Purpose:
  106. // Increments or decrements the lock count of the DLL. If the
  107. // lock count goes to zero and there are no objects, the DLL
  108. // is allowed to unload. See DllCanUnloadNow.
  109. //
  110. // Parameters:
  111. // fLock BOOL specifying whether to increment or
  112. // decrement the lock count.
  113. //
  114. // Return Value:
  115. // HRESULT NOERROR always.
  116. //***************************************************************************
  117. STDMETHODIMP CProvFactory::LockServer(BOOL fLock)
  118. {
  119. if (fLock) InterlockedIncrement(&g_cLock);
  120. else InterlockedDecrement(&g_cLock);
  121. return NOERROR;
  122. }