CComClassFactorySingleton Class

 

The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.

The latest version of this topic can be found at CComClassFactorySingleton Class.

This class derives from CComClassFactory and uses CComObjectGlobal to construct a single object.

Important

This class and its members cannot be used in applications that execute in the Windows Runtime.

Syntax

template<class T>
class CComClassFactorySingleton : public CComClassFactory

Parameters

T
Your class.

CComClassFactorySingleton derives from CComClassFactory and uses CComObjectGlobal to construct a single object. Each call to the CreateInstance method simply queries this object for an interface pointer.

Members

Public Methods

Name Description
CComClassFactorySingleton::CreateInstance Queries m_spObj for an interface pointer.

Public Data Members

Name Description
CComClassFactorySingleton::m_spObj The CComObjectGlobal object constructed by CComClassFactorySingleton.

Remarks

ATL objects normally acquire a class factory by deriving from CComCoClass. This class includes the macro DECLARE_CLASSFACTORY, which declares CComClassFactory as the default class factory. To use CComClassFactorySingleton, specify the DECLARE_CLASSFACTORY_SINGLETON macro in your object's class definition. For example:

class ATL_NO_VTABLE CMySingletonClass :
   public CComObjectRootEx<CComSingleThreadModel>,
   public CComCoClass<CMySingletonClass, &CLSID_MySingletonClass>,
   public IMySingletonClass
{
public:
   DECLARE_CLASSFACTORY_SINGLETON(CMySingletonClass)

   // Remainder of class declaration omitted.

Inheritance Hierarchy

CComObjectRootBase

CComObjectRootEx

IClassFactory

CComClassFactory

CComClassFactorySingleton

Requirements

Header: atlcom.h

CComClassFactorySingleton::CreateInstance

Calls QueryInterface through m_spObj to retrieve an interface pointer.

STDMETHOD(CreateInstance)(LPUNKNOWN pUnkOuter, REFIID riid, void** ppvObj);

Parameters

pUnkOuter
[in] If the object is being created as part of an aggregate, then pUnkOuter must be the outer unknown. Otherwise, pUnkOuter must be NULL.

riid
[in] The IID of the requested interface. If pUnkOuter is non- NULL, riid must be IID_IUnknown.

ppvObj
[out] A pointer to the interface pointer identified by riid. If the object does not support this interface, ppvObj is set to NULL.

Return Value

A standard HRESULT value.

CComClassFactorySingleton::m_spObj

The CComObjectGlobal object constructed by CComClassFactorySingleton.

CComPtr<IUnknown> m_spObj;

Remarks

Each call to the CreateInstance method simply queries this object for an interface pointer.

Note that the current form of m_spObj presents a breaking change from the way that CComClassFactorySingleton worked in previous versions of ATL. In previous versions the CComClassFactorySingleton object was created at the same time as the class factory, during server initialization. In Visual C++.NET 2003, the object is created lazily, on the first request. This change could cause errors in programs that rely on early initialization.

See Also

IClassFactory
CComClassFactory2 Class
CComClassFactoryAutoThread Class
CComObjectRootEx Class
CComGlobalsThreadModel
Class Overview