关于调用DLL文件的参数问题,十分火急 ( 积分: 100 )

8

88jian

Unregistered / Unconfirmed
GUEST, unregistred user!
本人调用一个'ECR32xp.DLL'文件里的long function ReceiveComm(lpvbuf)

uchar *lpvbuf
本人在代码里调用入下:
function ReceiveComm(Buff:pchar):Integer;stdcall;external 'ECR32xp.DLL'
// 參數的類型是否正确 ?
Rbuf: String;
r:=ReceiveComm(Rbuf);
但是出现报读地址的错误;
本人曾经换过很多类型的参数都不行,但是在VB里如下面调用就没有问题:
Declare Function ReceiveComm Lib "ecr32.dll&quot
(Buff As Any) As Integer
Type buftype
buffer As String * 2048
End Type
i = ReceiveComm(Rbuf)
请指教,可以加我QQ;51562441或MSN:you_yongjian,万分感激!!!
 
8

88jian

Unregistered / Unconfirmed
GUEST, unregistred user!
本人调用一个'ECR32xp.DLL'文件里的long function ReceiveComm(lpvbuf)

uchar *lpvbuf
本人在代码里调用入下:
function ReceiveComm(Buff:pchar):Integer;stdcall;external 'ECR32xp.DLL'
// 參數的類型是否正确 ?
Rbuf: String;
r:=ReceiveComm(Rbuf);
但是出现报读地址的错误;
本人曾经换过很多类型的参数都不行,但是在VB里如下面调用就没有问题:
Declare Function ReceiveComm Lib "ecr32.dll&quot
(Buff As Any) As Integer
Type buftype
buffer As String * 2048
End Type
i = ReceiveComm(Rbuf)
请指教,可以加我QQ;51562441或MSN:you_yongjian,万分感激!!!
 
N

newsmile

Unregistered / Unconfirmed
GUEST, unregistred user!
以下是borland的申明,真的很重要!
{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }
 
X

xianguo

Unregistered / Unconfirmed
GUEST, unregistred user!
用pchar类型作为传出参数时,在调用函数前需预先分配内存空间
Buff := StrAlloc(128);
 
8

88jian

Unregistered / Unconfirmed
GUEST, unregistred user!
楼上的兄弟,你可以就上面的问题来解释吗,由于本人很菜,多谢了,在线等待
 
X

xianguo

Unregistered / Unconfirmed
GUEST, unregistred user!
function ReceiveComm(Buff:pchar):Integer;stdcall;external 'ECR32xp.DLL';

Var
Buff : PChar;

Buff := StrAlloc(128)
//根据实际情况调整大小
ReceiveComm(Buff);
Edit1.Text := StrPas(Buff);
StrDisplose(Buff);
 
8

88jian

Unregistered / Unconfirmed
GUEST, unregistred user!
楼上的兄弟,万分感激!!! StrDisplose 是哪个单元的,他提示没有定义
 
X

xianguo

Unregistered / Unconfirmed
GUEST, unregistred user!
//对不起,写错了
StrDispose(Buff);
 
X

xianguo

Unregistered / Unconfirmed
GUEST, unregistred user!
uses SysUtils;

function ReceiveComm(Buff:pchar):Integer;stdcall;external 'ECR32xp.DLL';

Var
Buff : PChar;

Buff := StrAlloc(128)
//分配空间,根据实际情况调整大小
ReceiveComm(Buff)
//静态调用
Edit1.Text := StrPas(Buff)
//显示
StrDispose(Buff)
//回收空间
 
X

xianguo

Unregistered / Unconfirmed
GUEST, unregistred user!
也可以
function ReceiveComm(Buff:pchar):Integer;stdcall;external 'ECR32xp.DLL';

Var
Buff : Array[0..128] Of Char;

ReceiveComm(Buff);
 
C

cgzcgz

Unregistered / Unconfirmed
GUEST, unregistred user!
在代码里调用时修改为如下:
function ReceiveComm(var Buff:pchar):Integer;stdcall;external 'ECR32xp.DLL'
//
 
8

88jian

Unregistered / Unconfirmed
GUEST, unregistred user!
尊敬的xianguo:
非常感谢!现在没有报错了,但是返回的内容不对,我要的是内容信息,不是地址信息,请指教!感激不尽!你可以加我们,QQ51562441或MSN;YOU_YONGJIAN,我想问一下如过我定义如下
type buftype=record
buffer:string;
end ;
Rbuf:buftype;
r:=ReceiveComm(Rbuf);
下面如何修改:
function ReceiveComm(Buff:pchar):Integer;stdcall;external 'ECR32xp.DLL';参数如何修改
 
X

xianguo

Unregistered / Unconfirmed
GUEST, unregistred user!
我的MSN:xianguochen@hotmail.com
1、返回结果不对请检查dll中 ReceiveComm函数是否正确

2、最好定义为PChar(不要定义为String)
type buftype=record
buffer:pChar;
end ;
function ReceiveComm(Buff : buftype):Integer;stdcall;external 'ECR32xp.DLL';
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
1K
DelphiTeacher的专栏
D
I
回复
0
查看
793
import
I
顶部