DLL的定义中有一个函数
function test(var pst:pchar):integer;stdcall
var str:string ;
begin
    str:='ILOVEYOU';
    pst:=Pchar(str);//强制转换
    showmessage(pst);//显示真确。为ILOVEYOU
    result:=1;
end;调用如下
procedure TForm1.Button9Click(Sender: TObject);
var a:pchar;
    z:integer;
begin 
 a:=allocmem(8);
 z:=test(a);
 showmessage(a);
end;结果显示的是乱码 两次显示的不一样请 问是不是我写dll错了,还是调用有问题。

解决方案 »

  1.   

    当然不一样拉
    还是老问题是传值还是传址的问题啊
    dll中当然可以改pst:=Pchar(str);
    不过你赋值后pst已经不等于a拉
    真个过程你没有给a赋值当然是乱码
    dll中不要赋值用
    strpcopy(pst,str);
    这样才能传回来你的程序就算不是dll,而是再一个exe中也是一样啊
    基本功不扎实,尤其是指针的操作
      

  2.   

    现在我改成了。
    function test(var pst:pchar):integer;stdcall
    var str:string ;
    begin
        str:='ILOVEYOU';
        strpcopy(pst,str);
        showmessage(pst);//显示真确。为ILOVEYOU
        result:=1;
    end;
    在vb里调用如下
    Declare Function test Lib "ICEnginer.dll" (ByVal pst As String) As Integer
    Dim b As String
    Dim z As Integer
      b = Space(255)
      z = test(b)
      MsgBox b
    End Sub
    结果提示错误 
    如果 在dll里把 strPcopy(pst,str) 改成pst:=Pchar(str);则出现乱码
      

  3.   

    在DLL中用字符串最好是用PCHAR
      

  4.   

    function test(var pst:pchar):integer;stdcall
    var str:string ;
    begin
        str:='ILOVEYOU';
        strpcopy(pst,str);
         //pst:=Pchar(str);//强制转换
        showmessage(strpas(pst));//显示真确。为ILOVEYOU
        result:=1;
    end;