How can I create a standard DLL in VB6?

2019-06-13 15:02发布

TL:DR; How can I compile a VB6 module file into a standard DLL which I can use across multiple VB6 applications?

I am tasked with the support of multiple legacy applications written in VB6.

All of these applications make use of piece of hardware constructed by my employer. Before I came on to work for my employer, he had outsourced the work of developing a DLL for the project to a company that is no longer capable of supporting it since the individual working for THEM recently quit and no one else is capable of figuring it out.

My employer has recently upgraded our hardware, so even worse - the DLL that Company furnished us with is no longer useful either.

Further exacerbated by the fact that the company who released to us the NEW hardware did not release to us a DLL file which is capable of running in VB6.

It now falls to me to create a DLL file ( NOT a device driver ) which is capable of facilitating communications between the new ( and hopefully the old ) devices and VB6 applications.

My knowledge of VB6 is... limited, at best. I am mostly familiar with .Net and have had much success in creating DLLs in .Net, but when it comes to VB6, I know enough to get by. I'm entering into uncharted territory here.

I'm well acquainted with the HID.dll and the SetupAPI.dll P/Invokes and structs necessary to make this work, and I was even fortunate enough to stumble upon this, which had a working bit of VB6 code which facilitates read/writing to/from HIDs connected to the system. I tested this and ( with a bit of fidgeting ) it worked for our device out of the box. But that doesn't help me because I can't compile the module into a DLL file ( let alone figuring out events in VB6 and a truck load of other things, but I'm getting ahead of myself ).

I've read and tried a few different methods and while they proved promising, they didn't work.

Google has also inundated me with a lot of red herrings and been in general not very helpful.

If necessary, I would even write it in C/C++ ( though I'd rather not if there is some other way ).

So is what I am trying to do possible? Can someone direct me to some step-by-step for this sort of thing?

EDIT 1 :

To expound a bit, when I say that "they didn't work", what I mean is that in the case of the first link, the program still failed to find the function ( with an error message like "Function entry point not found" ) and in the second case I consistently and repeatedly received a memory write error when trying to call the function ( not fun ).

标签: dll vb6
2条回答
神经病院院长
2楼-- · 2019-06-13 15:32

1. After your trip to 1998 to get your copy of VB6, start a new ActiveX DLL project:

enter image description here

2. Edit Project Properties for the name of the beast.

3. Add a Class for the interface you are creating. I cleverly named the class VB6Class because the project/DLL is named VB6DLL.

enter image description here

4. Write code. I added some test methods to perform complex calculations:


Option Explicit

Public Function GetAString(ByVal index As Integer) As String
    Dim ret As String

    Select Case index
        Case 0
            ret = "Alpha"
        Case 1
            ret = "Beta"
        Case Else
            ret = "Omega"
    End Select

    GetAString = ret
End Function

Public Function DoubleMyInt(ByVal value As Integer) As Integer  
    DoubleMyInt = (2 * value)    
End Function

Public Function DoubleMyLong(ByVal value As Long) As Long 
    DoubleMyLong = (2 * value)
End Function

5. Make DLL from File menu. You may need to be running As Admin so it can register the DLL.

6. In the project which uses it, add a reference to the DLL.

Test code:

Private Sub Command1_Click()
     Dim vb6 As New VB6DLL.VB6Class

     Dim var0 As String
     Dim var1 As Integer
     Dim var2 As Long

     var0 = vb6.GetAString(0)
     var1 = vb6.DoubleMyInt(2)
     var2 = vb6.DoubleMyLong(1234)

      Debug.Print "GetAString == " & var0
      Debug.Print "DoubleMyInt == " & var1
      Debug.Print "DoubleMyLng == " & var2
End Sub

Result:

GetAString == Alpha
DoubleMyInt == 4
DoubleMyLng == 2468

Not sure what to do about the "truck load of other things".

查看更多
霸刀☆藐视天下
3楼-- · 2019-06-13 15:35

Here's a link to a way to do a standard DLL, that looks more straightforward than the links you've posted. I can say that if Mike Strong ("strongm") posts code, it works, too. You might want to have a look at it.

However, it's probably better to use COM if you're able: it's easier to set up (obviously) and it also has some standard capabilities for keeping track of the object's interface, that are built into VB6. For example, when you use the TypeOf keyword, VB6 actually makes an internal call to the object's QueryInterface method, which is guaranteed to exist as one of the rules of COM (and, if you use the keyword on a reference to a standard DLL object you'll get an error).

VB6 does "static" classes by setting the class's Instancing property to GlobalMultiUse. Warning: the "static" keyword has an entirely different meaning in VB6: static local variables' values persist between method calls.

查看更多
登录 后发表回答