<%@ Page %>
.NET Interoperability with COM
Up till recently COM ruled the world of component development. Many companies
have invested a great amount of money and time in COM components. With
introduction of .NET people are concerned about the future of COM. Microsoft has
recognized this and provided means to use classic COM components in .NET code
and vice versa. This article discusses the process of using COM components in
.NET code as well as .NET components with COM compatible tools.
What is wrong With COM?
Even though COM is most successful technology for
component development it sufferers from some weaknesses.
Registration : Perhaps registration
is the most troublesome part while using COM components. Every COM
component stores information about it in Windows registry. The information is
basically about GUIDs, CLSIDs, Path etc. Even though a COM component is to be
used by only one application, this information is made available machine wide.
Versioning : Another problem with
COM components is version compatibility. Many times the newer versions of COM
component overwrite its earlier version and breaks some old applications.
Cross-language Support : It is
said that COM components developed in one language can be used any other COM
compatible tool. However, components developed in VB can't be used easily in VC++.
What is the difference between COM component and .NET components?
For a component to be called as COM compatible it must
support IUnknown interface. This interface is responsible for
reference counting of the component. When you compile a COM
component all the information about various methods and their parameters is
stored in a 'Type Library' (In case of VB type library information is embedded
in the resulting EXE or DLL itself). To successfully use COM component this
information must be available along with some registration information.
In contrast, .NET component are very different. The component do
not itself keeps track of reference count. It's the responsibility of the
CLR. Also, .NET components store all the information about the component along
with the assembly itself. This information is called as MetaData. .NET
components are self-contained units of functionality that do not require any registration.
Why interoperability between .NET and COM is
Even though .NET provides good platform for component
development COM is not going to die in near future. Following are some of the
basic reasons for which will keep COM live :
Companies have already invested great amount of
money and time in developing COM components. Such interoperability will save
them from more investment.
Applications already developed in VB6 or VC++ can be
smoothly ported to .NET platform since during initial phases existing COM
components can be used out of the box.
Existing COM components are already tested and
working as per business rules. If we shift to .NET components this testing is
to be performed again.
COM components and managed execution
All the code that executes under the control of .NET is
called as managed code. The memory consumed by such code is reclaimed by CLR
automatically via garbage collection mechanism.
COM components are not under control of .NET CLR and
hence treated as unmanaged code. Tasks like automatic memory management are not
available to such components.
Deployment of COM components
.NET components do not require any registration with the
system registry. This makes XCOPY deployment possible for such applications.
Once you decide to use COM components in your .NET application you should also
take care to register the component while installing the application. The XCOPY
deployment will no longer work. You must 'regsvr' your COM components on the
Using COM components in .NET
Now let us understand how to use classic COM components
in our .NET code. Following are the steps you must perform to use such
Register the COM component on development
Generate MetaData for the COM component using TlbImp
(Type Library Importer) utility
Consume the functionality provided by COM
Compile your .NET code by giving reference to the
Deploy the application
In this section we will create a Visual Basic 6 COM DLL
called MyComp. This component will supply a class called MyClass. MyClass has a
simple method called GetMessage() which simply returns a string message.
Now, we will generate metadata
of the COM component using TlbImp utility. Go ahead with following command line
This will create
a .NET DLL named mycompprody.dll. You will use this dll in your .NET
Next, we will create a .NET console application that
makes use of our COM component. Here is the source code from the
public class comtest
dim x as mycomp.myclass
public sub new()
dim s as string=x.getmessage()
shared sub main()
dim test as comtest
Compile above vb file with following command line :
vbc comtest.vb /t:exe
Note how we have imported COM component namespace in our code.
Using .NET components in traditional COM tools
We have already seen how to use COM components in .NET.
Now let us try the reverse case. In order to use .NET component in COM
environment you must perform following steps :
Create your .NET component
Generate registry information using RegAsm
Add reference to .NET component from your COM
Use component as if it is traditional COM
In this section we will create a .NET component and use
it in VB6
First, create our component as follows :
public class nettocomtest
public function GetMessage() as string
return "Hello From .NET Component"
The component is very simple with just one method.
Compile this component as nettocom.dll
Next, we will generate registry information and type library for this
component. Run RegAsm utility from the command line as follows :
RegAsm nettocom.dll /tlb:nettocomp.tlb
This will insert information like CLSID in the system
registry. Note that to use this component you need to have .NET SDK installed on
the system. You are now ready to use this component in COM compatible tool like VB6.
.NET allows you to use your existing COM components
in .NET code
You use TlbImp utility to import type library
information into .NET metadata
You can also use your .NET components in COM
You use RegAsm utility to generate registry
information about the .NET component