在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));请高手看看,我调用是不是有什么问题?我不输出的话为啥报错?

解决方案 »

  1.   

    声明是cdecl,引用也应该是cdecl而不是stdcall吧
      

  2.   

    同意楼上,或者都改成标准的stdcall方式
      

  3.   

    你检查一下编译信息,是否有提示什么变量声明了没有使用(不加输出时)
    如果是这样可能是由于delphi的编译器没有把这个变量编译进去.
    PAnsiChar=PChar
    你可以用var
      P:PChar;
    begin
     try
       P:=StrAlloc(10);
       StrPCopy(P,'COM2');
       ....
     finally
       StrDisPose(P);
     end;
    end;