Search code examples
c#vb6

Calling a function using C# Interop library without class defined


I have a VB6 application that i need to be able to switch a reference from one dll to another (that i am writing)

It needs to be able to call someFunction() without adding in a class

Such as

in vb6:

Dim test As SomeObject
Set SomeObject = someFunction("test")

These two dlls need to be interchangable code wise. We only want one reference to change in the resulting program to do two different things

It seems that C# does not allow interop (because of COM restrictions) of static functions; though i still need a function exposed to vb6 that fulfills this requirement


Solution

  • I found the answer; I just wasn't looking in the right place for a while.

    UnmanagedExports (Repacked)

    A very useful nuget package that exports functions with the attribute "DllExport"

    For example:

    In C#

    [DllExport]
    public static void HelloWorld() { return "Hi!" }
    

    Can be called from the VB6 application like so

    Private Declare Function HelloWorld Lib "MyCSharpDll.dll" () As String
    

    And sometime later in the code simply call it (this is if you wanted to output to like a MsgBox persay)

    MsgBox HelloWorld() 
    

    Which will simply return "Hi!" in a message box

    isn't that kind of cool?

    You can view the exports from your .dll with this command from the Developer Console

    dumpbin /EXPORTS MyCSharpDll.dll