CComClassFactoryAutoThread 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 CComClassFactoryAutoThread Class.

This class implements the IClassFactory interface, and allows objects to be created in multiple apartments.

System_CAPS_ICON_important.jpg Important

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

class CComClassFactoryAutoThread : public IClassFactory,
public CComObjectRootEx<CComGlobalsThreadModel>

Public Methods

NameDescription
CComClassFactoryAutoThread::CreateInstanceCreates an object of the specified CLSID.
CComClassFactoryAutoThread::LockServerLocks the class factory in memory.

CComClassFactoryAutoThread is similar to CComClassFactory, but allows objects to be created in multiple apartments. To take advantage of this support, derive your EXE module from CComAutoThreadModule.

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 CComClassFactoryAutoThread, specify the DECLARE_CLASSFACTORY_AUTO_THREAD macro in your object's class definition. For example:

class ATL_NO_VTABLE CMyAutoClass :
   public CComObjectRootEx<CComMultiThreadModel>,
   public CComCoClass<CMyAutoClass, &CLSID_MyAutoClass>,
   public IMyAutoClass
{
public:
   DECLARE_CLASSFACTORY_AUTO_THREAD()

   // Remainder of class declaration omitted.

CComObjectRootBase

CComObjectRootEx

IClassFactory

CComClassFactoryAutoThread

Header: atlcom.h

Creates an object of the specified CLSID and retrieves an interface pointer to this object.

STDMETHODIMP 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.

Remarks

If your module derives from CComAutoThreadModule, CreateInstance first selects a thread to create the object in the associated apartment.

Increments and decrements the module lock count by calling _Module::Lock and _Module::Unlock, respectively.

STDMETHODIMP LockServer(BOOL fLock);

Parameters

fLock
[in] If TRUE, the lock count is incremented; otherwise, the lock count is decremented.

Return Value

A standard HRESULT value.

Remarks

When using CComClassFactoryAutoThread, _Module typically refers to the global instance of CComAutoThreadModule.

Calling LockServer allows a client to hold onto a class factory so that multiple objects can be quickly created.

IClassFactory
CComClassFactory2 Class
CComClassFactorySingleton Class
CComObjectRootEx Class
CComGlobalsThreadModel
Class Overview

Show: