COM 101: A Quick Primer - Features Of COM (Page 2 of 4 )
Lets first start by looking at some salient features COM:
COM is a specification. It describes what standards you should follow in order to create a component.
Now COM also includes a set of services. The specification is backed up by a set of API's. These API's include various helper functions for the COM programmer.
COM is object oriented. Each COM object has its own identity and state.
COM objects can link dynamically. Just visualize them as plug and play devices on your computer: just plug it in and it starts working. Pull it out when you don't need it.
COM enables you to create distributed applications too. You donít even have to know where the COM object resides when you call it. It could even reside somewhere on a remote computer.
COM applications can be written in any programming language.
COM components are self-versioning. This means that when you upgrade an already existing COM component it is treated as another version of the same component. This is done because it helps to avoid any version conflicts with older clients.
COM components help you implement code reusability. Once a COM component is created it can be use any number of times in any number of projects.
Now, when you create a COM component it can be of any of these types:
In-process Component: They are in form of (DLL's) Dynamic Link Libraries. They run in the memory space of your client application (that's why they are termed as in-process). If they crash they crash the entire client application with them as they operate in the same memory space as that of the client application.
Out-process Component: They are in the form of (EXE's) Executable's. They run in a different memory space as that of your client application (that's why they are termed as out-process). If they crash it doesn't affect the client application as they operate in a different memory space as that of the client application.
Remote Component: Remote components are just like any other component but the only difference is that remote components run from a separate remote location via a network. They are implemented using DCOM (Distributed COM).
COM components have a unique identity number. These numbers are stored in the registry under the HKEY_CLASSES_ROOT hive. COM is, as I explained in its features, not just a specification on paper. It also includes various API's. It also includes some amount of system-level code. All of this is present in your COM runtime library.
"The Component Object Library is itself a system component (which is usually present in your OS) which provides COM components the ability to make calls among different components within a process (in-process), across processes (out-process) or over a network (remote)."
The component object library is implemented through OLE32.DLL on Windows NT and Windows 9x machines.
Component objects, which you create, are highly encapsulated. When you create a component and distribute it, the client using it can't see the implementation code. He doesn't even have access to the class you have your code in.
If this is the case then how does the client using your component to access the services in it? Well, here is where the concept of Interfaces jumps in. Every component has an interface, which it has to implement. That interface do not carry any implementations of the methods: those are present in the CoClass (Component Class). The Interface simply contains the method declarations. The interface is the only way a client can access the services of the component.
Now interfaces are implemented using vTables. The vTable contains an array of pointers. These pointers in turn point to the functions of the component. When you create an object of the component in your code, it also creates its vTable in memory.
The client creates a pointer to the interface. That pointer points to a virtual pointer. The virtual pointer points to the vTable. Using the interface pointer and the virtual pointer, the client of the component can call any functions in it.
There are mainly two types of interfaces:
Standard Interfaces: These are the interfaces provided by the COM library. Some of the standard interfaces are IUnknown, IDispatch, IClassFactory, IOle, IDataObject, IStream and IStorage.
Custom Interfaces: These are the interfaces created by you.