什麼是正確的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