vb.netdllvb6

Function not found inside .dll created using vb6 inside vb.net 4.8.1


I'm trying to access the function inside a VB6 created dll, but it fails stating that the name of the function cannot be found. I'm trying to access the function inside vb.net using framework 4.8.1

The VB6 code for the dll is as follows:

Name of the class module: MyTest

Public Function myFunc(sInput As String) As String
    Dim sReturn As String
    sReturn = "[test]" & sInput & "[/test]"
    
    myFunc = sReturn
End Function

The VB.net code for Form1:

Public Class Form1
    Public Declare Function myFunc Lib "C:\temp\Test DLL.dll" (sInput As String) As String
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Private Sub bTest_Click(sender As Object, e As EventArgs) Handles bTest.Click
        Me.tOutput.Text = myFunc(Me.tInput.Text)
    End Sub
End Class

The error I get: Kan ingangspunt met de naam myFunc niet vinden in DLL C:\temp\Test DLL.dll.
translation: "Cannot find entry point named myFunc in DLL C:\temp\Test DLL.dll."

I've tried to use the following command, but that did not make a change: regsrv32 C:\temp\Test DLL.dll

Here's the .dll opened in notepad, so you can see the function is there: enter image description here

What code in VB.net do I have to use to access the function myFunc?


Solution

  • I got it to work.

    There are a few things you need to do.

    First, if you are planning on working on the DLL and make changes, if you have a DLL already, make a copy of that DLL first, and don't change it.

    Next, in vb6, go to Project -> myTestDLL properties (bottom one).

    From here change the following:

    In your code, you only place the function, nothing else is needed.

    I removed the following from my project file again, its not necessary (see the other answer).

    [VBCompiler]
    LinkSwitches=/EXPORT:myFunc
    

    Next, I copied the dll from my vb6 development machine to my .net development machine.

    There I placed the DLL somewhere where I want to use it, and opened a command prompt as administrator and ran regsvr32

    The reason we use binary compatibility is so that each time we create a new version of the DLL, it keeps the guids, and you don't need to register the DLL again.

    Next, Add a reference to the DLL in .net as a COM object, and finally, the following code can be used in your project to access the function:

    Public Class Form1
        Public myTestDLL As New myTestDLL.MyTest
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        End Sub
    
        Private Sub bTest_Click(sender As Object, e As EventArgs) Handles bTest.Click
    
            Me.tOutput.Text = Me.myTestDLL.myFunc(Me.tInput.Text)
        End Sub
    End Class