[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Using C# dll from VB6

Posted on 2007-09-28
8
Medium Priority
?
4,596 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.
0
Comment
Question by:Jenslove
  • 5
  • 3
8 Comments
 
LVL 2

Expert Comment

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

Author Comment

by:Jenslove
ID: 19994184
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?
0
 

Author Comment

by:Jenslove
ID: 19994193
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.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:Jenslove
ID: 19994207
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?
0
 

Author Comment

by:Jenslove
ID: 19994237
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.
0
 
LVL 2

Accepted Solution

by:
DaveMon earned 2000 total points
ID: 19999390
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"
>[/color]

Only strong-named assemblies can go in the GAC.

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

http://msdn.microsoft.com/library/d...ystrongname.asp
0
 

Author Comment

by:Jenslove
ID: 20001569
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!
0
 
LVL 2

Expert Comment

by:DaveMon
ID: 20002723
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
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
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 lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

829 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