调用DLL的函数时怎样传递数组参数?

tangliang01 2008-11-04 09:44:46
下面是VB调用C的WIN32动态连接库的程序,Source2字串传递正确,Source1作为数组怎么也传递不正确,请各位大虾指点。

VB程序:

Private Declare Function test Lib "F:\XUXI\C++6练习\test\Debug\test.dll" (ByRef Source1() As Long, ByVal Source2 As String) As Boolean
Private Sub Command1_Click()
Dim Source1(1 To 6) As Long, Source2 As String

Source1(1) = 1: Source1(2) = 2: Source1(3) = 3: Source1(4) = 4: Source1(5) = 5: Source1(6) = 6:
Source2 = "Basic"
ret = test(Source1, Source2)
If ret = True Then
End
Else
rt = MsgBox("Dll函数错误", vbOKOnly)
End
End If
End Sub


WIN32动态连接库:
// test.cpp

#include "StdAfx.h"
#include <stdio.h>
#include "test.h"
void succode(double Source1[],char *Source2);

BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}

EXPORT BOOL CALLBACK test(double Source1[],char *Source2)
{
succode(Source1,Source2);
return TRUE;
}

void succode(double Source1[],char *Source2)
{
int i;
for(i=0;i<6;i++)
printf("%20.12f",Source1[i]);
}

test.def:
;test.def:Declares the module parameters for DLL.
LIBRARY "test"
DESCRIPTION 'test test'
EXPORTS
;Explicit export can go here
test

//test.h

#define EXPORT extern "C" __declspec(dllexport)

EXPORT BOOL CALLBACK test(double Source[],char *Source2);


...全文
243 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
一条晚起的虫 2008-11-20
  • 打赏
  • 举报
回复
先两边都用long或者byte看看
zhinangren 2008-11-20
  • 打赏
  • 举报
回复
我也在学,数组传递是有问题,4楼兄弟能给个例子吗?
非常感谢!
tangliang01 2008-11-08
  • 打赏
  • 举报
回复
我用如下的语句定义过
VB:
Private Declare Function test Lib "F:\XUXI\C++6练习\test\Debug\test.dll" (ByRef Source1() As double, ByVal Source2 As String) As Boolean

c:
EXPORT BOOL CALLBACK test(double *Source1,char *Source2)

Watch1里观察Source1[0]、Source1[1]等还是不正确。对于任何变量只要VB里用ByVal传值方式其参数就能正确传递,
只用ByRef地址方式参数就不能正确传递,但对于数组只能用ByRef,我在C里试了多种方法都没传递成功哦。希望有人能指点一下。
sys0004 2008-11-05
  • 打赏
  • 举报
回复
EXPORT BOOL CALLBACK test(double *Source1, int nSource1Size,char *Source2)
tangliang01 2008-11-05
  • 打赏
  • 举报
回复
我也用如下的语句定义过,结果还是一样
Private Declare Function test Lib "F:\XUXI\C++6练习\test\Debug\test.dll" (ByRef Source1() As double, ByVal Source2 As String) As Boolean

再增加一个数字个数的参数试试,什么意思?单个数字变量我想应该能传输吧,我试试看!
evileagle 2008-11-04
  • 打赏
  • 举报
回复
你在vb里定义的source1是long,在dll函数入口类型是double怎么可能对啊?
sys0004 2008-11-04
  • 打赏
  • 举报
回复
参数直接传指针,在增加一个数字个数的参数试试。
VB里的Long对应VC里的double类型?

15,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧