且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-12-25 08:58:17

在该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.