When we are talking about Interface, we are as well talking about the separation between interface definition and implementation. Therefore, an interface defines a contract between the implementor and the user that physically prevents the user from accessing any of the details of the implementation.
You can look an Interface as a set of methods. This set of methods provides you services. You do not care about the implementation of these methods so long as you can use the services you need. COM interface refers to a predefined group of related functions that a COM class implements, but does not necessarily represent all the functions that the COM class supports.
One object-oriented programming (OOP) scholar says that we should concentrate upon the outside view of an object, and he calls this contract programming
(B. Meyer, Object-Oriented Software Construction, 1988).
COM uses the concept of contract programming.
One of the rules in the COM world is that all COM interfaces must derive, either directly or indirectly, from the IUnknown
Component Object Class (coclass)
(from the Platform SDK: COM / COM Class Objects and CLSIDs
"A COM server is implemented as a COM class. A COM class is an implementation of a group of interfaces in code executed whenever you interact with a given object.
There is an important distinction between a C++ class and a COM class. In C++, a class is a type. A COM class is simply a definition of the object, and carries no type, although a C++ programmer might implement it using a C++ class. COM is designed to allow a class to be used by different applications, including applications written without knowledge of that particular class's existence. Therefore, class code for a given type of object exists either in a dynamic linked library (DLL) or in another application (EXE)."
A COM object is an instance of a COM Class at the runtime.
COM class object
(from the Platform SDK: COM / COM Class Objects and CLSIDs
"The basic way to create an instance of a class (COM Class
) is through a COM class object. This is simply an intermediate object that supports functions common to creating new instances (COM Object
) of a given class (COM Class
). Most class objects used to create objects from a CLSID support the IClassFactory
interface, an interface that includes the important method CreateInstance. You implement an IClassFactory
interface for each (COM) class of object that you offer to be instantiated.
Servers that support some other custom class factory interfaces are not required to support IClassFactory specifically. However, calls to activation functions other than CoGetClassObject
(such as CoCreateInstanceEx
) require that the server support IClassFactory
The Platform SDK: Automation
refers to a class factory
as "An object that implements the IClassFactory interface, which allows it to create other objects of a specific class.
". I will just add that it could be other class factory interfaces (not only the IClassFactory interface) such as the IClassFactory2 or a custom interface.
COM component refers to a binary module, such as a DLL or an Executable. After registering, a component will expose one or more COM Class Object (or Object Factory).
Type libraries contain the specification (metadata) for one or more COM elements, including classes, interfaces, enumerations, and more. These files are stored in a standard binary format. A type library can be a stand-alone file with the .tlb filename extension, or it can be stored as a resource in an executable file, which can have a .ocx, .dll, or .exe file name extension.
In COM world, you have to identify different pieces such as coclasses, interfaces, type libraries, applications, etc. These pieces must be unique in the world.
COM uses the G
) to define these different IDs.
In an Interface Definition Language (IDL) file, you have to use the attribute uuid
that stands for U
entifier. A UUID
and a GUID
So, what is a GUID ? A GUID is a 128-bit number, usually represented in hexadecimal, which is guaranteed "to be unique across space and time". For example, the following number is a GUID:
COM borrows this identify system to the Distributed Computing Environment (DCE) naming scheme. The DCE RPC system uses UUIDs.
The main COM IDs are:
- LIBID: the Type Library ID, based on GUID.
- APPID: the Application ID, based on GUID.
- CLSID: the COM Class ID, based on GUID.
- IID: the Interface ID, based on GUID.
- PROGID: the Program ID, based on a text string.
In short, we can have:
Nota: this scheme is not complete, e.g. the Class factory is missing.
To obtain a GUID/UUID you can use:
- the UUIDGEN.EXE found in the \TOOLs directory of the COM Toolkit (or "\Microsoft Visual Studio\Common\Tools").
- the GUIDGEN.EXE found in the "\Microsoft Visual Studio\Common\Tools" directory.
- the COM API function CoCreateGuid().
Before starting, here are some acronyms used in the Windows registry documentation and their meaning:
- HKLM: the HKEY_LOCAL_MACHINE key. Information registered under this key might apply to all users on the local computer.
- HKCR: the HKEY_CLASSES_ROOT key, a short cut for HKEY_LOCAL_MACHINE\Software\Classes.
- HKCU: the HKEY_CURRENT_USER key.
Class registration and file extension informations are stored under the HKEY_LOCAL_MACHINE\Software\Classes
COM retrieves the information about COM Classes from the local Registry. With Windows 2000 there is now a global implementation repository called Active Directory. COM uses the registry as well to look for Interface properties and security-related configuration in order to manage the marshalling and distributed access.
All COM Classes related configuration information on the local workstation is stored in the following registry keys:
- HKEY_CLASSES_ROOT, for backward compatibility.
- HKEY_LOCAL_MACHINE\Software\Classes, Available on Windows NT 4.0 .
- HKEY_CURRENT_USER\Software\Classes, Available on Windows 2000.
On Windows 2000 you can register COM class not only as the local machine level but also at the user level, and so you should be very careful as the HKCR
is a merged view of HKLM\Software\Classes
key and HKCU\Software\Classes
key. This is due to the fact that in Windows 2000 class registration and file extension information are stored under both the HKLM
key and the HKCU
A CLSID is a globally unique identifier that identifies a COM class object.
The AppID registry key groups the configuration and security options for all coclasses exposed by a COM Sever and it is localised in the registry like that:
The keys referring to ACL values (AccessPermission and LaunchPermission) have to be set using an appropriate tool such as DCOMCNFG.EXE
The Interface registry key registers new interfaces by associating an interface name with an Interface ID or IID and it is localised in the registry like that: