用DELPHI 做的 DLL 返回 PCHAR 类型,返回值一长就会出错,请教如何解决? 100酬谢!谢谢!!返回值短的时候好像没有任何问题

解决方案 »

  1.   

    你可以返回值设为String,注意在Uses 中加入 “ShareMem"
    具体:
    --------------------------------
      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
    --------------------------------
      

  2.   

    delphi的string和windows即c中的Pchar类型是完全兼容的。
    把你的成功的代码和出错的代码片断贴出来看一下!
      

  3.   

    把返回值处理一下:
    S1 := String (PChar (S1));
      

  4.   

    string是字符数组,而pchar是零终止串
    S1 := String(PChar(S1));
    这样处理一下就可以安全的把pchar转换成string
      

  5.   

    应该PCHAR 比STRING更加合适的呀
      

  6.   

    pchar指向的字串是不受长度限制的,而且是以零做结尾的,当串中有“零”则不能保证串的后面没有数据。
      

  7.   

    嗯,前几天我也碰上这个问题,其实用PCHAR跟用STRING是没有什么区别的,只是你在用的时候要注意了,注意不要把#0一块返回过来,你用一个StrPas函数就可以把它转换成为DELPHI的STRING了
      

  8.   

    PChar作参数传递时,DLL里一般不要主动申请空间,让调用的程序申请,而且要求调用程序传PChar的长度,如Windows的大多数API函数,DLL里只是对PChar指向的内容填内容就行了。如:GetSystemDirectory,GetTempPath,等等,