Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Have you worked with C++ programming language? Do you want to learn something new in C++?
Then you have come to the right place. This article is focused on the Component Object Model, i.e., COM. COM is a technique for exchanging binary code between programs and languages. It is unlike the C++ method, which encourages source code reuse. ATL is the ideal example of this. Reusing code at the source level is effective, but only in C++. Along with the possibility of name collisions, it increases bloat in your project. This bloat is caused because of having several copies of the same code.
Let's dive into the article to know more.
Introduction to COM
COM stands for Component Object Model. It is the famous TLA (three-letter acronym) that is present in Windows everywhere now. Various technologies are coming based on COM. COM is based on an "object model". ActiveX Controls and OLE are also based on this model. Using COM, a component can expose its capabilities to host apps and other components. It specifies how the object exposes itself. It also determines how this exposure operates across networks and processes. The life cycle of an object is also defined by COM.
The following terminology tells about the fundamental of COM:-
Interfaces - the process by which an object makes its functionality visible.
IUnknown - it is the basic interface upon which all others are dependent and built. It implements the interface querying mechanisms and the reference counting running through COM.
Reference counting - the process through which an object (or an interface) determines when it is no longer required and can thus be removed.
QueryInterface - this method is used to look up an object using a given interface.
Marshaling - this method enables objects to be used across process, thread, and network boundaries. It is allowed for location independence.
Aggregation - a method via which one object can use another.
Interfaces (ATL)
An object exposes its capabilities to the outside world through an interface. An interface in COM is a table of pointers to functions that the object has implemented. It is similar to a C++ vtable. The functions that the table points to are the interface's methods. Also, the table itself is the interface. An object is free to expose any number of interfaces.
Every interface is based on IUnknown, which is a fundamental COM interface. The IUnknown method's navigation to additional interfaces by the object is possible. A unique interface ID is also assigned to each interface (IID). It is simple to support interface versioning because of this uniqueness. An interface is when updated, then it has a new interface and an updated IID.
IUnknown
Every COM interface's primary interface is IUnknown. The three methods, QueryInterface, AddRef, and Release, are defined by this interface. QueryInterface enables an interface user to request a pointer to one of its other interfaces. Reference counting is implemented by AddRef and Release on the interface.
Reference Counting
COM does not remove the object from memory automatically when the object is no longer in use. Instead, the unused object must be deleted by the object programmer. The programmer decides the removal of objects based on a reference count.
COM uses the AddRef and Release methods of IUnknown. It is used to manage the reference count of object interfaces. There are some basic rules for calling these methods. They are as follows:
The client must call AddRef on the interface whenever they receive an interface pointer.
The client must call Release whenever they finish using the interface pointer.
In a basic implementation, a counter variable inside the object is incremented by each AddRef call and decremented by each Release call. The interface can delete itself from memory once the count reaches zero and there are no more users.
QueryInterface
The primary COM mechanism is to use the IUnknown method, QueryInterface. There are other ways for an object to express the functionality statically (before it is instantiated).
Every interface has a QueryInterface implementation. It is so because they are all derived from IUnknown. This function uses the IID(interface ID) of the interface. The caller needs a pointer to query an object, regardless of implementation. If the object is compatible with that interface, QueryInterface calls AddRef while retrieving a pointer to the interface. If not, the E_NOINTERFACE error code is returned.
Marshaling
Interfaces provided by an object in one process can be used in another process with the marshaling COM technique. When a method's parameters are marshaled, COM offers code to pack the parameters into a format. This format can be transferred between processes (and across the wire to processes operating on other machines). It unpacks the parameters at the other end. The same procedures must be followed by COM on the call's return.
Aggregation
There are situations when the implementor of an object wants to use the features provided by another prebuilt object. It also wants this second object to look like a natural extension of the first. Both of these objectives are accomplished by COM through containment and aggregation.
Aggregation refers to forming the contained (inner) object by the containing (outer) object and the outer object's exposure to the inner object's interfaces. An object can decide whether or not it wants to be aggregated. If so, it must adhere to specific constraints for aggregation to function effectively. The containing object must be the primary delegate for all IUnknown method calls on it.
Frequently Asked Questions
What is ATL in programming?
The Active Template Library (ATL) is a set of template-based C++ classes. It allows you to create small and fast Component Object Model (COM) objects.
What is the class in C++?
In C++, a class is a user-defined type or data structure that includes data and functions as members. Its access is controlled by the three access specifiers: private, protected, and public.
What is COM class?
A COM class implements a group of interfaces in code executed whenever you interact with a given object.
What are COM applications?
A COM application is the primary unit of administration and security for Component Services. It consists of a group of COM components that perform related functions.
What is the use of COM?
COM is a platform-independent, object-oriented, and distributed system. It is used for creating interactive binary software components. It is the foundation technology for Microsoft's ActiveX and OLE technologies.
Conclusion
This complete article has helped us to learn about all the concepts and steps for COM. We have learned an overview of COM, ATL, IUnknown, Reference Counting, and many more things in detail.