我在Unit1中定义了一个函数
function ReadData(StrData:Pchar):smallint;然后在Unit2中调用它:var
   s:string;
   buf:array[0..255] of char;
begin   
readdata(buf);
s:=copy(buf,1,1);
edit1.text:=s;
end;
编译通过,运行时到“edit1.text:=s;”处就出错如果把这行删除或换成“showmessage(s);”则成功。请各位大侠看看是什么问题。

解决方案 »

  1.   

    function ReadData(var StrData:Pchar):smallint;
      

  2.   

    Converts null-terminated string to a Pascal string.UnitSysUtilsCategorybackward compatibility routinesfunction StrPas(const Str: PChar): string;DescriptionThis function is provided for backwards compatibility only. To convert a null terminated string to a Pascal-type string, use a typecast or an assignment.用上面的函数:我在Unit1中定义了一个函数
    function ReadData(StrData:Pchar):smallint;然后在Unit2中调用它:var
       s:string;
       buf:array[0..255] of char;
    begin   
    readdata(buf);
    s:=copy(buf,1,1);
    edit1.text:=StrPas(s);
    end;
      

  3.   

    function ReadData(StrData:pchar):smallint;
    var
        offset:smallint;
        readlen:byte;
        readbuf:array[0..255]of byte ;
        recv:integer;
        i:integer;
        s:string;
    begin
        offset:=66;
        readlen:=22;
        recv:=ReadSle4428(offset,readlen,readbuf);
        //ReadSle4428是一个读卡程序,将地址偏移量是offset,
        //长度为readlen的数据读入readbuf中
        for i:=0 to recv-1 do
            s:=s+chr(readbuf[i]);
        StrCopy(strdata, PChar(s));
        readdata:=recv;
    end;
      

  4.   

    to   Drate(鸟窝里的虫) :
      我用你方法试了,也不行
      

  5.   

    我把ReadSle4428换成FillChar,一切正常,可能是读卡函数有点问题。
      

  6.   

    你的代码没错呀, 还是从其它方面找问题吧。//***下面是我的测试代码
      procedure readdata(strData: Pchar);
      begin
        FillMemory(strData, 256, 96);
      end;
    Var
      s:string;
      buf:array[0..255] of char;
    begin
      readdata(buf);
      s:=copy(buf,1,1);
      edit1.text:=s;
    end;
      

  7.   

    错误信息:
    access violation at 0x00453969:read of address 0x000002f9