简体   繁体   English

在 VB6 中使用来自 C-Dll 的回调

[英]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.我必须将用 C 编写的 dll 的一些功能导入 VB6 项目。 I have an example written in C# but I don't really know how to do the same thing in VB6.我有一个用 C# 编写的示例,但我真的不知道如何在 VB6 中做同样的事情。

In C# it goes like this:在 C# 中是这样的:

[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 .调用someFunction时,示例中的一切正常。

The documentation of the dll just gives me this: dll 的文档只是给了我这个:

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.在 VB6 中应该有一种方法可以做到这一点,但我对这种语言没有太多经验。 Searched the web for a good time but didn't find anything that could help me.搜索了 web 一段时间,但没有找到任何可以帮助我的东西。

I know how to declare functions from that dll in my project but thats that.我知道如何在我的项目中从 dll 声明函数,但仅此而已。 How to convert this UnmanagedFunctionPointer thingy into VB6 code, I just don't know.如何将此UnmanagedFunctionPointer东西转换为 VB6 代码,我只是不知道。

VB6 has no attributes in the way VB.NET (and C#) does, and the annotation would be unnecessary anyway. VB6 没有 VB.NET(和 C#)那样的属性,无论如何注释都是不必要的。 You can just pass a function pointer to a C API function via the AddressOf operator:您可以通过运算符将 function 指针传递给 C AddressOf ZC1C425268E68385D1AB5074

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.但即使这样也行不通,因为 VB6 不支持使用 cdecl 调用约定的本机互操作,它只支持 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).您将需要使用 stdcall 调用约定重新编译您的 DLL,或者在 C 或 IDL 中创建一个包装器(有一些黑客使用内联汇编来包装单个 cdecl 调用,但我不建议使用这些)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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