请教一个DLL兼容性的问题??? 100分(100分)

  • 主题发起人 主题发起人 rob
  • 开始时间 开始时间
R

rob

Unregistered / Unconfirmed
GUEST, unregistred user!
我用DELPHI写了一个非常简单的DLL做试验,让VB调用,<br>发现用除了PCHAR类型以外得所有类型做为传递参数在VB就能调用成功,可是一用PCHAR,<br>VB调用的时候老是关闭性错误!<br>请那位高人指点迷经(函数调用一定得有字符参数)<br>程序如下:<br>Delphi DLL<br>**********************************<br>library Project1;<br>uses<br>&nbsp; SysUtils,<br>&nbsp; Classes;<br><br>{$R *.res}<br>function robtest(VAR buf:pchar):integer;stdcall;<br>&nbsp; begin<br>&nbsp; &nbsp;buf:='rob';<br>&nbsp; &nbsp;result:=2003;<br>&nbsp; end;<br><br>exports<br>&nbsp; &nbsp;robtest;<br>begin<br>end.<br>*************************************************<br>VB 调用程序<br>Private Declare Function robtest Lib "c:/windows/desktop/dll/test/Project1.dll" (ByVal buf As String) As Long<br><br>Private Sub Command1_Click()<br>Dim a As Integer<br>Dim bb As String <br>a = robtest(bb)<br>Text1.Text = Text1.Text + bb + ";" + Str(a)<br>End Sub<br>**************************************************<br><br>
 
临时变量作用域的问题,函数执行完就被释放了
 
问题自己已经解决了<br>是DELPHI中function里用Var 声明的参数,在VB调用里不能加Byval<br>delphi里给PCHAR参数赋值时用,用个string类型变量倒一下<br>修改后<br>elphi DLL<br>**********************************<br>library Project1;<br>uses<br>&nbsp; SysUtils,<br>&nbsp; Classes;<br><br>{$R *.res}<br>function robtest(VAR buf:pchar):integer;stdcall;<br>var<br>tempL:string;<br>begin<br>&nbsp; &nbsp;temp:='rob';<br>&nbsp; &nbsp;buf:=pchar(temp);<br>&nbsp; &nbsp;result:=2003;<br>end;<br><br>exports<br>&nbsp; &nbsp;robtest;<br>begin<br>end.<br>*************************************************<br>VB 调用程序<br>Private Declare Function robtest Lib "c:/windows/desktop/dll/test/Project1.dll" (buf As String) As Long<br><br>Private Sub Command1_Click()<br>Dim a As Integer<br>Dim bb As String <br>a = robtest(bb)<br>Text1.Text = Text1.Text + bb + ";" + Str(a)<br>End Sub<br>**************************************************<br>
 
学习,还不结贴呀。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
613
import
I
I
回复
0
查看
780
import
I
后退
顶部