Introduction

This example will help you understand how to implement and use a C# DLL in VB 6.0 code. As C# is an object-oriented language, we can use the object-oriented features to create proper classes in C# DLL. We can use COM Interop or follow the COM Plus Approach to refer to such DLLs in our old VB 6.0 applications. It's like delegating our business logic to this DLL. In this article, I tried to show two different approaches to refer to a C# DLL in VB 6.0. The code is attached herewith; you can directly refer it to understand the following instructions or try to create your own code from the instructions.

To create COM Interop

By using COM Interop, we create a DLL that can be private or shared. This DLL can be referred to in a VB 6.0 application. A VB 6.0 application refers to a Type Library of this DLL, i.e. a file with the .tlb extension that is created using the VS tool utility. For a client of a COM object to have access to the object, the client needs a description of it, how to locate it and how to call its methods and properties. For a "real" unmanaged COM class, this description is available in the form of a Type Library. A Type Library is a binary description of the GUIDs, classes, and interfaces (methods, properties, and parameters) that the COM class supports.

.NET assemblies don't include information in Type Library compatible format. So, it is necessary for the programmer to run one of two .NET-supplied utilities to extract the assembly description of a class into a Type Library file. One utility is tlbexp.exe, the .NET Type Library Exporter. This command-line utility takes as input the name of an assembly DLL file to be converted to a Type Library. The programmer can also specify the name of a Type Library file to be created.

tlbexp ComInteropExample.DLL /out:ComInteropExample.tlb

Once a Type Library has been created, it can be referenced by a COM client to obtain the information necessary for the COM client to bind to the interfaces of the COM class and activate the COM class at runtime. Another command-line utility for creating a Type Library from an assembly is regasm.exe, the .NET Assembly Registration utility. In addition to creating a Type Library, this utility also creates the Windows Registry entries necessary for making the assembly visible as a COM object to clients, as shown below.

regasm ComInteropExample.DLL /tlb: ComInteropExample.tlb

Note that there is also a property in the Project Properties for a .NET class library DLL called "Register for COM Interop." Setting this property to True instructs the IDE to automatically register the assembly for COM Interop each time you build it, so you don't have to perform this step manually.

Instructions for creating a COM Interop DLL in a Visual Studio 2005 project

Register the assembly using the SDK command prompt of VS 2005. Go to the release folder of your project:

regasm ComInteropExample.DLL /tlb: ComInteropExample.tlb

Refer to this TLB in VB. The VB code is provided with the ZIP file, so please refer to it. It can be used for late binding. If the strong key is not assigned, the assembly will be private, so copy the assembly into the folder where you want to use it. To make the assembly public, assign the strong key to the assembly using the SN tool of VS 2005.

Summary

Refer to the TLB of the assembly from the Release folder to use in VB6 applications. Please let me know if you have any question. I have tried to make this article a readable one and I will try to improve it with your invaluable suggestions. Thank you.

Hi Rakesh, I work in one of the MNC IT company in India where I was given a task to convert a VB project to VB.NET. Evrything went well in the conversion and as a result I got a successfull build after some minor changes in the code in VB.NET.

But when I made the project to run, it throwed a run time exception as "Creating an instance of the COM component with CLSID {XXXXX} from the IClassFactory failed due to the following error :80004004" when the project accessed a reference file (DLL which has file type as ACTIVEX in its properties).

The strange thing is the Dll name used by VB was EXECU.dll. But after the conversion the reference was changed to interop.ECECU.dll

Even when I tried to add the reference manually to the dll EXECU.dll, its refernce automatically changes its name to interop.EXECU.dll.

When I browsed the dll content in the object browser I found that the DLL was having the class EXECU inside the namespace (library) but the class was not having any function in it.

Do i have to run anything to get the full functionality of the original DLLAlso what is the error code?

I used this concept in my application. It is working fine on my development system.After that i try to run my application on some other machine Where vb 6.0 and .NET 2.0 framework is installed. The application showing the following error "automation error the system cannot find the file specified".

i registered the dll on the testing machine by using regasm.

I placed the dll and tlb files in same path as develpment machine.

dim obj as Operation.additionset obj = new operation.addition -------------> here it is showing the errir "automation error the system cannot find the file specified"dim str as stringmsgbox(obj.add(10,20))

Thanks a lot man. This worked for me. I was trying to publish feeds from Excel to C# application using remoting and I didn't have problem on dev machine but had it on prod. I used /code flag and then it started working.

I used this concept in my application. It is working fine on my development system.After that i try to run my application on some other machine Where vb 6.0 and .NET 2.0 framework is installed. The application showing the following error "automation error the system cannot find the file specified".

i registered the dll on the testing machine by using regasm.

I placed the dll and tlb files in same path as develpment machine.

dim obj as Operation.additionset obj = new operation.addition -------------> here it is showing the errir "automation error the system cannot find the file specified"dim str as stringmsgbox(obj.add(10,20))

...I find that the VB6 code crashes on the call to Test. I get a message:Visual Basic has encountered a problem and needs to close. We are sorry for the inconvenience.If you were in the middle of something, the information you were working on might be lost.

1- First .. Have you Compiled your C# Code to be Sure 100 Percent that Your C# Code Contains no Bugs Or Not . 2- Second : In your VB Code :

Dim d() As Double Dim c As New csClass Rem Since you have Created a Dynamic array , You need to use : Rem ReDim d(5) As ... for 5 elements say , Or to use: Rem Redim preserve d(7) As ... for expanding (resize) the array with out wiping the old content. Rem So you must have the following statement at least : ReDim d(8) : rem if you need 8 Rem Then use : c.Test d(1)

Rem Or you must have a Loop I= 1 to 8 : C.Test d(I) : Next I

3- Third: for the declaration of the Class Instance Rem I dont see Set c= New csClass after your Dim c as New csClass

Rem since Declaring of a class instance in one of the two available declarative ways. Rem may not work in your environment , so use the other way of declaring. 4- Forth: you may need to declaire an array of the class instance instead of declaring d() as an array,if this is true you must apply dynamic array declaration and ReDim .... statement for your class declaration and this depends on your entire code which is not visible to us to guess your problem more precisely. I hope this leads you to find out what is Incomplete in your code ... check please.

Hello,I greatly appreciate this article. Because of the To create COM Interop section, I was able to create a TLB file and use VB6 to import it.I would very much like to know how to import a TLB into a Visual C++ 6.0 program. Can anyone demonstrate this to me or show me another article/link in which I can find what I'm looking for? Any help at all is appreciated.

Thanks for a great example of a standard cominterop project. I have able to add a VB6 com complaint to a .net 3.5 framework DLL containing a public interface and a class using the interface.Thanks again Gary K.

Hey, thanks for this. This was really helpful for me.I had a project where I needed to use .NET 2.0 to use a web service.I needed to be able to make a dll that vb6 could call and use.

I had some troubles, and I thought I would share them here so others may have an easier time.I found that the comPlus never worked for me. I found that the comPlus also was making my project not build an EXEI removed the comPlus from the VB6 application. I added the reference after the regasm.I had some problems with a MISSING: prefix but it does not seem to affect the project.I found that the VB6 project would never debug. It would always say file not found.If you build the .EXE and place it in the folder with the .tbl and .dll, it works FLAWLESSLY

this is just my account of how things went for me. Others might have had no problems.thanks again!

When I use dll in excel it's show the error message'The format of the file 'myDll' is invalid.' i 've checki have copy mydll.dll file to excel folder but it's always showthat message and can't works!!Do you know why?

actual i use your sample in excel(2003) vba on other PC(not the same as source code)It's show the message The format of the file 'ComInterOpExample' is invaild.And i try create my mydll.dll follow your method but it's still appear the same message.It's strange the develop pc is work,but it's cant work on other PC,I follow you method to deploy sample dll.

Yes,I've installed .net 2.0 framework.And i also install vb6 in that pc.It's work by VB6 but it's fail by Excel VBA,Very strange!I guess that is Excel use .Net 1.1 to run sample dll.I cant slove it yet.

Hey the dll provided get compiled on Dot Net Framework...as it is developed in Dot Net 2.0...did u install DotNet 1.1 or 2.0?...The dll will get compiled on DotNet 2.0.....Another thing if u mailed(gunijan_rakesh@yahoo.co.in) me zist or prototype of ur problem(how u r accessing dll in excel....i am dumb in excel), i can try at my end....

Actually there are two set of TLbEXP executables.1. That comes as part of the Visual studio .net 2003 (Path:c:\Programfiles\microsoft visual studio .net 2003\sdk\v1.1 )2. That comes as part of Microsoft SDK (Path: c:\Program files\Microsoft SDKs\Windows\v6.0A )

If ur "myDLL" is built using vs 2k5/2k8 ( anything greater than 2003), Use Option 2.This should solve ur issue..

Very useful article! I succesfully build a COM+ component.However, I was a bit confused in the strong key part, where I had to rebuild my project after assigninng the strong key. This was not explained in the article.

1> regasmexposes _Object as the default interface. To have C# class public method shown withVB intellisense, you need to declare an interface containing those methods,and let your class extend it.

2>The Automation error says "The System cannot find the file specified" Run-time error - '2147024894(80070002).VB6 client is not able to locate the C# dll , because the creation of the object itself is throwing an error. Am I missing something else?Ans: It complains that the DLL you compiled with C# is not found.You can do one of the following:- copy the DLL to VB installation folder (likely C:\ProgramFiles\Microsoft Visual Studio\VB98)- copy DLL to OS system folder (e.g WinNT/System32)- Copy the DLL file to the same folder as your VB DLL/EXE.

3>Why is it required to place the VB Exe in the same path as the C# Dll?Ans: Use the /codebase option when running regasm.

regasm ComInterOpExample.dll /codebase

4> What is codebase mentioned above?Ans - codebase - gets the location of assembly as specified originally for example System.Reflection.Assembly obj.

HiI have created COM+ dll and im trying to use that dll in vb6. I am trying to register that dll in vb6 "Regsvr32 <dll path>" but it is giving error DLL Failed no entry point found. Can anyone please help me with this.

Thanks for the great article - it helps me a lot to create my own classes for use with windows scripting host. Before i found that article, it didnt work at foreign pc´s. I have a question about creating the GUID´s.

VS25005 creates the GUID at AssemblyInfo.cs ' GUID to be visible at com interop [assembly: Guid("22212-4430-1213-acd6-f33280cfd65fb")]

Error 1 The assembly "C:\Documents and Settings\rakesh_gunijan\Desktop\c#VB\C#ComExample\C#ComInterOp\ComInterOpExample\ComInterOpExample\bin\Release\ComInterOpExample.dll" could not be converted to a type library. Type library exporter encountered an error while processing 'ComInterOpExample.iInterface, ComInterOpExample'. Error: Element not found. ComInterOpExample

Scenario: 2Then I use same GUID for interface and class (but not of AssemblyInfo.cs)It still gives same error.

Original ScenarioBut when I use different GUId it get compile.

Scenario: Without using GUIDYou can exclude GUID attribute from your COM InterOp dll...It will work