C++ 函数的 VB6 声明给出了“错误的 DLL 调用约定";

VB6 declaration for C++ function gives quot;Bad DLL calling conventionquot;(C++ 函数的 VB6 声明给出了“错误的 DLL 调用约定;)
本文介绍了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 版本允许调用此类函数的事实已被微软确认为 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 调用约定";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Rising edge interrupt triggering multiple times on STM32 Nucleo(在STM32 Nucleo上多次触发上升沿中断)
How to use va_list correctly in a sequence of wrapper functions calls?(如何在一系列包装函数调用中正确使用 va_list?)
OpenGL Perspective Projection Clipping Polygon with Vertex Outside Frustum = Wrong texture mapping?(OpenGL透视投影裁剪多边形,顶点在视锥外=错误的纹理映射?)
How does one properly deserialize a byte array back into an object in C++?(如何正确地将字节数组反序列化回 C++ 中的对象?)
What free tiniest flash file system could you advice for embedded system?(您可以为嵌入式系统推荐什么免费的最小闪存文件系统?)
Volatile member variables vs. volatile object?(易失性成员变量与易失性对象?)