什么是正确的VB6声明本C++函数:VB6声明C++函数提供了“错误的DLL调用约定”
LPCWSTR* MW_ListReaders(_ULONG Context, int* NumberOfReaders);
下给了我“错误的DLL调用约定”。
Private Declare Function ListReaders Lib "MyDLL.dll" (ByVal Context As Long, ByRef NumberOfReaders As Integer) As Long
什么是正确的VB6声明本C++函数:VB6声明C++函数提供了“错误的DLL调用约定”
LPCWSTR* MW_ListReaders(_ULONG Context, int* NumberOfReaders);
下给了我“错误的DLL调用约定”。
Private Declare Function ListReaders Lib "MyDLL.dll" (ByVal Context As Long, ByRef NumberOfReaders As Integer) As Long
有在C++声明没有指定调用约定。大多数C/C++编译器默认为__cdecl
。如果函数实际使用__cdecl
,那么你将不能够调用它VB6:
How To Call C Functions That Use the _cdecl Calling Convention
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.
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:
Run-time Error '49': Bad DLL Calling Convention
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.
除了雷米的回答,你也得到了VB的声明稍有不当:
Private Declare Function ListReaders Lib "MyDLL.dll" (ByVal Context As Long, ByRef NumberOfReaders As Long) As Long
“Integer”是vb中的2字节整数。
那么_ULONG呢?它是64位吗? – Dabblernl 2013-04-30 22:14:13
否 - 它在windows.h中定义为一个32位无符号整数。 VB不支持无符号整数,所以你必须用一个有符号的32位整数来伪造它,即“As Long” – 2013-05-01 00:06:52