Basic COM  «Prev  Next»
Lesson 3COM terminology and objects
ObjectiveDescribe COM terminology and COM objects.

COM Terminology and Objects

COM is an object-based technology in that it specifies the behavior of objects and how to integrate (i.e. use) objects. Unlike C++, COM does not require users of an object to know the internal layout of the object. A COM object can be thought of as a container for one or more COM interfaces. COM objects are implemented within a COM server. To denote a COM object and its interfaces, we use a box with connected interface diagrams:
COM object consisting of 1) IUnknown and 2) Three interfaces
COM object consisting of 1) IUnknown and 2) Three interfaces

A COM server is a Windows DLL or EXE that houses COM objects.

Class Objects and Class Factories

COM objects are created by another type of object, a Class object. Class objects are implemented within a COM server. The most common type of class object is a class factory. A class factory is a class object that implements an interface called IClassFactory. IClassFactory defines a method that creates an instance of a COM object.
To create a COM object, a client asks COM for the class factory that knows how to create a specific COM object. In response, COM asks the COM server for the requested class factory. The server returns a pointer to the class factory's IClassFactory interface to COM, and COM returns it to the client. Using methods in IClassFactory, the client creates instances of the COM object

Who creates COM Objects and apartments?

COM objects are actually created in the context of a COM apartment. A COM apartment defines the threading context of an executing COM object. A single-threaded apartment (STA) allows only one thread to access interface methods within the object. This is always the same thread. A multithreaded apartment (MTA) allows multiple threads to access interface methods within the object. This access can, and often does, occur simultaneously. For example, thread A and thread B can simultaneously access the same or different methods within the object.
Unlike COM interfaces and objects, which are visible in our code via instances of C++ classes, (usually) there is no apartment object. Instead, the type of apartment dictates how we code our COM interface methods. For example, in an STA, because only one specific thread can access an instance of a COM object, no special coding is required to guard data and methods. In an MTA, data and methods are often guarded using Win32 synchronization objects to protect the integrity of the object. See the Microsoft Platform SDK documentation for details.


We need one dedicated class factory for each type of COM object supported, for example, if a server implements COM objects A and B, it must implement two class factories: one for A and one for B.
A COM server consisting of 1) Two Class Factories and 2) Two Com Objects
A COM server consisting of 1) Two Class Factories and 2) Two Com Objects
The image contains the following text:
  1. Title: "COM Server"
    • Box 1: "Class Factory for ComObj1"
    • Box 2: "Class Factory for ComObj2"
    • Object 1: "ComObj1"
    • Object 2: "ComObj2"
    • Description: "Every type of COM object within a server must have an associated Class Factory. A Class Factory creates one or more instances of its associated COM object."
  2. Features of the Image:
    • COM Server Structure: The image illustrates a simplified structure of a COM server, showing the relationship between class factories and COM objects.
    • Class Factories: There are two class factories represented in the image:
      • One for `ComObj1`
      • One for `ComObj2`
    • COM Objects: The image shows two COM objects:
      • `ComObj1`
      • `ComObj2`
    • Arrows: There are arrows pointing from the class factories to the corresponding COM objects (`ComObj1` and `ComObj2`), indicating that each class factory is responsible for creating instances of a specific COM object.
    • Description Box: The description emphasizes the role of a class factory in the COM architecture, explaining that every type of COM object within a server must have an associated class factory, and that the class factory is responsible for creating instances of its associated COM object.

Summary: The image visually explains the concept of a class factory in the COM (Component Object Model) architecture. It highlights how each COM object type within a server has an associated class factory, which is responsible for creating instances of that object. This is a fundamental part of the COM system, enabling the dynamic creation of objects based on client requests.

Even though COM has the notion of objects, all interactions between the client and server are through interfaces. This means the only way to talk to a COM object is through its interfaces. This also means that COM objects do not directly expose data members.

COM Programming with Microsoft .NET

Com Basics - Quiz

Click here to check your understanding of COM basics.
COM Basics - Quiz

SEMrush Software