The CComClassFactory Class
The CComClassFactory class is used to implement the IClassFactory interface. This interface allows us to create an object with a specific CLSID. CLSID (Class Identifier) is a unique identifier used to identify a specific type of COM object. This interface also allows us to save the class factory in memory. This lets us create new objects more quickly.
Note that you must implement the IClassFactory class for every class that is registered by you in the system registry and assigned a CLSID.
Syntax
Take a look at the syntax of the CComClassFactory Class.
class CComClassFactory
: public IClassFactory,
public CComObjectRootEx<CComGlobalsThreadModel>

You can also try this code with Online C++ Compiler
Run Code
The ATL library in Windows provides a number of convenient classes and macros for building COM objects, including the CComCoClass class and the DECLARE_CLASSFACTORY macro.
When an ATL object is derived from the CComCoClass, it automatically acquires a class factory, which is used to create and manage instances of the object. The DECLARE_CLASSFACTORY macro, which is included in the CComCoClass class definition, declares CComClassFactory as the default class factory for the object.
However, in some cases, developers may need to override the default class factory and implement a custom class factory.
This can be achieved by using one of the DECLARE_CLASSFACTORYXXX macros in the class definition, which specify a custom class factory implementation.
For example, the DECLARE_CLASSFACTORY_EX macro uses the specified class for the class factory:
Code:
class ATL_NO_VTABLE CMyCustomClass :
public CComObjectRootEx<CComSingleThreadModel>,
public CComCoClass<CMyCustomClass, &CLSID_MyCustomClass>,
public IDispatchImpl<IMyCustomClass, &IID_IMyCustomClass, &LIBID_NVC_ATL_COMLib, /*wMajor =*/ 1, /*wMinor =*/ 0>
{
public:
DECLARE_CLASSFACTORY_EX(CMyClassFactory)
// Class declaration's remaining text is omitted.

You can also try this code with Online C++ Compiler
Run Code
The above code demonstrates a class definition. It states that the CMyClassFactory is to be used as the object’s default class factory.
Apart from this, ATL also provides us with three other macros to declare a class factory.
-
DECLARE_CLASSFACTORY2: It uses the CCOMCLASSFACTORY2. It controls the creation of classes through a licence.
-
DECLARE_CLASSFACTORY_AUTO_THREAD: It uses the CComClassFactoryAutoThread. It allows us to create objects in multiple apartments.
-
DECLARE_CLASSFACTORY_SINGLETON: It uses CComClassFactorySingleton. It is used to construct a single CComObjectGlobal object.
Note that in order to use the CComClassFactory class, you need to provide the atlcom.h file as header.
CComClassFactory: Methods
The CComClassFactory class provides us with two important public methods. Let us understand each one of them in detail.
CComClassFactory::CreateInstance
The method is used to create an instance of a COM object with a particular CLSID. It also provides an interface pointer to the object created.
Syntax
STDMETHOD(CreateInstance)(LPUNKNOWN pUnkOuter, REFIID riid, void** ppvObj);

You can also try this code with Online C++ Compiler
Run Code
The CreateInstance method takes three parameters:
-
pUnkOuter: This is a pointer to the controlling IUnknown interface of an aggregate object. An aggregate object is a composite object that is made up of two or more individual objects, where one object acts as the controlling object and the others act as the contained objects. If the object being created is not part of an aggregate, pUnkOuter should be set to NULL.
-
riid: It is the IID (interface identifier) of the requested interface. The IID must obtain the value IID_IUnknown if the pUnkOuter is not NULL.
- ppvObject: This is a pointer to the pointer of the newly created object. The method sets this pointer to the address of the created object, allowing the client to access the object's interface. If the interface is not supported by the object, then this parameter is set to NULL.
It returns a standard HRESULT value, which is a result code format in COM.
CComClassFactory::LockServer
This method is used to increment and decrement the lock count of the module. It does that by using the _Module::Lock and _Module::Unlock methods, respectively.
Note that here _Module refers to an instance of the CComModule or the classes derived from it.
Syntax
STDMETHOD(LockServer)(BOOL fLock);

You can also try this code with Online C++ Compiler
Run Code
This method takes a single boolean value, fLock, as a parameter. If lock count gets incremented if this value is true, and decremented otherwise.
It also returns a standard HRESULT value.
Frequently Asked Questions
What is the Active Template Library (ATL)?
The Active Template Library (ATL) is a set of C++ templates and macros provided by Microsoft to simplify the development of Component Object Model (COM) components and Windows software. It provides a framework for creating and using COM objects, handling memory management, and simplifying the implementation of common COM features.
What is CComClassFactory?
CComClassFactory is a class in the Active Template Library (ATL) in Windows. It is used to implement a class factory for a COM class. A class factory is an object that creates instances of a specific COM class.
What is Component Object Model (COM)?
Component Object Model (COM) is a Microsoft technology that enables software components to interact with each other in a flexible, platform-independent manner. It provides a way to define objects, their interfaces, and their behavior, and to create and use those objects in different applications and languages.
Conclusion
In this article, we discussed about the CComClassFactory. We understood how it helps us to create instances of COM objects. We learned about its two methods and understood their working.
We hope you enjoyed reading this article. If you wish to learn more about ATL class, refer to the following blogs.
Visit our website to read more such blogs. Make sure you enroll in our other courses as well. You can take mock tests, solve problems, and interview puzzles. Also, you can check out some exciting interview stuff- interview experiences and an interview bundle for placement preparations. Do upvote our blog to help fellow ninjas grow.
Keep Grinding! 🦾
Happy Coding! 💻