tags:

views:

759

answers:

5

It seems COM objects are general use objects which are governed by the OS. The objects follow a strict interface and allow you to query the objects to determine information. Is this what COM objects are?

+6  A: 

You won't beat wikipedia on this.

Igal Serban
+4  A: 

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

SnOrfus
Actually, COM doesn't suck, but it's too hard for most people, because of the way things are done. Being one of the first attempts to implement a way to make programs interoperate, it's rather good.
Eduardo León
Agreed with Eduardo.
unforgiven3
I don't think COM sucks. In C++ you can just include the headers. In some unknown program language I have to use at work we can't use C headers. Especially as it is not compiled to machine code. COM is designed for any laguage and it is good for that.
Tim Matthews
@eduardo - the altair was the first attempt to make a computer, but compared to the systems we have now... it sucked. bad. The idea for COM was great, but the end result left much to be desired. *remembers having to write COM interfaces in IDL - cries*
SnOrfus
+5  A: 

COM was a mechanism developed to allow people to distribute binaries that could be reused even if the caller was using another vendor's C++ compiler or (ultimately) a different language altogether.

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

McDowell
A: 

This. :)

Steve Fallows
+4  A: 

COM is a mechanism that allows the re-use of objects (or rather components), independently of the languages used by the programmer who implemented the component and the programmer who uses it, and independently of whether the component was implemented in the client's program or elsewhere on the machine (or network).

Broadly speaking, each COM component provides an implementation of one or more interfaces. Those interfaces are defined in a language-neutral manner using the Interface Definition Language (IDL). As an example, one of the fundamental interfaces in COM, IUnknown, is defined like this:

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

This little interface is fundamental in COM, because each COM component must implement it. It defines two important aspects of the COM machinery:

  • QueryInterace allows calling code to get an implementation for a known interface. In COM, interfaces are referenced by GUIDs (also known as Interface Identifiers, IID). If an object implements several interfaces, that's how client code gets a reference to each of those interfaces. It acts as a sort of casting operator, if you will.
  • AddRef() and Release() implement the memory management mechanism for COM objects. As their name suggests, the most common model is the reference counting mechanism, where an instance is destroyed after the last client has released its reference to it.

All COM components are registered with the system upon installation. If a programmer wants to use a certain component, he needs to:

  • Make sure the component is installed at a reachable location. Most of the time it is on the system of the running application, but COM+ also allows components to exist on remote computers.
  • Know the GUID of the given component. With this GUID, the client can then ask the system to instantiate the component (in C, the function to do this is called CoCreateInstance()). You can look in the registry under HKEY_CLASSES_ROOT\CLSID: each GUID in there is (most probably) an identifier for a COM component or interface, and the entries below that key tell the system how it should be instanciated.

The COM machinery is extremely complex. For example, implementing or using COM components in C requires a horrendous amount of work, but higher-level languages like Visual Basic have done a lot to ease the implementation and use of COM components. The benefits are however very real. It makes it possible to write an application in, say, Visual Basic, but to still implement the performance-critical algorithms in C or C++ as COM objects, which can be used directly from the VB code. The system takes care of marshalling method-call arguments, passing them through threads, processes and network connections as needed so that the client code has the impression of using a normal object.

Many fundamental parts of Windows are based on COM. Windows Explorer (the file manager), for instance, is basically an empty shell. It defines a bunch of COM Interfaces for navigating and displaying tree hierarchies, and all the code that actually displays "My Computer", the drives, the folders and the files is as a set of COM components that implement those interfaces.

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

Carl Seleborg