VB6 相当于 C++ 个字符数组是什么?

What's is the VB6 equivalent of a C++ char array?

本文关键字:数组 是什么 字符 相当于 C++ VB6      更新时间:2023-10-16

我正在尝试从VB6应用程序调用用C++编写的DLL。

下面是用于调用 DLL C++示例代码。

char firmware[32];
int maxUnits = InitPowerDevice(firmware);

但是,当我尝试从VB6调用它时,我收到错误bad DLL calling convention

Public Declare Function InitPowerDevice Lib "PwrDeviceDll.dll" (ByRef firmware() As Byte) As Long
Dim firmware(32) As Byte
InitPowerDevice(firmware)

编辑:C++原型:

Name: InitPowerDevice
Parameters: firmware: returns firmware version in ?.? format in a character string (major revision and minor revision)
Return: >0 if successful. Returns number of Power devices connected
CLASS_DECLSPEC int InitPowerDevice(char firmware[]);

已经很长时间了,但我认为您还需要将 C 函数更改为标准调用。

// In the C code when compiling to build the dll
CLASS_DECLSPEC int __stdcall InitPowerDevice(char firmware[]);
' VB Declaration
Public Declare Function InitPowerDevice Lib "PwrDeviceDll.dll" _
        (ByVal firmware As String) As Long
' VB Call
Dim fmware as String
Dim r  as Long
fmware = Space(32)
r = InitPowerDevice(fmware)

我不认为 VB6 支持以任何正常方式调用cdecl函数 - 可能会有黑客来做到这一点。也许你可以编写一个包装器dll,它用stdcall函数包装cdecl函数,然后只转发调用。

这些是一些黑客 - 但我还没有尝试过。

http://planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=49776&lngWId=1

http://planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=62014&lngWId=1

您需要传递指向数组内容开头的指针,而不是指向 SAFEARRAY 的指针。

也许您需要的是:

Public Declare Function InitPowerDevice Lib "PwrDeviceDll.dll" ( _
    ByRef firmware As Byte) As Long
Dim firmware(31) As Byte
InitPowerDevice firmware(0)

Public Declare Function InitPowerDevice CDecl Lib "PwrDeviceDll.dll" ( _
    ByRef firmware As Byte) As Long
Dim firmware(31) As Byte
InitPowerDevice firmware(0)

CDecl 关键字仅适用于编译为本机代码的 VB6 程序。 它永远不会在 IDE 或 p 代码 EXE 中工作。

由于您的错误是"错误的调用约定",因此您应该尝试更改调用约定。 默认情况下,C 代码使用__cdecl,IIRC VB6 有一个Cdecl关键字,您可以与Declare Function一起使用。

否则,可以将 C 代码更改为使用 __stdcall ,或使用类型信息和调用约定创建类型库 (.tlb)。 这可能比Declare Function更好,因为在定义类型库时使用 C 数据类型,但 VB6 可以很好地识别它们。

就参数类型而言,firmware() As Byte带有ByVal(不是ByRef)应该没问题。