We help IT Professionals succeed at work.

Using C# dll from VB6

4,672 Views
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);
  }
  [ComVisible(true)]
  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("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

And here is the VB6

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

test.Init
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.
Error:
    -2147024894
    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.
Comment
Watch Question

Commented:
-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

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 1.0.0.0, so I don't THINK this could be the problem.

Any other thoughts?

Author

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.

Author

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?

Author

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.
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
DaveMon

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!

Commented:
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

Dave

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.