• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4631
  • Last Modified:

Using C# dll from VB6

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.
  • 5
  • 3
1 Solution
-2147024894 is a standard system error which means "The system cannot find
the file specified. "
so it really may be just a path issue.....
JensloveAuthor Commented:
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?
JensloveAuthor Commented:
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.
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

JensloveAuthor Commented:
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?
JensloveAuthor Commented:
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.
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.

JensloveAuthor Commented:

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!
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

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now