Introduction to COM
This introduction gives more emphasis on COM as applied to
What is COM?
Component Object Model (COM) is a Microsoft standard for developing component based
software. COM provides :
- A definition - or model- for an object
- Services for creation and destruction of the object
- Services for communication between client and server
COM reuses the code in binary form. This makes it possible to write the component with
different tool and use it in some other tool.
What are Advantages of COM?
- Binary code reuse : You can write and use your COM components with different COM
compliant development tools like VB, VC++, Delphi etc.
- Cross-platform development : Even though Windows is the ultimate platform for COM
development. COM can be used for other platforms also. Some vendors are providing support
- Version Control : COM components are said to be self-versioning. You can install
multiple versions of same COM component on the same machine. Your older applications
continue to work the same way and you are free to utilize new functionality of the
component for new applications
What are interfaces?
Interface is a set of properties and methods through which client and server component
communicate with each other. Interfaces just provide details like method name, its
parameters , return type etc. and not the actual implementation of the method. If you are
a VB programmer then you can look at it this way- each class within your EXE or
DLL (which is a set of properties and methods) project represents an interface.
What are the requirements of a COM component?
To be recognized as a COM component all the objects must satisfy following conditions :
- The object must be able to keep track of no. of connections made to it
- The object must be capable of being queried by client for the methods or properties.
This functionality is provided by an interface called IUnknown. This interface
has following methods :
- AddRef : called when a new client connects to it i.e. when you say Set x=new ...
- Release : called when the object is destroyed i.e. when you say Set x=nothing
- QueryInterface : which can query for methods supported by the object. VB does this
automatically when you say Set x=new...
What are different types of COM components in VB?
- ActiveX DLL
- ActiveX EXE
- ActiveX Controls
- ActiveX Documents
What are in-process components ?
In process components are ActiveX DLLs which run in the same address space as that of
client. The communication between client and in-process server is fast but if the server
crashes it can also cause the client to crash.
What are out-of-process components?
Out-of-process components are ActiveX EXEs which run in their own address space. The
communication between client and out-of-process server is slow because of marshalling. If
the server crashes the client can still continue to function as the two processes are
What is marshalling?
When a client calls a method from out-of-process server component the required
parameters need to be sent across process boundaries. Packaging and transporting of
all parameters and return values between client and server is known as Marshalling.
What are GUIDs?
GUID or Globally Unique Identifier is a 128-bit number generated using an algorithm
which ensures its uniquely throughout the world.
GUID is key for identifying various COM components and their versions.
Before you use any component it needs to be registered with the operating system. The
registration is nothing but adding certain GUIDs like ProgID, ClassID and type
library, path etc. in the Windows registry (HKEY_CLASSES_ROOT section).
What are type libraries?
Type library provides information about a component like :
- Definition of interfaces provided by the object
- Information about properties, methods and events provided by the object
- The parameter data types and return types of methods
Some tools create type library as a separate disk file ( typically .tlb or .olb ).
Visual Basic embeds this information in the compiled DLL or EXE itself.
What is IDispatch interface?
IDispatch is an interface provided mainly for scripting languages to discover about
properties and methods of a COM component. It also supports functionality of IUnknown. The
interface provides following functions :
What are dual interfaces?
Dual interface is a custom interface designed by a development tool which provides all
the functionality of IDispatch plus some efficient mechanism to call the methods of the
The less sophisticated clients and scripting languages continue to use IDispatch methods
but advance clients like VB can use the more efficient mechanism.
What is early binding and late binding?
- Early binding means that the compiler has knowledge about an object at compile time
- Late binding means that compiler do not have any knowledge about an object
Here, the 'knowledge' of an object refers to the properties and methods of an object.
For a VB programmer early binding occurs when you say something like :
dim rs as new ADODB.Recordset
Since you have specified exact type of object, compiler already knows about the
object. You can check it in VB IDE also. If you type rs. then VB IDE automatically presents
you with the properties and methods of record set. You will not get such a list if you
declare rs as :
dim rs as Object
As you might have guessed late binding is not an efficient mechanism and you should
avoid using it as far as possible.
What is instancing of a component?
When you open an ActiveX DLL or EXE project each class has a design time property called
Instancing. This property determines how your objects will be created. There are three
main categories :
- Single use : in which case each requesting client is provided with new copy of the
- Multi use : in which case multiple clients share the same instance of the component
- PublicNotCreatable : this means instance of your component can not be created directly
by 'New' or 'CreateObject' but some other component returns one to you (generally as return
value from a function).
What is most efficient way to create objects?
In VB you can create objects using following methods :
- dim x as new myclass
- dim x as myclass
- dim x as myclass
set x=new myclass
Out of these methods the first method is least efficient and you should actually never
use it (unless you are doing testing etc.)
The second method uses standard COM services and is the only way to use in scripting
languages like VBScript. If you are coding in VB and creating multiple instances of same
class then try using last method instead
The last method also uses standard COM services but is optimized for creating multiple
objects of same class. Thus it is the most efficient.
What is interface inheritance?
Unlike languages like C++ VB do not provide inheritance of implementation. VB provides
something called interface inheritance. Which means you can inherit interface ( or
declaration or syntax ) of methods and not the implementation( or definition).
You can inherit classes using Implements <class name> keyword
How do I provide events to my class?
There are four basic steps in designing and using an event :
- Declare an event in your class
public Event myevent(param1 as integer)
- Raise event in some of the method of the class
- Now, at client side declare object of your class
dim WithEvents x as new myclass
- Write code to handle myevent event
public sub myevent(param1 as integer)