You are on page 1of 4

What is COM?

i. COM defines a standard for component interoperability that is not dependent on any particular programming language,
available on multiple platforms, & is extensible. It is an object model at the operating-system level.
ii. COM is used by developers to create re-usable software components, link components together to build applications, & take
advantage of Windows services.
iii. The family of COM technologies includes COM+, Distributed COM (DCOM) & ActiveX Controls.
iv. Microsoft provides COM interfaces for many Windows application programming interfaces such as Direct Show, Media
Foundation, Packaging API, Windows Animation Manager, Windows Portable Devices, & Microsoft Active Directory (AD).
v. In fact, COM promotes the concept of component-based development, which lets you subdivide large applications into
smaller pieces that you can maintain & deploy more easily than you can a monolithic application
vi. COM objects can be created with a variety of programming languages thus providing developers the ability to reuse code.
Object-oriented languages, such as C++, provide programming mechanisms that simplify the implementation of COM objects.
vii. You can use an OOP tool such as Microsoft Visual C++ to construct a set of objects. Other Visual C++ developers can use &
further extend these objects. However, if you package your objects in an COM component, they can be used & further
extended with any programming tool that supports COM technology, such as Visual Basic.
viii. A client application or component object does not care what language a COM object was written in, only that how it can
communicate with the object. This allows application & component objects to communicate with each other regardless of the
language or development tool in which they were created.
ix. For example, if you want to use the Microsoft Word spell checker in a custom application, you can use Microsoft Word
software components, called objects, to provide spell check functionality to your application. Users of your application do not
have to do anything in order for the Microsoft Word spell checker to execute; this is done automatically through your Visual
Basic code.
x. Types of COM Components
(a) In-process servers (DLL)
The simplest type of COM component is a DLL that executes in the same address space as the application that's using it.
Because each process under 32-bit platforms has its own address space, each one works with a distinct instance of the
component.
These components communicate directly with their clients, without the aid of COM, which makes them the most
appropriate choice when speed really matters
ActiveX controls are a special category of in-process components that can be hosted by an ActiveX container, such as a
Visual Basic form or Internet Explorer.
To qualify as an ActiveX control, a component must implement a number of interfaces defined by the ActiveX
specifications.
(b) Local out-of-process servers (EXE)
You can also compile an ActiveX component as an EXE program. This is convenient when you want to create an
application that can work as a stand-alone program & offer programmable objects to the outside at the same time.
The best examples of such servers are the applications in the Microsoft Office suite: You can use Excel or Word either as
independent applications or as providers of components that you can use from within your own programs.
(c) Remote out-of-process servers (EXE)
Remote servers are EXE programs that run on a machine different from the one that is running the client application.
The client & the server communicate through DCOM (or Remote Automation).
xi. There are many reasons to use COM components in an application, including:
Reusability - Once you create a COM component, other developers can use it. This enables easy access to your
component's features in other applications without requiring developers to write extensive code.
Reduced complexity - You can create a COM component to hide programming complexity from other programmers.
Other programmers need only know what information to provide to your component, & what information to retrieve.
Easier updating - Components make it easier for you to revise & update your applications. For example, you can create
a COM component that encapsulates business rules. If the business rules change, you update just the component, not
all the applications that use the component.

Common Object Model (COM)


i. The Component Object Model (COM) is a binary standard that defines how objects are created and destroyed and, most
importantly, how they interact with each other. As long as applications follow the COM standard, different applications from
different sources can communicate with each other across process boundaries. People use COM to make communication with
other applications easy.
ii. Because COM is a binary standard, it is language independent. You do not have to use C++ to implement COM. You can use
any language that supports tables of function pointers.
iii. It is an object model at the operating-system level.

iv. COM is the fundamental "object model" on which ActiveX Controls and OLE are built. It allows an object to expose its
functionality to other components and to host applications. It defines both how the object exposes itself and how this
exposure works across processes and across networks. COM also defines the object's life cycle.
Fundamental to COM are these concepts:
v. Interfaces
An interface is the way in which an object exposes its functionality to the outside world. In COM, an interface is a table of
pointers (like a C++ vtable) to functions implemented by the object.
The table represents the interface, and the functions to which it points are the methods of that interface. An object can
expose as many interfaces as it chooses.
Each interface is based on the fundamental COM interface, IUnknown. The methods of IUnknown allow navigation to other
interfaces exposed by the object.
Also, each interface is given a unique interface ID (IID). This uniqueness makes it is easy to support interface versioning. A
new version of an interface is simply a new interface, with a new IID.
vi. IUnknown interface
IUnknaown is the base interface of every other COM interface.
IUnknown defines three methods: QueryInterface, AddRef, and Release
vii. Reference Counting
It is the technique by which an object (or, strictly, an interface) decides when it is no longer being used and is therefore free
to remove itself.
COM itself does not automatically try to remove an object from memory when it thinks the object is no longer being used.
Instead, the object programmer must remove the unused object. The programmer determines whether an object can be
removed based on a reference count.
COM uses the IUnknown methods, AddRef and Release, to manage the reference count of interfaces on an object. The
general rules for calling these methods are:
Whenever a client receives an interface pointer, AddRef must be called on the interface.
Whenever the client has finished using the interface pointer, it must call Release.
In a simple implementation, each AddRef call increments and each Release call decrements a counter variable inside the
object. When the count returns to zero, the interface no longer has any users and is free to remove itself from memory.
viii. QueryInterface
It is the method used to query an object for a given interface.
Although there are mechanisms by which an object can express the functionality it provides statically (before it is
instantiated), the fundamental COM mechanism is to use the IUnknown method called QueryInterface.
Every interface is derived from IUnknown, so every interface has an implementation of QueryInterface. Regardless of
implementation, this method queries an object using the IID of the interface to which the caller wants a pointer.
If the object supports that interface, QueryInterface retrieves a pointer to the interface, while also calling AddRef.
Otherwise, it returns the E_NOINTERFACE error code.
ix. Marshaling
It is the mechanism that enables objects to be used across thread, process, and network boundaries, allowing for location
independence.
In marshaling, COM provides code (or uses code provided by the interface implementor) both to pack a method's
parameters into a format that can be moved across processes (as well as, across the wire to processes running on other
machines) and to unpack those parameters at the other end.
Marshaling is typically not necessary when an interface provided by an object is being used in the same process as the
object. However, marshaling may be needed between threads.
x. Aggregation
It is a way in which one object can make use of another.
There are times when an object's implementor would like to take advantage of the services offered by another, pre-built
object. Furthermore, it would like this second object to appear as a natural part of the first.
COM achieves both of these goals through containment and aggregation.
Aggregation means that the containing (outer) object creates the contained (inner) object as part of its creation process
and the interfaces of the inner object are exposed by the outer. An object allows itself to be aggregatable or not.
If it is, then it must follow certain rules for aggregation to work properly.
Primarily, all IUnknown method calls on the contained object must delegate to the containing object.