我用delphi写了一个dll
其中有个函数是这样的
function aaa(var pst:pchar):integer;stdcall
var str:string
begin
    str='ILOVEYOU'
    pst:=Pchar(str);//强制转换
    showmessage(pst)//显示真确。为ILOVEYOU
    result:=1;
end;
我在vb里调用的时候
declare Function aaa Lib "aaa.dll" (ByVal b As String) As Integerdim b as string;
dim z as integer;
   b=space(255);
   z=aaa(b)
   msgbox b
结果显示的乱maz = aaa(b)
结果返回的值

解决方案 »

  1.   

    VB的东西忘光了,你Delphi的代码没有问题
      

  2.   

    ByVal b As String 参数生命不正确 应该为byref 或者不写pst:=Pchar(str);最好用strPCopy函数
      

  3.   

    我知道在vb里  用   
    declare Function aaa Lib "aaa.dll" (Byref b As String) As Integer  能够真确显示,但byref b as string 和byval b as string 完全是一样的。
      

  4.   

    我用delphi写了一个dll
    其中有个函数是这样的
    function aaa(var pst:pchar):integer;stdcall
    var str:string
    begin
        str='ILOVEYOU'
        pst:=Pchar(str);//强制转换
        showmessage(pst)//显示真确。为ILOVEYOU
        result:=1;
    end;请问如何调用。
    我这样写的 但是返回的值是错误的。
     
    var a:array[0..8] of char;
        z:integer;
    begin 
     z:=aaa(a);
     showmessage(a);
    end;
    结果显示的是乱码  而与dll中本身SHOW出来得值不一致。
      

  5.   

    你这么写当然错了.pst只是指向了str的指针(实际上是@str[1]), 离开函数的时候str就不存在了,也就是实际的数据并没有copy到你传入的字符串缓冲区,而且你连传入的pst这个指针都改变了.const
      MAX_SIZE = 传出的最大缓冲区大小
    function aaa(const pst:pchar):integer;stdcall
    var str:string
    begin
        str='ILOVEYOU';
        StrPLCopy(pst, str, MAX_SIZE-1);
        showmessage(pst)//显示真确。为ILOVEYOU
        result:=1;
    end;