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.

COM Programming with Microsoft .NET
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

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 Basics - Quiz

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