读书人

vb调用vc生成的dll传递自定义构造体

发布时间: 2012-03-12 12:45:33 作者: rapoo

vb调用vc生成的dll,传递自定义构造体参数出错
在用vb调用vc生成的dll时,如果调用的函数参数类型为自定义构造体类型时,编译时显示dll调用错误。代码如下:
vc部分:
typedef struct{
int i;
int j;
} Item;

__declspec(dllexport) int __stdcall add(Item item)
{
int c = 0;
c = item.i + item.j;
return c;
}

EXPORTS add @1

vb部分:
Public Type ITEM
i As Integer
j As Integer
End Type

Private Declare Function add Lib "xxx.dll " (ByRef t As ITEM) As Integer

Private Sub Command1_Click()
Dim rt As Integer
Dim it As ITEM
it.i = 1
it.j = 2
rt = add(it)
End Sub

请问是什么原因?



[解决办法]
C里的INT貌似是4字节,那么在VB里就应该是LONG....

而VB里的Integer是2字节...

然后再把声明改下,返回也是LONG:

Private Declare Function add Lib "xxx.dll " (ByRef t As ITEM) As Long

试下吧.

读书人网 >VB

热点推荐