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