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.

189 lines
4.6 KiB

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