Using C# dll from VB6

Posted on 2007-09-28
Last Modified: 2013-11-17
I have a lagecy system written in VB6 which I want to have interface via COM with a C# dll. I have read several examples on the internet and have written a test C# dll and VB6 test app to try and figure out the basics of doing this. However, even doing this test app I am running into a problem.

Here is the C# code:

using System;
using System.Text;
using System.Collections.Generic;
using System.EnterpriseServices;
using System.Runtime.InteropServices;

namespace DSICustomAPI
  public interface IDSICustomAPI
    void Init();
    string SayHello(string sName);
  public class DSICustomAPI : IDSICustomAPI
    private string sReturn;
    public DSICustomAPI() { }
    public void Init() {
      sReturn = " says Hello World";
    public string SayHello(string sName) {

      return sName + sReturn;

And the C# AssemblyInfo

using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.EnterpriseServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("DSICustomAPI")]
[assembly: AssemblyDescription("Custom API for test")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("DSICustomAPI")]
[assembly: AssemblyCopyright("Copyright ©  2007")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("b3129e2b-f231-4245-80f5-fe38b8df5534")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components.  If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
[assembly: ApplicationName("DSICustomAPI")]

// Version information for an assembly consists of the following four values:
//      Major Version
//      Minor Version
//      Build Number
//      Revision
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("")]
[assembly: AssemblyFileVersion("")]

And here is the VB6

Private Sub Command1_Click()
Dim test As DSICustomAPI.DSICustomAPI
Set test = New DSICustomAPI.DSICustomAPI

Me.Text1.Text = test.SayHello("Mike")
End Sub

I am registering the assembly by running

regasm DSICustomAPI.dll /tlb:DSICustomAPI.tlb
Which says it successfully registered and it creates the tlb file

The error appears on the 'Set test...' line within the VB6.
    Automation error
   The system cannot find the file specified

Any and all help is welcome, I am sure it is something simple and silly I am doing or not doing.
Question by:Jenslove
    LVL 2

    Expert Comment

    -2147024894 is a standard system error which means "The system cannot find
    the file specified. "
    so it really may be just a path issue.....

    Author Comment

    First, thanks for your reply.

    Yes, I did read/find that, but the dll is in the same directory as the VB6 project and I did regasm it, which as I understand creates the tlb (if run properly for it) and registers the dll for use bye applications. Shouldn't it be findable by anything once regasm is run on it?

    Also, I have tried different locations for the dll (and have run regasm against each location to make sure I had it correct), one being Windows\System32 where all dll are supposedly findable.

    I was wondering if it might be a versioning issue as I know .NET retains versions for usage, but I have a guid for the assembly and have 'hardcoded' the version to, so I don't THINK this could be the problem.

    Any other thoughts?

    Author Comment

    Also, if anyone can give me an example c# code/dll and vb6 code/exe and instructions that will create a working situation in which a c# dll is being called from a vb6 exe I will that posting the solution.

    I just need to get SOMETHING that works, I can take it from there.

    Author Comment

    One more thing, I do NOT have a strong name file for the dll, I am hoping to not have to do this and just maintain the dll in the 'working' directory. But maybe I am wrong in doing that. Do I HAVE to create a strong name file and gacutil the dll?

    Author Comment

    LOL, One MORE, one-more-thing. I went to strong name the dll and gacutil it, but the c# version is 2.0, which doesn't seem to have gacutil installed with it. So this confuses me even more.
    LVL 2

    Accepted Solution

    according to some info I found you may be required to Strong name

    another post on this subject*
    > when using gac utility i am getting error
    > "failure adding assembly to cache: attempt to install the
    > assembly without strong name"

    Only strong-named assemblies can go in the GAC.

    See the online help for instructions on strong-naming your dll.

    Author Comment


    You didn't exactly get me the solution, but you did get me to thinking in the right direction so I gave it to you.

    The problem WAS that the methods could not be found, ie vb6 couldn't find the dll to run the functions. I found that there is a /codbase option with regasm that will put the full path into the registery. Once I did regasm with this option, as in 'regasm /codebase MyClass.dll /tlb:MyClass.tlb' it all started working!!

    There was a warning about my assembly not being signed and that I should create a strong name for it, but the vb6 code was able to find everything it needed without the strong name. My guess is however that a strong named/signed assembly would be very smart even though its not going into the GAC.

    Thanks for your help!
    LVL 2

    Expert Comment

    wow,  thanks !    For the record,  I am a VB6 to C# convert,  and I thought this sounded really easy,  so I tried to do the same thing (make a C# dll,  and call it from VB6 )  as you know, not too easy.

    As it turns out,  YOU taught me how to do this.    Anyway,  I am glad you are off and running


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now