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.

179 lines
4.1 KiB

  1. /*++
  2. Copyright (C) 1995-2001 Microsoft Corporation
  3. Module Name:
  4. CFDYN.CPP
  5. Abstract:
  6. Defines the virtual base class for the Dynamic Provider
  7. class factory objects. This code was largly pilfered from
  8. the Brockschmidt samples. The class is always overriden
  9. so that each provider type (DDE, registry, etc.) will have
  10. its own class factory.
  11. History:
  12. a-davj 27-Sep-95 Created.
  13. --*/
  14. #include "precomp.h"
  15. #include "cfdyn.h"
  16. //***************************************************************************
  17. // CCFDyn::CCFDyn
  18. // CCFDyn::~CCFDyn
  19. //
  20. // DESCRIPTION:
  21. //
  22. // Constructor and destructor.
  23. //
  24. //***************************************************************************
  25. CCFDyn::CCFDyn(void)
  26. {
  27. InterlockedIncrement(&lObj);
  28. m_cRef=0L;
  29. return;
  30. }
  31. CCFDyn::~CCFDyn(void)
  32. {
  33. InterlockedDecrement(&lObj);
  34. return;
  35. }
  36. //***************************************************************************
  37. // HRESULT CCFDyn::QueryInterface
  38. // long CCFDyn::AddRef
  39. // long CCFDyn::Release
  40. //
  41. // DESCRIPTION:
  42. //
  43. // Standard Com IUNKNOWN functions.
  44. //
  45. //***************************************************************************
  46. STDMETHODIMP CCFDyn::QueryInterface(
  47. IN REFIID riid,
  48. OUT PPVOID ppv)
  49. {
  50. *ppv=NULL;
  51. if (IID_IUnknown==riid || IID_IClassFactory==riid)
  52. *ppv=this;
  53. if (NULL!=*ppv)
  54. {
  55. ((LPUNKNOWN)*ppv)->AddRef();
  56. return NOERROR;
  57. }
  58. return ResultFromScode(E_NOINTERFACE);
  59. }
  60. STDMETHODIMP_(ULONG) CCFDyn::AddRef(void)
  61. {
  62. return InterlockedIncrement(&m_cRef);
  63. }
  64. STDMETHODIMP_(ULONG) CCFDyn::Release(void)
  65. {
  66. long lRet = InterlockedDecrement(&m_cRef);
  67. if (0L!=lRet)
  68. return lRet;
  69. delete this;
  70. return 0L;
  71. }
  72. //***************************************************************************
  73. // HRESULT CCFDyn::CreateInstance
  74. //
  75. // DESCRIPTION:
  76. //
  77. // Instantiates a provider object returning an interface pointer. Note
  78. // that the CreateImpObj routine is always overriden in order
  79. // to create a particular type of provider.
  80. //
  81. // PARAMETERS:
  82. // pUnkOuter LPUNKNOWN to the controlling IUnknown if we are
  83. // being used in an aggregation.
  84. // riid REFIID identifying the interface the caller
  85. // desires to have for the new object.
  86. // ppvObj PPVOID in which to store the desired
  87. // interface pointer for the new object.
  88. //
  89. // RETURN VALUE:
  90. // HRESULT NOERROR if successful, otherwise E_NOINTERFACE
  91. // if we cannot support the requested interface.
  92. //***************************************************************************
  93. STDMETHODIMP CCFDyn::CreateInstance(
  94. IN LPUNKNOWN pUnkOuter,
  95. IN REFIID riid,
  96. OUT PPVOID ppvObj)
  97. {
  98. IUnknown * pObj;
  99. HRESULT hr;
  100. *ppvObj=NULL;
  101. hr=ResultFromScode(E_OUTOFMEMORY);
  102. //Verify that a controlling unknown asks for IUnknown
  103. if (NULL!=pUnkOuter && IID_IUnknown!=riid)
  104. return ResultFromScode(CLASS_E_NOAGGREGATION);
  105. //Create the object passing function to notify on destruction.
  106. pObj = CreateImpObj();
  107. if (NULL==pObj)
  108. return hr;
  109. hr=pObj->QueryInterface(riid, ppvObj);
  110. //Kill the object if initial creation or Init failed.
  111. if (FAILED(hr))
  112. delete pObj;
  113. else
  114. InterlockedIncrement(&lObj); // dec takes place in the objects destructor
  115. return hr;
  116. }
  117. //***************************************************************************
  118. // HRESULT CCFDyn::LockServer
  119. //
  120. // DESCRIPTION:
  121. // Increments or decrements the lock count of the DLL. If the
  122. // lock count goes to zero and there are no objects, the DLL
  123. // is allowed to unload. See DllCanUnloadNow.
  124. //
  125. // PARAMETERS:
  126. // fLock BOOL specifying whether to increment or
  127. // decrement the lock count.
  128. //
  129. // RETURN VALUE:
  130. // HRESULT NOERROR always.
  131. //***************************************************************************
  132. STDMETHODIMP CCFDyn::LockServer(
  133. IN BOOL fLock)
  134. {
  135. if (fLock)
  136. InterlockedIncrement(&lLock);
  137. else
  138. InterlockedDecrement(&lLock);
  139. return NOERROR;
  140. }