dll问题,请高手指点。

prgliker 2004-03-25 08:54:56
implementation
function opencomm(port:integer):boolean;stdcall;
external 'ZComm.dll';
function closecomm(port:integer):boolean;stdcall;
external 'ZComm.dll';
function ztccomm(port:integer;resdata:pchar;inpdata:Pchar):integer;stdcall;
external 'ZComm.dll';
这是我申明部分
但在调用ztccomm()函数时出现以下错误:
"Access violation at address 00241ff9 in module 'ZComm.dll',read of address 01104000"
我在DLL中ztccomm的函数是这么写的:
function ztccomm(port:integer;resdata:pchar;inpdata:Pchar):integer;stdcall;
label lerrorwait,lwaitdata;
var
outbuff:Pchar;
buff:Pchar;
currentbag:integer;
inbuff:array[0..254] of byte;
i:integer;
restimer:integer;
param:integer;
begin
result:=-2;
restimer:=0;
outbuff:=resdata;
buff:= inpdata;
currentbag:=1;
while outbuff[3] > #0 do
begin
writecomm(1,outbuff,ord(outbuff[4]));
inc(restimer);
if restimer >3 then
begin
result :=-1;
exit;
end
else
begin
param:=readcomm(1,1000,300,Pchar(@inbuff[0]));
if param>0 then
begin
if jugeright(Pchar(@inbuff[0]),param) then
begin
if (inbuff[0]=ord('N')) and (inbuff[1]=ord('P')) then
begin
inc(outbuff,byte(outbuff[4]));
restimer:=0;
end;
end;
end;
end;
end;
writecomm(1,outbuff,ord(outbuff[3]));
param:=readcomm(1,30000,300,Pchar(@inbuff[0]));
if param >0 then
begin
lerrorwait:
if jugeright(Pchar(@inbuff[0]),param) then
begin
if (inbuff[0]=ord('O')) and (inbuff[1]=ord('K')) then
begin
while inbuff[2] >0 do
begin
NPNext(inbuff[2]);
if currentbag = inbuff[3] then
begin
inc(buff,ord(buff[3]));
inc(currentbag);
end;
for i:= 0 to inbuff[3]-1 do
buff[i]:=chr(inbuff[i]);
lwaitdata:
param:=readcomm(1,1000,300,Pchar(@inbuff[0]));
if param >0 then
begin
if not jugeright(Pchar(@inbuff[0]),param) or (inbuff[0]<>ord('O')) or (inbuff[1]<>ord('K'))then
goto lwaitdata;
end
else
begin
result:=-5;
exit;
end;
end;
NPNext(inbuff[2]);
{dispose data}
if currentbag >1 then
begin
inc(buff,ord(buff[3]));
end;
for i:= 0 to inbuff[3]-1 do
buff[i]:=chr(inbuff[i]);
//inc(buff,inbuff[3]);
result :=0;
exit;
end;
if (inbuff[0]=ord('E')) and (inbuff[1]=ord('R')) then
begin
NPNext(inbuff[2]);
for i:= 0 to inbuff[3]-1 do
buff[i]:=chr(inbuff[i]);
//inc(buff,inbuff[3]);
result :=0;
exit;
end;
if (inbuff[0]=ord('W')) and (inbuff[1]=ord('T')) then
begin
param:=readcomm(1,30000,300,Pchar(@inbuff[0]));
if param >0 then
goto lerrorwait
else
begin
result:=-4;
exit;
end;
end;
end
else
begin
param:=readcomm(1,1500,300,Pchar(@inbuff[0]));
if param >0 then
goto lerrorwait
else
begin
result:=-3;
exit;
end;
end;
end
else
begin
result:=-2;
exit;
end;
end;
其他两个函数都是可以用的。这是为什么,请高手指点,分不够再加!
...全文
62 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
prgliker 2004-03-25
  • 打赏
  • 举报
回复
谢谢老大们我做好了,还有爱的眼睛,用STRCOPY虽然很好,但它占用了很多的空间,我用指针是为节省空间的,指针只占一个地址空间的,虽然很危险,我注意点就行了,结帖!谢谢大家关心,如果有什么学习的东东,或有趣的程序请大家通知我,QQ:409869,互相进步,互相学习。
aiirii 2004-03-25
  • 打赏
  • 举报
回复
outbuff:=resdata;
buff:= inpdata;
這樣操作不是很好!

StrCopy(XXX, YYY);

太長的代碼, 沒法仔細看
marlboroywp 2004-03-25
  • 打赏
  • 举报
回复
注意一下大小写看看呢?包括函数名和参数的大小写。路过,希望对你有帮助
prgliker 2004-03-25
  • 打赏
  • 举报
回复
这个函数要不是用动态库的行式,是可以运行的。是不是不能这么调用该函数呀
procedure TForm1.N8Click(Sender: TObject);
var
P,q:Pchar;
begin
opencomm(1);
getmem(p,5);
getmem(q,4);
p[0]:=chr($1b);
p[1]:=chr($10);
p[2]:=#0;
p[3]:=#0;
p[4]:=#5;
if ztccomm(1,p,q)=0 then
showmessage(string(q))
else
showmessage('error');
freemem(p,5);
freemem(q,4);
end;

1,183

社区成员

发帖
与我相关
我的任务
社区描述
Delphi Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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