var
  InputS,OutPutS:string;
begin
  OutPutS := DLL.func1(InputS);
end;
上述写法,DLL链接库的函数的输入和输出都是string类型,好像有些问题,如果改成pchar,大家有什么简单的方法吗?

解决方案 »

  1.   

    如果使用pchar的话,返回值可以转换成string吗?
    -----------
    可以的吧?
      

  2.   

    使用PCHAR挺方便的 
    返回值可以直接赋给STRING
      

  3.   

    使用Pchar是为了兼容问题,方便不同的语言调用
    如果是用D写的dll,D调用,用string也是可以的string与pchar可以相互转换
    var
     s:string;
     p:pchar;
    begin
     s:='AAA';
     P:=pchar(s);
     showmessage(p);{AAA}
     p:='BBB';
     s:=P;
     showmessage(s);{BBB}
    end;
      

  4.   


    谢谢。不需要分配内存吗?
    DLL中,
    function FUNC1(a:pchar):Pchar;
    var
     s:string;
    begin
      s := a;
      s := s + 'abc';
      result := pchar(s);
    end;Delphi的调用代码:
    var
     s:string;
     p1,p2:pchar;
    begin
     s := '123';
     p1 := pchar(s);
     p2 := FUNC1(p1);
     s := p2;
    end;DLL和调用的代码,都没有new内存的地方,上述的写法是否有隐患呢?
      

  5.   


    支持这个。
    作为交换,String就要谨慎,
    返回值,或者在参数中使用 var S : AnsiString这样的情况,在DLL中给String赋值,而在Exe中使用这些值,就肯定有问题,而使用Const S : AnsiString这样的,某些使用下没问题,大部分使用下存在问题。
    建议使用PAnsiChar,而且建议谁申请谁销毁。
    举个例子:Windows标准API
    function GetCurrentDirectoryA(nBufferLength: DWORD; lpBuffer: PAnsiChar): DWORD; stdcall;
    尽管也可以写成
    function GetCurrentDirectoryA: PAnsiChar;
    但是M$为什么没写成这样,仔细想一下就明白了
      

  6.   

    新建 dll 的时候已经说的很清楚了吧
    { Important note about DLL memory management: ShareMem must be the
      first unit in your library's USES clause AND your project's (select
      Project-View Source) USES clause if your DLL exports any procedures or
      functions that pass strings as parameters or function results. This
      applies to all strings passed to and from your DLL--even those that
      are nested in records and classes. ShareMem is the interface unit to
      the BORLNDMM.DLL shared memory manager, which must be deployed along
      with your DLL. To avoid using BORLNDMM.DLL, pass string information
      using PChar or ShortString parameters. }
      

  7.   

    DLL,导出函数字符参数,还是用PCHAR吧
      

  8.   

    Use后边加个shareMem吧!  注:放在第一位!
    好用,专门解决这个问题的