COM provides a specification and a set of services for supplying client-server functionality through method (function) calls.
A COM method follows a set calling procedure and uses a specific return type. COM methods are grouped together by placing them into a COM interface. A COM interface contains a group of related functions. In COM, we use a straight line with a circle at the end to denote an interface:
Interface navigation
COM requires all interfaces to implement specific functionality that supports Interface navigation and Lifetime management/reference counting.
This requirement is expressed by saying that every COM interface implements IUnknown.
IUnknown is an interface specification (not an implementation) that defines functions to support finding other interfaces and lifetime management. We will examine IUnknown in later lessons.
COM is a specification. It means it is available in the form of documentation and can be printed and read. One can download and read the complete COM specification from the official website of Microsoft Available: (https://www. microsoft.com/com). This specification also includes lot of system code implemented by Microsoft.
Define the semantics of an interface within the context of Microsoft's "Component Object Model"?
In the context of Microsoft's Component Object Model (COM), an **interface** is a contract that defines a set of methods that a COM object must implement. The semantics of an interface in COM can be understood through several key points:
Contractual Nature
An interface in COM is a strict contract between the client (the code that uses the COM object) and the server (the COM object that implements the interface). The contract is defined by the methods that the interface declares. Once defined, the interface cannot be changed; it remains constant throughout its lifetime.
Interface Identifier (IID): Each COM interface is uniquely identified by a globally unique identifier (GUID) known as an Interface Identifier (IID). This IID is used by clients to request and interact with specific interfaces on a COM object.
Pure Abstract Class:
In C++ terms, a COM interface is typically represented as a pure abstract class, meaning it contains only pure virtual methods and no data members. All the methods in a COM interface are virtual and must be implemented by the COM object.
Multiple Interfaces: A single COM object can implement multiple interfaces. Each interface provides different functionalities or views of the object. Clients can query an object for the interfaces it supports using the `QueryInterface` method.
Interface Inheritance: COM supports interface inheritance, allowing a new interface to be defined based on an existing one. The derived interface inherits all the methods of the base interface and can add new methods. This allows for the extension of functionality while maintaining backward compatibility.
Binary Standard: COM interfaces are defined at the binary level, which means that any language that can follow the COM binary standard can implement or use COM interfaces. This makes COM language-independent, allowing components written in different programming languages to interact.
Reference Counting: Interfaces in COM use reference counting to manage the lifetime of an object. This is typically done through the `AddRef` and `Release` methods. When a client acquires a pointer to an interface, it calls `AddRef` to increment the reference count. When the client is done with the interface, it calls `Release`, which decrements the reference count. When the reference count drops to zero, the object can be destroyed.
Interface Methods: The methods declared in an interface are purely abstract, meaning the interface does not provide any implementation. The COM object that implements the interface must provide the concrete implementation of these methods. Each method in an interface returns an `HRESULT` value to indicate success or failure, and may take additional parameters as required by the interface's functionality.
Versioning: Once an interface is published, it cannot be modified. If changes or extensions are needed, a new interface with a new IID is created. This ensures that existing clients that rely on the old interface continue to function correctly.
Example: `IUnknown` Interface
The `IUnknown` interface is the foundational interface in COM, from which all other interfaces must inherit, either directly or indirectly. It defines three essential methods: `QueryInterface`, `AddRef`, and `Release`. These methods are used for interface querying and reference counting, forming the basis of COM's interface-based architecture.
Summary
In COM, an interface defines a set of functionalities that a COM object must implement. It acts as a contract, ensuring that the object can be used in a consistent and predictable manner by clients. The interface is identified by a unique IID and enforces a binary standard, allowing for language-independent interaction between components. Interface-based programming in COM is fundamental to building modular, reusable, and interoperable software components.