大家好!动态连接库的工程文件:library DLLP;uses
  ShareMem,
  SysUtils,
  Classes,
  dllu in 'dllu.pas';{$R *.res}exports
  openserialport,closeserialport,SendHex,readdata;begin
end.
函数实现单元如下:unit dllu;interfaceuses
  ShareMem,Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;var
  hserialport : thandle;
  procedure openserialport(temp:string);stdcall;
  procedure closeserialport;stdcall;
  procedure SendHex(sss: String);stdcall;
  function readdata:string;stdcall;implementation
procedure openserialport(temp:string);stdcall;
var
  cc:tcommconfig;
begin
  hserialport := createfile(pchar(temp),generic_read or generic_write,0,nil,
                        open_existing,0,0);
  if (hserialport = invalid_handle_value) then
    begin
      messagebox(0,'打开串口时发生错误!','',mb_ok);
      exit;
    end;
  getcommstate(hserialport,cc.dcb);
  cc.dcb.BaudRate := cbr_9600;
  cc.dcb.ByteSize := 8 ;
  cc.dcb.Parity := noparity;
  cc.dcb.StopBits := onestopbit;
  if not setcommstate(hserialport,cc.dcb) then
    begin
      messagebox(0,'不能设置串口!','',mb_ok);
      closehandle(hserialport);
      exit;
    end;
  showmessage('串口已经打开!');
end;procedure closeserialport;stdcall;
begin
  closehandle(hserialport);
  showmessage('串口已经关闭!');
end;procedure SendHex(sss: String);stdcall;
var
  s,s2:string;
  buf1:array[0..50000] of char;
  i:integer;
  lw : longword;
begin
  s2:='';
  s:=sss;
  for i:=1 to  length(s) do
  begin
    if ((copy(s,i,1)>='0') and (copy(s,i,1)<='9'))or((copy(s,i,1)>='a') and (copy(s,i,1)<='f'))
        or((copy(s,i,1)>='A') and (copy(s,i,1)<='F')) then
    begin
        s2:=s2+copy(s,i,1);
    end;
  end;
  for i:=0 to (length(s2) div 2-1) do
    buf1[i]:=char(strtoint('$'+copy(s2,i*2+1,2)));
  if (hserialport = 0) then
    exit;
  writefile(hserialport,buf1,(length(s2) div 2),lw,nil);
end;function readdata:string;stdcall;
var
  tempstring,tmpStr,wchb:string;
  inbuff:array[0..2047] of char;
  pstr:pchar;
  bytesread,dwerror:longword;
  cs:tcomstat;
  tmpArray:array[0..4096] of Byte;
  i: DWORD;
begin
  clearcommerror(hserialport,dwerror,@cs);
  if cs.cbInQue > sizeof(inbuff) then
    begin
      purgecomm(hserialport,purge_rxclear);
      exit;
    end;
  readfile(hserialport,inbuff,cs.cbInQue,bytesread,nil);
  pStr:=inbuff;
  tmpStr:=string(pStr);
  Dec(PStr);
  for i:=0 to cs.cbInQue-1 do
    begin
      inc(PStr);
      tmpArray[i]:=Byte(PSTR^);
      wchb:=wchb+IntToHEX(Ord(tmpArray[i]),2);
    end;
  tempstring := wchb;
  result:= tempstring;
end;end.
我的问题是:为什么调用readdata时总出错啊????????错误提示是:“Invalid Pointer Operation”。我是没办法了啊啊!!!!!!!!!!!!请高手前辈指导指导!!!分不够在加!!

解决方案 »

  1.   

    function readdata:string;stdcall;
    dll中不要用string,用pchar代替
      

  2.   

    pStr:=inbuff;
      tmpStr:=string(pStr);
    改為:
     tempStr := copy(inbuff, 1, cs.cbInQue);
      

  3.   

    是string的错误,你试试用pchar
      

  4.   

    请那位前辈帮俺改一下readdata这个函数好吗?
      

  5.   

    tmpArray:array[0..4096] of Byte;
    改為:
    tmpArray: Byte;Dec(PStr);
      for i:=0 to cs.cbInQue-1 do
        begin
          inc(PStr);
          tmpArray[i]:=Byte(PSTR^);
          wchb:=wchb+IntToHEX(Ord(tmpArray[i]),2);
        end;
    改為:
      for i:=1 to length(tmpstr) do
        begin
          tmpArray :=Byte(StrToInt('$' + tmpstr[i]));
          wchb:=wchb+IntToHEX(Ord(tmpArray),2);
        end;我只是看代碼改, 具體還要測試下