VB6 declaration for C++ function gives “Bad DLL calling convention”

前端 未结 2 1363
悲&欢浪女
悲&欢浪女 2020-12-22 09:56

What is the correct VB6 declaration for this C++ function?

LPCWSTR* MW_ListReaders(_ULONG Context, int* NumberOfReade         


        
相关标签:
2条回答
  • 2020-12-22 10:19

    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:

    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.

    0 讨论(0)
  • 2020-12-22 10:27

    In addition to Remy's answer, you have also got the Vb declaration slightly wrong:

    Private Declare Function ListReaders Lib "MyDLL.dll" (ByVal Context As Long, ByRef NumberOfReaders As Long) As Long
    

    "Integer" is a 2 byte integer in vb.

    0 讨论(0)
提交回复
热议问题