Make Legacy VB 6 Components Work With .NET Applications

Jan 17, 2012
C#
By

Most IT professionals do not work in state of the art design shops with all the latest developer suites available to them.

Many of us have to make older applications and components work with newer technologies.  I found myself in this predicament last year when I was required to make a new ASP.NET (3.5) web application work with a ten year old VB6.0 COM Plus Component.

When I suggested to management that I re-write the component, they said:  There’s no time. I know you can make it work. By the way, we need it by Friday. Story of a developer’s life, huh?

I got lucky.  A developer friend of mine had done just this and it turns out to be fairly simple. In order to call a VB 6.0 component or dynamic link library (.dll) from a .NET application, you need to generate a Runtime Callable Wrapper (RCW) for the older .dll using .NET’s Type Library Importer (tlbimp.exe) and reference the wrapper created by the importer in your .NET app.

The primary function of the RCW is to expose the older COM app to .NET’s Common Runtime Language (CLR).  The RCW exposes the older app to the CLR using the .NET Interfaces: INew and INewer.  These interfaces are used by the RCW to police the cache of pointers to the COM object and keep track of when they need cleaned up by the .NET garbage collection process.

Runtime Callable Wrapper Model

Another function of the RCW is to interpret and validate types between the Unmanaged COM object and your Managed .NET application. The RCW must review method arguments and return codes and translate them into the equivalent .NET types.  Microsoft calls this reviewing process marshaling.

Next I’ll walk you through the steps to create the RCW.  In this example, my .NET Client is called MyEnterpriseWebApp and the COM object is called OldLogger.dll.

Step 1: Open a Command Window with Administrative Access.

Step 2: Change Directory (CD) to the Bin folder of the version of Visual Studio you use. This is where you will find the Type Library Importer program.  On my computer it is found here:  “c:\program files\Microsoft Visual Studio 8\SDK\v2.0\Bin\tlbimp.exe”.

Step 3: Enter the command to create the RCW as seen below.  The ‘out’ parameter is the name of your wrapper dll file.

C:\<path to tblimp.exe file>>tlbimp C:\<path to your COM Object>\COMOBJ_NAME.dll /out:C:\<path to your RCWrapper File>\WRAPPER_NAME.dll

NOTE: If you leave out the <path to your RCWrapper File>, the new wrapper file will be placed in the same folder as tlbimp.exe.

Using the tlbimp.exe to create the RCW.

Now that the wrapper has been created let’s take a look at how to hook it up to your .NET Client application.  This wrapper can be used in any type of .NET app.

Step 1: Add a reference to your new wrapper assembly in your .NET app by right-clicking on the Project in Solution Explorer and selecting “Add Reference”.  Click the “Browse” tab and navigate to the place you put the wrapper.dll.

Step 2: Import the reference to your class.  Here is an example of importing using C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LoggerRCW;

Step 3: In your .NET class access your COM Object as its Interface requires.  Here’s an example of how the OldLogger COM works.

namespace MyEnterpriseWebApp
{
   public partial class _Default : System.Web.UI.Page
   {
      protected void Page_Load(Object sender, EventArgs e)
      {
	//Create Logger Object
	LoggerRCW.clsApplogClass oLog = new clsApplogClass();

	try
	{
		//Page Load code here.
	}
	catch(Exception ex)
	{
		oLog.logMessage("Message to Log");
	}
	finally
	{
		//Clean up Logger Object.
	}
     }
   }
}

Viola!  Access to your old COM object from your new .NET app!

–Happy Coding

Author: Kim S. Teeple
Kim S Teeple graduated with a Communications Degree from Ohio State University, but found she had an aptitude for computers soon after college. She joined The Limited's IT department as a helpdesk analyst in 1995 and quickly moved into web development. In 1998, she moved back to her home town of Crestline, Ohio to join Pittsburgh Glass Works as a Systems Analyst. She has also done some free lance web development work for various companies.
  • Ruth Hendricks

    That is one impressive post with information I do not understand.

    • Anonymous

      What specifically did you have trouble with? We can certainly have the author answer any questions.

  • VK

    Viola? Cello? Piano!