delphi4写的dll在vb6中调用出错!(200分)

S

steve

Unregistered / Unconfirmed
GUEST, unregistred user!
我用delphi4写了如下的dll,在vb6中调用,如果不传参数,没有问题,一旦传参数就会提示非法操作,该dll导致无效页面错误.程序如下:
library testdll2;
function test(var x:integer):integer;stdcall;
begin
showmessage('The param is:'+inttostr(x));
test:=x+1;
end;

exports
test index 1 name 'test';
begin
end.

在vb中调用如下:
Private Declare Function test Lib "f:/hong/testdll2.dll"
(ByVal x As Integer) As Integer
Private Sub Command1_Click()
Dim x As Integer
Dim y As Integer
y = Val(Text1.Text)
MsgBox "The param in vb is:" + Str(y)
x = test(y)
MsgBox "The feedback is:" + Str(x)
Text1.Text = Str(x)
End Sub
希望各位大虾能尽快帮我,很急!
 
B

Blade

Unregistered / Unconfirmed
GUEST, unregistred user!
你应该在Test函数的声明中去掉“ByVal”。ByVal用于值参的传递,而你在
Delphi中定义的Test实现函数中使用的是变参(var)。用了ByVal后x=test(y)
实际上将y的地址作为值参传给了test,导致错误。
可参见MSDN中"DLL 的调用约定错误(错误 49)"中的有关说明。
 
Z

z_cd

Unregistered / Unconfirmed
GUEST, unregistred user!
VB 中用 ByVal 还是比较正规的(建议)。
最好将 Delphi 中你写的代码改成如下:
Library testdll2;
Function test(x:integer):integer;
stdcall;
begin

... │
└─ 这里去掉 var
 
J

Jams

Unregistered / Unconfirmed
GUEST, unregistred user!
Delphi参数传递格式与VB不同!
 
Y

yanghaijun

Unregistered / Unconfirmed
GUEST, unregistred user!
同意Z_cd的看法。
 
S

steve

Unregistered / Unconfirmed
GUEST, unregistred user!
谢谢各位的回答,这里我应该接受Z_CD的答案了,虽然BLADE兄的
分析极是,但是提出的方案却不可行(经我验证),可是我的问题还
没有得到圆满解决,所以只好再等等。(更为了节约,不好意思,嘻嘻
因为我是入门级delphi迷,得分的机会较少,为了能多问些问题,
只好出此下策)
另一个问题是,我在dll的函数中定义了14个实型变量,在vb中
调用后只传过去了8个,另外6个莫名其妙地为0,还希望各位能
再次帮帮我,谢谢!等我问题解决了,再算总帐,ok?
 
B

Blade

Unregistered / Unconfirmed
GUEST, unregistred user!
你还是把写的函数、调用的地方摘录一些放上来,要不然,实在不知道具体是什么
情况。(另,前面我说的方法经我验证是好的,值传递还是地址传递要看你具体的
应用而定)
 
S

steve

Unregistered / Unconfirmed
GUEST, unregistred user!
function T(X1,Y1,X2,Y2,X3,Y3,T0,Ta,cp,k,dx,dy,BT,QM,E:real):real;stdcall;
var
N1,N2,N3,N4:integer;
begin
showmessage(floattostr(X1)+#13+floattostr(y1)+#13+floattostr(x2)+
#13+floattostr(y2)+#13+floattostr(x3)+#13+floattostr(y3)+
#13+floattostr(t0)+#13+floattostr(ta)+#13+floattostr(cp)+
#13+floattostr(k)+#13+floattostr(dx)+#13+floattostr(dy)+#13+floattostr
(bt)+#13+floattostr(qm));
......
end;

exports
T index 1 name 'temper';

以下是vb中的声明:
Private Declare Function T Lib "f:/hong/caldll.dll" Alias "temper"
(ByVal X1 As Single, ByVal Y1 As Single, ByVal X2 As Single, ByVal Y2
As Single, ByVal X3 As Single, ByVal Y3 As Single, ByVal t0 As
Single, ByVal ta As Single, ByVal cp As Single, ByVal k As Single,
ByVal dx As Single, ByVal dy As Single, ByVal bt As Single, ByVal qm
As Single, ByVal e As Single) As Single
Private Sub Command1_Click()
Text1.Text = Str(T(2, 3, 0, 0, 10, 20, 20, 20, 1.2, 0.1, 0.1, 0.1,
0.0008, 4000, 40))
End Sub
我让dll显示了一下传过去的数据,如果参数是整型
则传过去的是该整数的二进制数取反的结果,如:test(1),则dll中得到
的数是-65535,test(2)则-65534,但是返回值却是正确的.
而以上的函数,传了14个实数的参数,可是在dll中得到的值我就看不出
是什么了,而且只有前8个参数有值,后边的6个全是0,这种情况有办法解决吗?
要不就传一个数组过去,可是在vb中如何传数组,我却不知道,希望你
能帮我找到一个很好的解决办法,谢谢!
 
Z

z_cd

Unregistered / Unconfirmed
GUEST, unregistred user!
问题是这样的:
Delphi 中的 real类型是所有实型的统称,缺省情况下表示8个字节长
的Double类型, 而Single类型是4个字节。所以你在Delphi中传的参数VB没
法正确读出。
建议您将Delphi代码中的 real 改成 Single,再重新生成Dll试试:
function T(X1,Y1,X2,Y2,X3,Y3,T0,Ta,cp,k,dx,dy,BT,QM,E:single)
:single;stdcall;
传数组的话,是不是掩饰了各个变量的含义,程序的可读性变差了?..
 
Z

z_cd

Unregistered / Unconfirmed
GUEST, unregistred user!
致steve:
我也是入门级啊,所以你惜分的感觉我也能体会。不过你一下出200分
问这个问题,一定是很急了,呵呵。
作为入门级的朋友,我劝您多挤些时间研究一下语言的基本核心,比
如传值与传址的区别、各种基础数据类型的格式、函数调用格式、类的三
大特性等等,在细的方面不妨多钻钻牛角尖,不求快要求精。如果自己通
过钻研解决了问题,不知道有多兴奋。也不会出200分问这样的问题啦 :)
感谢您接受我的建议,我会把赚到的 1/3 的分数还给你的噢,呵呵,拿回扣了..
 
B

Blade

Unregistered / Unconfirmed
GUEST, unregistred user!
<font color="red" style="楷体_GB2312">呵呵,同意。</font>
 
S

steve

Unregistered / Unconfirmed
GUEST, unregistred user!
致Z_CD:
非常感谢Z_CD兄,Z_CD兄不仅帮我解决了问题,还教我如何进一步学习,实在是位
良师益友,小弟是非计算机专业大四的学生,再加上先天较笨,不知道Z_CD以上的建议
该从何处学起,希望Z_CD兄能给推荐几本书以及提供其他相关的帮助,小弟非常希望能
拜您为师,不知Z_CD兄意下如何?
下面发工资了:Z_CD兄150大分,Blade兄50大分如何?Blade兄是第一个回答我问题
的,而且命中要害,所以给他50分.
 
S

steve

Unregistered / Unconfirmed
GUEST, unregistred user!
多人接受答案了。
 

Similar threads

顶部