December 2017 : Online courses in ASP.NET MVC and Angular 4. Conducted by Bipin Joshi. Read more...
Registration for December 2017 batches of ASP.NET MVC / Core and Angular 4 online courses have already started. Conducted by Bipin Joshi. Book your seat today ! Click here for more details.

<%@ Page %>

.NET Interoperability with COM

Introduction

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 essential?

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 client machine.

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 components :

  • Register the COM component on development machine
  • Generate MetaData for the COM component using TlbImp (Type Library Importer) utility
  • Consume the functionality provided by COM component
  • Compile your .NET code by giving reference to the COM component
  • Deploy the application

Hands on...

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 :

           TlbImp mycomp.dll
/out:mycompproxy.dll

This will create a .NET DLL named mycompprody.dll. You will use this dll in your .NET code.

Next, we will create a .NET console application that makes use of our COM component. Here is the source code from the applications:

imports system
imports mycomp


public class comtest

  dim x as mycomp.myclass

  public sub new()
   mybase.new()
   x=new mycomp.myclass()
   dim s as string=x.getmessage()
   console.writeline(s)
  end sub

shared sub main()
   dim test as comtest
   test=new comtest()
end sub

end class

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 utility
  • Add reference to .NET component from your COM environment
  • Use component as if it is traditional COM component

Hands on...

In this section we will create a .NET component and use it in VB6

First, create our component as follows :

imports system
namespace nettocom
       public class nettocomtest
            public function GetMessage() as string
                 return "Hello From .NET Component"
           end function
      end class
end namespace

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.

Summary

  • .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 environment
  • You use RegAsm utility to generate registry information about the .NET component

Bipin Joshi is a software consultant, an author and a yoga mentor having 22+ years of experience in software development. He also conducts online courses in ASP.NET MVC / Core and Design Patterns. He is a published author and has authored or co-authored books for Apress and Wrox press. Having embraced the Yoga way of life he also teaches Meditation and Mindfulness to interested individuals. To know more about him click here.

Get connected : Twitter  Facebook  Google+  LinkedIn

Posted On : 28 April 2001


Tags : .NET Framework Architecture Components Programming Languages