问题描述
此 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 版本允许调用此类函数的事实已被微软确认为 bug.您不应依赖此行为,因为这可能会在 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 调用约定";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!