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.

162 lines
3.8 KiB

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