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.

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