VB6 相当于 C++ 个字符数组是什么?
What's is the VB6 equivalent of a C++ char array?
我正在尝试从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
)应该没问题。
- C++数组是如何识别的
- 使用嵌套函数数组是个好主意吗?
- 将任意对象附加到数组有什么问题
- 如何检查字节数组是protobuf序列化字节数组还是普通字节数组
- 堆指针不会被分配给数组有什么原因吗
- Getter 函数,用于在 2d 数组是类的数据成员时检索 2d 数组元素
- 数组是如何通过引用、值或指针传递C++的?
- 构建对象数组是如何工作的?我被这个例子困住了
- 你不能在 void 指针上使用指针算法,那么 void 指针数组是如何工作的呢?
- C/C++ 中的常量数组和静态常量数组有什么区别
- 为什么当将 short[] 转换为字符*时,数组是反转的?
- 从一系列的evens和赔率中,新数组是持续的第一个赔率
- 数组是通过引用传递的,但是如果我只传递未存储在内存中的数组的值,会发生什么
- VB6 相当于 C++ 个字符数组是什么?
- 字符串数组[] = " " 是什么;意思是,为什么它有效?
- 传递枚举数组是什么意思
- 为什么有人告诉我数组是指针?C++中数组和指针之间的关系是什么
- c++中传递裸数组是什么意思?
- 什么时候变长数组是合法的?
- 如何编写一个函数,无论数组是什么类型都输出它