Introduction to COM
The Component Object Model (COM) is a binary-interface standard for software components introduced by Microsoft. It is used to create reusable, attachable software components. COM is the foundation for many Microsoft technologies, including OLE, ActiveX, and COM+.
COM provides a language-independent, processor-independent, and operating-system-independent mechanism for creating extensible and interoperable software components. Key features include:
- Object-Oriented Design: COM objects encapsulate data and behavior.
- Interface-Based Programming: Clients interact with objects through interfaces, not concrete classes.
- Late Binding: Objects can be created and manipulated at runtime without compile-time knowledge of their specific type.
- Language Independence: Components can be written in various languages (C++, VB, Delphi, etc.) and interoperate.
Learn more about the COM architecture and its core principles.
COM Interfaces
An interface in COM is a contract that defines a set of related functions (methods) that an object must implement. Interfaces are defined using the Interface Definition Language (IDL).
Key characteristics of COM interfaces:
- IUnknown: The root interface for all COM interfaces, providing reference counting and interface querying capabilities.
- GUIDs (Globally Unique Identifiers): Each interface has a unique GUID to prevent naming conflicts.
- Vtables (Virtual Function Tables): Interfaces are typically implemented using vtables, enabling efficient method dispatch.
Example of a simple COM interface definition in IDL:
[
uuid(YOUR_INTERFACE_GUID),
version(1.0),
object,
pointer_default(unique)
]
interface IMyInterface : IUnknown
{
HRESULT SayHello([in] int value, [out] int* result);
HRESULT AddToList([in, string] const wchar_t* item);
}
Explore different COM interface types and best practices.
COM Objects
A COM object is an instance of a COM class that implements one or more COM interfaces. COM objects are responsible for managing their own state and behavior.
Key aspects of COM objects:
- Reference Counting: Managed by the
AddRef
andRelease
methods ofIUnknown
to control object lifetime. - Aggregation: A mechanism for COM objects to reuse the functionality of another COM object.
- Composition: A more flexible way for objects to contain other objects.
Understand the lifecycle of COM objects and memory management.
COM Registration
For a COM object to be discoverable and creatable by clients, it must be registered with the operating system. This typically involves writing information to the Windows Registry.
Registration data includes:
- CLSID (Class Identifier): A unique GUID for each COM class.
- ProgID (Programmatic Identifier): A human-readable name for the COM class.
- InprocServer32 / LocalServer32: Registry keys pointing to the COM component's DLL or EXE.
Tools like regsvr32.exe
are used to register and unregister COM DLLs.
COM Threading Models
COM defines various threading models that determine how COM objects handle concurrency and synchronize access to their data.
Common threading models include:
- Single-Threaded Apartment (STA): Objects receive messages on a thread that belongs to a single-threaded apartment.
- Multi-Threaded Apartment (MTA): Objects can receive messages on any thread in a multi-threaded apartment.
- Both: Objects can exist in either STA or MTA.
Properly understanding and implementing threading models is crucial for building robust COM applications.
COM Marshalling
Marshalling is the process of packaging interface pointers and data so they can be passed across apartment boundaries or even across process boundaries (Distributed COM - DCOM).
Key concepts:
- Proxy/Stub: COM uses proxy objects on the client side and stub objects on the server side to handle marshaling.
- COM+ Services: COM+ provides built-in marshalling for distributed scenarios.
COM Activation
COM activation refers to the process of creating an instance of a COM object. This is typically done using functions like CoCreateInstance
or CoCreateInstanceEx
, which use the registry information to locate and instantiate the requested object.
Active Template Library (ATL)
The Active Template Library (ATL) is a collection of C++ templates that simplify the development of COM components. ATL provides classes for managing COM object creation, interface implementation, reference counting, and more.
Learn more about developing COM with ATL.
COM+
COM+ is an evolution of COM that adds services such as transaction management, security, object pooling, and publish/subscribe messaging. It provides a richer programming model for building enterprise-level distributed applications.