C# and ActiveX DLLs

Posted by in C# category on for Advance level | Views : 11087 red flag

You may wonder about the idea of calling this DLL in a C# Application. Well, the C# language provides a way to call this COM server in a program. When we compile a C# program, Intermediate Language is generated and it is called as Managed Code. A Visual Basic 6.0 DLL is Unmanaged, meaning it is not generated by the Common Language Runtime, but we can make this VB DLL interoperate with C#, by converting it into a .NET compatible version. The following example shows how to create a simple server by using Visual Basic 6.0 and implementing it in a C# client program.

An Application using ActiveX DLL 


  1. Fire up Visual Basic and select ActiveX DLL Icon from the New Project dialog box.
  2. Change the class name to something meaningful like “Our_csharp”
  3. Now supply the following code:

               Public Function Show()
               MsgBox ("Message box created by using Visual Basic")
               End Function

  1. For creating a function, you can use Tools | Add procedure menu.
  2. Save the project by supplying relevant class and project names.
  3. Change the Project name and Description by selecting Project | Properties menu.
  4. Set the Binary Compatibility from the components tab of the above menu. This will not create separate GUID upon each compilation.
  1. Finally create your DLL by selecting File | Make CsharpStation.dll. This is the name by which you save your VB project.
  1. That’s all. Your DLL is now ready to use in a C# program.

It is not possible for a C# program to communicate with a VB DLL without converting it into a .NET equivalent. For doing so .NET SDK provides a tool called tlbimp. It stands for Type Library Import and converts your DLL to its equivalent .NET Assembly. For the above project, supply the following command after properly setting the Environment Variables:

tlbimp CsharpStation.dll /out:Csharp.dll

A new .NET compatible file called Csharp.dll will be placed in the appropriate directory. Type in the following C# client program and execute as usual:

   using Csharp;
   using System;

   public class Csharpapply     {

       public static void main()         {
           Our_csharp c = new Our_csharp();

Upon execution, you can be able to see the message box from our VB DLL.


If you are using Visual Studio. NET, then you can refer the DLL from Project | Add Reference | COM Tab. Select your DLL by using the browse button. This will add reference to your project. After adding a reference copy the above code and execute.

.NET SDK users have to run a batch file named corvars.bat before attempting compilation. This batch file is included with the SDK and located in the bin directory.

Page copy protected against web site content infringement by Copyscape

About the Author

Full Name: Majith Basha
Member Level:
Member Status: Member
Member Since: 7/18/2008 11:49:59 PM

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)