Send a suggestion!

We're building a brand new version of the site, and we'd love to hear your ideas

Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 20,056 times

Contents

Downloads

Related Categories

Accessing a COM component - Early Binding

Imtiaz Alam

Early Binding

Tools Required:
TlbImp.exe: (Type Library Importer) Imports (converts, generates wrapper DLL) the type defination found in COM component into equivalent .net definations (or metadata), understandable by Common Language Runtime (CLR). The metadata generated, by TlbImp can be viewed by Ildasm.exe. If you are using the Visual Studio development environment, you only need to add a reference to the COM type library and the conversion is done automatically.

Let's Start:
To use exisiting COM+ application we need create Runtime Callable Wrapper (RCW) using TlbImp.Exe, with VS.Net it is created automatically, when we reference the exisiting COM Component. The difference is that, VS.Net create the RCW with the same name as the original DLL, which may be confusing, and with TlbImp.Exe, we can specify the different name using /out: parameter.

Let us assume that we have a COM Component with a method Add, which takes two parameter A and B and returns the Sum.

Note: We have to register COM DLL first before using it...
'CompAdd.Dll 
(class1) 
Public Function Add(A As Long, B As Long) As Long

    Add = A + B

End Function

Now we will run TlbImp.Exe in our existing DLL i.e. CompAdd.Dll

This generated a wrapper dll CompAddRcw.dll. we can view this DLL using IlDasm.Exe

The discussion in IlDasm.Exe is beyond the scope of this article. Now we will simply write code to use the Wrapper DLL, which in turn will be calling the actual DLL. Now let us look into the C# code for doing it.

//Written By Imtiaz Alam
using CompAddRcw;
using System;

namespace Imtiaz
{
   class EarlyBinding
   {
       public static void Main()
       {
           CompAddRcw.Class1 objCar = new CompAddRcw.Class1();

           long c;

           int x=100;
           int y=200;
      
           c=objCar.Add( ref x, ref y);

           Console.WriteLine(c);
      
       }
   }
}

As we can see that we are getting the same kind of tooltip as we used to get with VB when we do early binding. Compile the program with /r: switch e.g.csc TestClient.cs /r:CompAddRcw.dll. Execute it and we just called COM component with .net.

Imtiaz Alam is a Senior Developer, currently residing in Phoenix, Arizona. He has more than five years of development experience in developing Mirosoft based Solution.

Imtiaz Alam is a Senior Developer, currently residing in Phoenix, Arizona. He has more than five years of development experience in developing Mirosoft based Solution. He can be reached at alamimtiaz@hotmail.com

Comments

  • Posted by lily0lin on 11 Oct 2005

    I got the same error? Have you figured out how to fix it? Thanks a lot!

  • Server Error when calling a vb dll

    Posted by Anne on 30 Jun 2004

    I have a vb dll that has been using for years in some asp applications. Recently I am developing a asp.net application that need to use same vb dll. I did exactly the steps author listed in early bind...