我写一个DLL的function, 两个指针的参数, 第一个是传入含值的地址, 第二个是在function处理过给值, 会在function执行过后要读取内容的值MyDll.dll
function ConveyData(pin,pout:pointer): bool; stdcall;
var
  p1,p2:PString;
  tmpstr:string;
begin
  if pin=nil then
    Exit;  p1:=PString(pin);
  p2:=PString(pout);
  tmpstr:='<custom>';
  tmpstr:=tmpstr+'<id>'+p1^+'</id>';
  tmpstr:=tmpstr+'</custom>';
  p2^:=tmpstr;
  Result:=true;
end;当我在程序中呼叫这个function时候有个问题我很疑惑, 程序如下,当执行到第13行时将会出现”Invalid pointer operation”的错误. 但是若将第11及12行批注拿掉, 也就是先使用TstringList将变量值读入一次后, 执行Memo1.Text:=s 就不会出现错误, 请问这是为什么?
01  procedure TForm1.Button1Click(Sender: TObject);
02  var
03    pstr1,pstr2:string;
04    pn:PString;
05    s:string;
06    ts:TStringList;
07  begin
08    pstr1:=Edit1.Text;
09    if ConveyData(@pstr1,@pstr2) then begin
10      s:=PString(@pstr2)^;
11      //ts:=TStringList.Create;
12      //ts.Add(s);
13      Memo1.Text:=s;
14    end;
15  end;