简体   繁体   中英

Using a callback from a C-Dll in VB6

I have to import a few functions of a dll written in C into a VB6 project. I have an example written in C# but I don't really know how to do the same thing in VB6.

In C# it goes like this:

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate int someCallback(IntPtr ch, uint chL, IntPtr cbData);

[DllImport("someDLL.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern int someFunction(IntPtr con, someCallback callback, IntPtr cbData);

Everything works fine in the example when calling someFunction .

The documentation of the dll just gives me this:

typedef int(SOMEAPI_CALL * someCallback)(const unsigned char *ch,
                                         unsigned int         chL,
                                         void                *cbData)

SOMEAPI_CALL someFunction(Con*         con,
                          someCallback callback,
                          void*        cbData)

There should be a way to do the same in VB6 but I don't have that much experience with this language. Searched the web for a good time but didn't find anything that could help me.

I know how to declare functions from that dll in my project but thats that. How to convert this UnmanagedFunctionPointer thingy into VB6 code, I just don't know.

VB6 has no attributes in the way VB.NET (and C#) does, and the annotation would be unnecessary anyway. You can just pass a function pointer to a C API function via the AddressOf operator:

Declare Function someFunction Lib "someDLL" ( _
    ByVal con As Long, _
    ByVal callback As Long, _
    ByVal data As Long _
) As Long

…

Call someFunction(con, AddressOf SomeCallback, data)

But even that won't work since VB6 does not support native interop using the cdecl calling convention, it only supports stdcall. You will either need to recompile your DLL using the stdcall calling convention, or create a wrapper in C or IDL (there are some hacks using inline assembly to wrap individual cdecl calls but I wouldn't recommend using these).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM