我用一个程序动态调用dll
    LoadLibrary('..\dll\project2.dll');
时 出现错误  错误如下 :
---------------------------
Debugger Exception Notification
---------------------------
Project Project1.exe raised exception class EApplicationError with message 'Fatal error: Cannot create application object in a shared object or library.'. Process stopped. Use Step or Run to continue.
---------------------------
OK   Help   
---------------------------
帮忙解决阿? 马上给分!

解决方案 »

  1.   

    遗憾,我对E文不是很懂,楼主把动态调用的代码粘出来吧
    DLL的入口也很重要
      

  2.   

    可能这时有别的程序已经再调用这个Dll
      

  3.   

    var
      aparam:TDllParams;
    begin
      aparam := TDllParams.Create;
      aparam.PrjServerIp := '10.160.16.142';
      aparam.PrjServerPort := 211;
      DoDllFunction('E:\dlyx\Sources\temp\dll\Project2.dll','cccccc',aparam);
    end;end.function DoDllFunction(aDllFile:String;aFunName:String;aParam:TDllParams):TDllResult;
    var
      DLLHandle : THandle;
      aFunction : FDllFunction;
    begin
      DLLHandle := LoadLibrary( PChar(aDllFile) );//执行这句话出错了!!!!!!!
      @aFunction := GetProcAddress(DllHandle,PChar(aFunName) );
      if not Assigned(aFunction) then
        raise Exception.Create('导入插件错误!');
      result := aFunction(aParam);
    end;
      

  4.   

    library Project2;{ 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. }uses
      SysUtils,
      Classes,
      Unit1 in 'Unit1.pas' {frmTemp},
      UserUtil in '..\..\Pub\UserUtil.pas',
      AuditMng in '..\..\Pub\AuditMng.pas',
      AuditMngClient in '..\..\Pub\AuditMngClient.pas',
      AuditObjs in '..\..\Pub\AuditObjs.pas',
      ClientConfig in '..\..\Pub\ClientConfig.pas',
      ClientSystem in '..\..\Pub\ClientSystem.pas',
      DirectoryDialog in '..\..\Pub\DirectoryDialog.pas',
      DynDll in '..\..\Pub\DynDll.pas',
      EntityBaseObj in '..\..\Pub\EntityBaseObj.pas',
      GlobalConst in '..\..\Pub\GlobalConst.pas',
      ServerConfig in '..\..\Pub\ServerConfig.pas',
      ServerSystem in '..\..\Pub\ServerSystem.pas',
      SqlConfig in '..\..\Pub\SqlConfig.pas',
      SysMng in '..\..\Pub\SysMng.pas',
      SysMngClient in '..\..\Pub\SysMngClient.pas',
      SysObjs in '..\..\Pub\SysObjs.pas',
      TestMainliFrm in '..\..\Pub\TestMainliFrm.pas' {TestMainliForm},
      AppConst in '..\..\Pub\AppConst.pas';
    function cccccc(adllparam:tdllparams):tdllresult;stdcall;
    var
      frmTemp: TfrmTemp;
    begin
      frmTemp := TfrmTemp.Create(nil);
      frmTemp.iniform(adllparam);
      frmTemp.Visible := true;
    end;
    {$R *.res}
     exports
        cccccc;
    begin
    end.
      

  5.   

    停止explorer.exe进程,然后重启动一个新的explorer.exe进程。
    估计是前次调用DLL没有释放。
      

  6.   

    aparam:TDllParams;
    是個類的實例, 在dll與主exe間,不能這樣傳遞 對象指針的, 經常會有問題的, 參考:
    http://borland.mblogger.cn/aiirii/posts/12450.aspx
      

  7.   


    关键代码没贴出来
    frmTemp.iniform(adllparam);干啥了?
      

  8.   

    哦看错了,呵呵你这样试试把Dll中的参数换成
    function cccccc(Classaddr:Integer):tdllresult;stdcall;
    begin
     ……
     frmTemp.iniform(Tdllparams(Pointer(Classaddr)));
     ……
    end;然后在调用的代码里把类的地址传进去
    function DoDllFunction(aDllFile:String;aFunName:String;classaddr:Integer):TDllResult;……
    DoDllFunction('E:\dlyx\Sources\temp\dll\Project2.dll','cccccc',Integer(Tdllparams));……
      

  9.   

    function DoDllFunction(aDllFile:String;aFunName:String;aParam:TDllParams):TDllResult;
    var
      DLLHandle : THandle;
      aFunction : FDllFunction;
    begin
      try
        DLLHandle := LoadLibrary( PChar(aDllFile) );//执行这句话出错了!!!!!!!
        @aFunction := GetProcAddress(DllHandle,PChar(aFunName) );
        if not Assigned(aFunction) then
          raise Exception.Create('导入插件错误!');
        result := aFunction(aParam);
       except
         ShowMessage(IntToStr(GetLastError())); // see the error code..
       end;
    end;