I've been able to design a front-end for a C DLL, calling the DLL functions from the VB program but I'm having trouble calling the VB functions from the C DLL. Maybe I should have put this in the C forum, but I don't know.

If I have this in a VB module:

Public Declare Function callback Lib "C:\c\dll\mydll" (ByVal cbAddress As Long)

Public Sub cb()
    MsgBox "Called back."
End Sub

and callback AddressOf cb is used for the even of a button click or something. In a C DLL I would use this:

EXPORT void __stdcall callback(long cbAddress){
    typedef void (__stdcall *funcp)();
    funcp VBCallback;
    VBCallback = (funcp)cbAddress;

Well, the DLL compiles, and the VB program runs, but when I try to call the callback function, it says bad DLL calling convention. I tried taking the address by pointer as well (since I was able to pass values by pointer in the past, I figured it would work this time also) to no avail. And I commented out all the code in the function to see if that was the problem, but it wasn't--it's the VB program calling the callback function, which is using a "bad convention." The only difference I can see between the working code and the broken code, is that I'm using a Long data type, and that I'm not returning a value (which shouldn't matter...right?)
I used DUMPBIN /EXPORTS to check the exports of the DLL and I get:

1    0 00001190 callback
2    1 00001190 callback@4

I checked the data types for VB6 and it says a long is 4 bytes, and if I compile the DLL using int, long, or short the DLL export always asks for 4 bytes in parameters, so I don't know what I'm doing wrong... Any ideas? Maybe I'll link to this in the C forum as well

7 Years
Discussion Span
Last Post by winrawr

Know what the problem was?

Public Declare Function callback Lib "C:\c\dll\mydll" (ByVal cbAddress As Long)

The function in the DLL doesn't return a value, so it should have been

Public Declare Sub callback Lib "C:\c\dll\mydll" (ByVal cbAddress As Long)

you would think VB could have just told me that since I didn't add "As ___" to the end of it, but, oh well.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.