C ++函数的VB6声明给出了“错误的DLL调用约定”。 [英] VB6 declaration for C++ function gives "Bad DLL calling convention"

查看:352
本文介绍了C ++函数的VB6声明给出了“错误的DLL调用约定”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此C ++函数的正确VB6声明是什么?

What is the correct VB6 declaration for this C++ function?

LPCWSTR* MW_ListReaders(_ULONG Context, int* NumberOfReaders);

以下内容为我提供了错误的DLL调用约定:

The following gave me "Bad DLL calling convention":

Private Declare Function ListReaders Lib "MyDLL.dll" (ByVal Context As Long, _
                                                    ByRef NumberOfReaders As Integer) As Long


推荐答案

在该C ++声明中未指定调用约定。大多数C / C ++编译器默认为 __ cdecl 。如果该函数确实使用 __ cdecl ,则您将无法在VB6中调用它:

There is no calling convention specified in that C++ declaration. Most C/C++ compilers default to __cdecl. If the function does actually use __cdecl then you will not be able to call it in VB6:

如何调用使用_cdecl调用约定的C函数


如果该函数使用_cdecl调用约定,则不可能直接在DLL中调用C函数。这是因为Visual Basic使用_stdcall调用约定来调用函数。这是一个问题,因为如果使用_cdecl,则调用函数负责清理堆栈。但是,如果使用_stdcall,则调用的函数负责清理堆栈。

It is not possible to directly call a C function in a DLL if that function uses the _cdecl calling convention. This is because Visual Basic uses the _stdcall calling convention for calling functions. This is a problem because if _cdecl is used, the calling function is responsible for cleaning up the stack. However, if _stdcall is used, the called function is responsible for cleaning up the stack.

注意:在Visual Basic中创建的.EXE文件将允许您调用已用_cdecl调用约定声明的DLL函数,而不会发生错误。只有当您尝试从Visual Basic IDE运行程序时调用此类函数时,Visual Basic才会生成以下错误:

NOTE: An .EXE file created in Visual Basic will allow you to call a DLL function that has been declared with the _cdecl calling convention without an error. It is only when you try to call such a function when running a program from the Visual Basic IDE, that Visual Basic generates the following error:

运行时错误'49 ':
错误的DLL调用约定

Run-time Error '49': Bad DLL Calling Convention

EXE版本允许您调用此类函数的事实已被Microsoft确认为错误。您不应该依赖此行为,因为在Visual Basic的将来版本中这可能会更改。

The fact that the EXE version allows you to call such functions has been confirmed to be a bug by Microsoft. You should not rely on this behavior as this might change in future versions of Visual Basic.

这篇关于C ++函数的VB6声明给出了“错误的DLL调用约定”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆