我使用Delphi7开发的程序,现遇到如下奇怪现象 1、公共单元定义结构: 
  PUserInfo = ^TUserInfo; 
  TUserInfo = record 
    UserID: Integer; 
    UserName: string; 
  end; 2、主程序声明变量 UserInfo:TUserInfo; 
现在向第一个DLL传递@UserInfo 且在其中可以对UserInfo进行赋值,释放此DLL,UserInfo中数据正常 
然后由第一个DLL调用第二个DLL,并将指针传递给第二个DLL,在第二个DLL中对UserInfo进行赋值,在释放DLL之前UserInfo中数据正常,但释放后主程序中UserInfo结构丢失且出错,此时跟踪UserInfo中的UserName或UserID值时显示“inaccessible value” 请问这是怎么回事?有什么办法解决?

解决方案 »

  1.   

    先保存结构和数据,然后在传递给DLL
      

  2.   

    你仔细查一下UserInfo的生命周期,肯定是生命周期完了,你还使用就会出现“inaccessible value”
      

  3.   

    DLL中不要用string传递变量。改成数组或PChar.
      

  4.   

    如果非要用string的话,看看Delphi生成DLL后delphi的提示:{ 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. }
      

  5.   

    DLL中不要用string传递变量。改成数组或PChar.
    因为Windows不支持Pascal中的字符串类型 
      

  6.   


    在第二个DLL中引用主程序的UserInfo结构内容正常,一旦对其赋值,UserInfo结构就丢失,在第一个DLL中如何操作都没问题,真是怪事生命周期如何判断???
      

  7.   


    谢谢你,可能是这个问题,因为我又跟踪了一下,integer类型数据存在,string类型的丢失