What is COM (Component Object Model) in a nutshell?

It seems that COM objects are public objects that are managed by the OS. Objects follow a strict interface and allow you to query objects for information. Are these COM objects?

+43
c ++ com
Jan 18 '09 at 18:45
source share
3 answers

COM is a mechanism that allows you to reuse objects (or rather components), regardless of the languages ​​used by the programmer who implemented the component and the programmer who uses it, and regardless of whether the component was implemented in a client program or somewhere either by car (or network).

In general, each COM component provides an implementation of one or more interfaces. These interfaces are defined in a language-neutral manner using the Interface Definition Language (IDL) . For example, one of the main interfaces in COM, IUnknown , is defined as follows:

interface IUnknown { virtual HRESULT QueryInterface(REFIID riid, void **ppvObject) = 0; virtual ULONG AddRef(void) = 0; virtual ULONG Release(void) = 0; }; 

This small interface is fundamental in COM because every COM component must implement it. It defines two important aspects of a COM machine:

  • QueryInterface allows QueryInterface to call code to get an implementation for a known interface. In COM, interfaces refer to GUIDs (also called interface identifiers, IIDs). If an object implements several interfaces, then how the client code receives a link to each of these interfaces. He acts as a kind of casting operator if you do.
  • AddRef() and Release() implement a memory management mechanism for COM objects. As their name implies, the most common model is the link counting mechanism, where the instance is destroyed after the last client issued a link to it.

All COM components are registered by the system during installation. If a programmer wants to use a specific component, he needs to:

  • Make sure the component is installed in an accessible place. Most of the time it is in the system of the running application, but COM + also allows components to exist on remote computers.
  • Know the GUID of this component. Using this GUID, the client can ask the system to instantiate the component (in C, the function for this is called CoCreateInstance() ). You can look in the registry under HKEY_CLASSES_ROOT\CLSID : each GUID contains (most likely) an identifier for a COM component or interface, and the entries below this key tell the system how this should happen.

The COM machine is extremely complex. For example, implementing or using COM components in C requires an appalling amount of work, but higher-level languages ​​such as Visual Basic have done a lot to ease the implementation and use of COM components. The benefits, however, are very real. This allows you to write an application, say, Visual Basic, but still run critical C or C ++ algorithms in the form of COM objects that can be used directly from VB code. The system will take care of the arguments of the sort calling method, passing them along threads, processes, and network connections as necessary, so that the client code has an idea of ​​using a regular object.

Many of the fundamental parts of Windows are based on COM. For example, Windows Explorer (file manager) is an empty shell. It defines a group of COM interfaces for navigating and displaying tree hierarchies, as well as all the code that actually displays β€œMy Computer,” disks, folders, and files are a set of COM components that implement these interfaces.

With the advent of .NET, COM is gradually becoming obsolete.

+54
Jan 20 '09 at 8:51
source share

COM is a mechanism that was designed to allow people to distribute binary files that could be reused, even if the caller used a different C ++ provider compiler or (ultimately) a different language in general.

If you want a good introduction to COM, read Essential COM Don Box .

+21
Jan 18 '09 at 19:23
source share

Think of it this way: when .NET came out, I immediately thought: "Wow ... this is COM, but it does not suck."

+15
Jan 18 '09 at 18:52
source share



All Articles