Compartir a través de


CComClassFactoryAutoThread Class

 

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.

Important

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

Syntax

class CComClassFactoryAutoThread : public IClassFactory,
public CComObjectRootEx<CComGlobalsThreadModel>

Members

Public Methods

Name Description
CComClassFactoryAutoThread::CreateInstance Creates an object of the specified CLSID.
CComClassFactoryAutoThread::LockServer Locks the class factory in memory.

Remarks

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.

Inheritance Hierarchy

CComObjectRootBase

CComObjectRootEx

IClassFactory

CComClassFactoryAutoThread

Requirements

Header: atlcom.h

CComClassFactoryAutoThread::CreateInstance

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.

CComClassFactoryAutoThread::LockServer

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.

See Also

IClassFactory
CComClassFactory2 Class
CComClassFactorySingleton Class
CComObjectRootEx Class
CComGlobalsThreadModel
Class Overview