DELPHI调用DLL函数出错

enli 2008-04-15 04:10:14
在DLL里面有一个函数,我是这样引用的:
function LIB_OPEN(ComName:PAnsiChar; EventID:longint;hWnd:Thandle):longint;stdcall; external 'DRIVER.dll';
这个是函数说明是这样的:
int __cdecl LIB_OPEN(LPCSTR ComName, int nEvtID,HWND hOwner)

然后我在程序里面这样调用的:
var temp:array[1..5] of char;
res:longint;
begin
TEMPADD :=0;
temp[1]:='C';
temp[2]:='O';
temp[3]:='M';
temp[4]:='2';
temp[5]:=char(0);
res := cct_lib_open(@temp,cct32_msg,frmmain.Handle);
end;
结果报出内存读取违例:access violation at address 007F063E.Read of address 007F07063E'
更奇怪的是:
我在后面加了输出就不报错了:修改调用如下:
TEMPADD :=0;
temp[1]:='C';
temp[2]:='O';
temp[3]:='M';
temp[4]:='2';
temp[5]:=char(0);
res := cct_lib_open(@temp,cct32_msg,frmmain.Handle);
if res <0 then
showmessage(inttostr(res));

请高手看看,我调用是不是有什么问题?我不输出的话为啥报错?
...全文
135 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
enli 2008-04-16
  • 打赏
  • 举报
回复
谢谢,一楼,已经给分!!
手指风 2008-04-16
  • 打赏
  • 举报
回复
你检查一下编译信息,是否有提示什么变量声明了没有使用(不加输出时)
如果是这样可能是由于delphi的编译器没有把这个变量编译进去.
PAnsiChar=PChar
你可以用

var
P:PChar;
begin
try
P:=StrAlloc(10);
StrPCopy(P,'COM2');
....
finally
StrDisPose(P);
end;
end;

aniugee 2008-04-16
  • 打赏
  • 举报
回复
同意楼上,或者都改成标准的stdcall方式
skylkj 2008-04-15
  • 打赏
  • 举报
回复
声明是cdecl,引用也应该是cdecl而不是stdcall吧

1,183

社区成员

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

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