在DLL中写函数
function setpw(pstr:string):string;stdcall;
var
i:integer;
x:integer;
n:integer;
begin
  x:=length(pstr);
  if x<>0 then
  begin
    for i:=1 to x do
    begin
      n:=ord(pstr[i])+28;
      if n>128 then
        n:=n-128;
      result:=result+chr(n);
    end;
  end;
end;
在程序中调用:
function setpw(pstr:string):string;stdcall;External'password.dll'
procedure ceshi();
begin
  a:=setpw(b);
end;
产生错误:Invalid Pointer operation
经测试:当b的长度n=0或者2,3的时候不会产生错误,其他的时候产生错误。
望高手指教

解决方案 »

  1.   

    你写的dll给别的语言调用时候
    最好用pchar,而不要用string
      

  2.   

    给你改好了,用pchar
    function setpw(pstr:pchar):pchar;export;
    var
    i:integer;
    x:integer;
    n:integer;
    result1:string;
    begin
      x:=length(pstr);
      result:='';
      if x<>0 then
      begin
        for i:=1 to x do
        begin
          n:=ord(pstr[i])+28;
          if n>128 then
            n:=n-128;
          result1:=result1+chr(n);
        end;
      end;
      result:=pchar(result1);
    end;function setpw(pstr:pchar):pchar;External'project1.dll'procedure TForm1.Button1Click(Sender: TObject);
    begin
      edit1.Text:=setpw(pchar('assdfsdf'));
    end;
    调试通过,嘿嘿,可以揭贴了吧
      

  3.   

    这样的, 如果用string做参数的话, 
    要在两个文件中都
    uses ShareMem;
      

  4.   

    多谢aiirii(ari-爱的眼睛) (
    又学到了新东西~