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.

179 lines
4.2 KiB

  1. /*****************************************************************************\
  2. FILE: classfactory.cpp
  3. DESCRIPTION:
  4. This file will be the Class Factory.
  5. BryanSt 8/12/1999
  6. Copyright (C) Microsoft Corp 1999-1999. All rights reserved.
  7. \*****************************************************************************/
  8. #include "priv.h"
  9. #include "autodiscovery.h"
  10. #include "objcache.h"
  11. #include "mailbox.h"
  12. /*****************************************************************************
  13. *
  14. * CClassFactory
  15. *
  16. *
  17. *****************************************************************************/
  18. class CClassFactory : public IClassFactory
  19. {
  20. public:
  21. //////////////////////////////////////////////////////
  22. // Public Interfaces
  23. //////////////////////////////////////////////////////
  24. // *** IUnknown ***
  25. virtual STDMETHODIMP_(ULONG) AddRef(void);
  26. virtual STDMETHODIMP_(ULONG) Release(void);
  27. virtual STDMETHODIMP QueryInterface(REFIID riid, LPVOID * ppvObj);
  28. // *** IClassFactory ***
  29. virtual STDMETHODIMP CreateInstance(IUnknown *pUnkOuter, REFIID riid, void **ppvObject);
  30. virtual STDMETHODIMP LockServer(BOOL fLock);
  31. public:
  32. CClassFactory(REFCLSID rclsid);
  33. ~CClassFactory(void);
  34. // Friend Functions
  35. friend HRESULT CClassFactory_Create(REFCLSID rclsid, REFIID riid, LPVOID * ppvObj);
  36. protected:
  37. int m_cRef;
  38. CLSID m_rclsid;
  39. };
  40. /*****************************************************************************
  41. * IClassFactory::CreateInstance
  42. *****************************************************************************/
  43. HRESULT CClassFactory::CreateInstance(IUnknown * punkOuter, REFIID riid, LPVOID * ppvObj)
  44. {
  45. HRESULT hr = E_INVALIDARG;
  46. if (NULL != ppvObj)
  47. {
  48. if (!punkOuter)
  49. {
  50. hr = E_NOINTERFACE;
  51. }
  52. else
  53. { // Does anybody support aggregation any more?
  54. hr = ResultFromScode(CLASS_E_NOAGGREGATION);
  55. }
  56. }
  57. return hr;
  58. }
  59. /*****************************************************************************
  60. *
  61. * IClassFactory::LockServer
  62. *
  63. * Locking the server is identical to
  64. * creating an object and not releasing it until you want to unlock
  65. * the server.
  66. *
  67. *****************************************************************************/
  68. HRESULT CClassFactory::LockServer(BOOL fLock)
  69. {
  70. if (fLock)
  71. DllAddRef();
  72. else
  73. DllRelease();
  74. return S_OK;
  75. }
  76. /*****************************************************************************
  77. *
  78. * CClassFactory_Create
  79. *
  80. *****************************************************************************/
  81. /****************************************************\
  82. Constructor
  83. \****************************************************/
  84. CClassFactory::CClassFactory(REFCLSID rclsid) : m_cRef(1)
  85. {
  86. m_rclsid = rclsid;
  87. DllAddRef();
  88. }
  89. /****************************************************\
  90. Destructor
  91. \****************************************************/
  92. CClassFactory::~CClassFactory()
  93. {
  94. DllRelease();
  95. }
  96. //===========================
  97. // *** IUnknown Interface ***
  98. //===========================
  99. ULONG CClassFactory::AddRef()
  100. {
  101. m_cRef++;
  102. return m_cRef;
  103. }
  104. ULONG CClassFactory::Release()
  105. {
  106. ASSERT(m_cRef > 0);
  107. m_cRef--;
  108. if (m_cRef > 0)
  109. return m_cRef;
  110. delete this;
  111. return 0;
  112. }
  113. HRESULT CClassFactory::QueryInterface(REFIID riid, void **ppvObj)
  114. {
  115. if (IsEqualCLSID(riid, IID_IUnknown) || IsEqualCLSID(riid, IID_IClassFactory))
  116. {
  117. *ppvObj = SAFECAST(this, IClassFactory *);
  118. }
  119. else
  120. {
  121. TraceMsg(TF_WMOTHER, "CClassFactory::QueryInterface() failed.");
  122. *ppvObj = NULL;
  123. return E_NOINTERFACE;
  124. }
  125. AddRef();
  126. return S_OK;
  127. }
  128. HRESULT CClassFactory_Create(REFCLSID rclsid, REFIID riid, LPVOID * ppvObj)
  129. {
  130. HRESULT hres;
  131. if (IsEqualCLSID(riid, IID_IClassFactory))
  132. {
  133. *ppvObj = (LPVOID) new CClassFactory(rclsid);
  134. hres = (*ppvObj) ? S_OK : E_OUTOFMEMORY;
  135. }
  136. else
  137. hres = ResultFromScode(E_NOINTERFACE);
  138. return hres;
  139. }