PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/Visual Studio 2008/CppExeCOMServer/ClassFactory.cpp

#
C++ | 133 lines | 68 code | 20 blank | 45 comment | 9 complexity | 7f24fa631eaffa309839e71cccb0e97f MD5 | raw file
  1. /****************************** Module Header ******************************\
  2. Module Name: ClassFactory.cpp
  3. Project: CppExeCOMServer
  4. Copyright (c) Microsoft Corporation.
  5. The file implements the class factory for the SimpleObject COM class. A class
  6. factory (aka a class object) is a component whose main purpose is to create
  7. other components. It provides many controls over the creation process of the
  8. component. When a client uses a CLSID to request the creation of an object
  9. instance, the first step is creation of a class factory, an intermediate
  10. object that contains an implementation of the methods of the IClassFactory
  11. interface. While COM provides several instance creation functions, the first
  12. step in the implementation of these functions is always the creation of a
  13. class factory. For example, CoCreateInstance provides a wrapper over a
  14. CoGetClassObject and CreateInstance method of IClassFactory interface.
  15. CoCreateInstance internally creates class factory for the specified CLSID,
  16. gets the IClassFactory interface pointer, and then creates the component by
  17. calling CreateInstance on IClassFactory interface pointer and then returns
  18. the requested interface pointer to the client by calling QueryInterface
  19. method in the CreateInstance method of IClassFactory.
  20. The class factory implements the IClassFactory interface. The class factory
  21. object must implement these two methods of IClassFactory to support the
  22. object creation.
  23. IFACEMETHODIMP CreateInstance(IUnknown *pUnkOuter, REFIID riid, void **ppv);
  24. IFACEMETHODIMP LockServer(BOOL fLock);
  25. This source is subject to the Microsoft Public License.
  26. See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
  27. All other rights reserved.
  28. THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
  29. EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
  30. WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
  31. \***************************************************************************/
  32. #include "ClassFactory.h"
  33. #include "SimpleObject.h"
  34. extern long g_cServerLocks;
  35. ClassFactory::ClassFactory() : m_cRef(1)
  36. {
  37. InterlockedIncrement(&g_cServerLocks);
  38. }
  39. ClassFactory::~ClassFactory()
  40. {
  41. InterlockedDecrement(&g_cServerLocks);
  42. }
  43. //
  44. // IUnknown
  45. //
  46. IFACEMETHODIMP ClassFactory::QueryInterface(REFIID riid, void **ppv)
  47. {
  48. HRESULT hr = S_OK;
  49. if (IsEqualIID(IID_IUnknown, riid) ||
  50. IsEqualIID(IID_IDispatch, riid) || // For implementing IDispatch
  51. IsEqualIID(IID_IClassFactory, riid))
  52. {
  53. *ppv = static_cast<IUnknown *>(this);
  54. AddRef();
  55. }
  56. else
  57. {
  58. hr = E_NOINTERFACE;
  59. *ppv = NULL;
  60. }
  61. return hr;
  62. }
  63. IFACEMETHODIMP_(ULONG) ClassFactory::AddRef()
  64. {
  65. return InterlockedIncrement(&m_cRef);
  66. }
  67. IFACEMETHODIMP_(ULONG) ClassFactory::Release()
  68. {
  69. ULONG cRef = InterlockedDecrement(&m_cRef);
  70. if (0 == cRef)
  71. {
  72. delete this;
  73. }
  74. return cRef;
  75. }
  76. //
  77. // IClassFactory
  78. //
  79. IFACEMETHODIMP ClassFactory::CreateInstance(IUnknown *pUnkOuter, REFIID riid, void **ppv)
  80. {
  81. HRESULT hr = CLASS_E_NOAGGREGATION;
  82. // pUnkOuter is used for aggregation. We do not support it in the sample.
  83. if (pUnkOuter == NULL)
  84. {
  85. hr = E_OUTOFMEMORY;
  86. // Create the COM component.
  87. SimpleObject *pSimpleObj = new SimpleObject();
  88. if (pSimpleObj)
  89. {
  90. // Query the specified interface.
  91. hr = pSimpleObj->QueryInterface(riid, ppv);
  92. pSimpleObj->Release();
  93. }
  94. }
  95. return hr;
  96. }
  97. IFACEMETHODIMP ClassFactory::LockServer(BOOL fLock)
  98. {
  99. if (fLock)
  100. {
  101. InterlockedIncrement(&g_cServerLocks);
  102. }
  103. else
  104. {
  105. InterlockedDecrement(&g_cServerLocks);
  106. }
  107. return S_OK;
  108. }