我怀疑FreeLibrary这个函数有BUG,因外我在做程序时经常碰到这样的错误,一使用FreeLibrary来释放DLL就出错,而注释掉这条语句,一切正常!

解决方案 »

  1.   

    但不释放系统资源也不是办法呀,FreeLibrary在VC中也经常用,好像不存在BUG,Windows系统中Dll这样应用也比较常见,没有问题呀?
      

  2.   

    放在这里:initialization
      LoadLibrary(...);
    finalization
      FreeLibrary(...);
      

  3.   

    borlandor可以详细说明一下吗,不胜感谢!
      

  4.   

    //Please try it!Var DLLHandle: THandle;implementionprocedure TForm1.AnyYourProcedure;
    begin
      {You can use DLLHandle here}
    end;......
    initialization
      DLLHandle := LoadLibrary(...);finalization
      FreeLibrary(DLLHandle);
    end.
      

  5.   

    问题出在dll中再调用Dll,不论对Lib1.dll的FreeLibrary操作在何处执行,或干脆注释掉,但关闭程序时就会出错!
      

  6.   

    这样可以的呀!我曾经用过的,而且在第一个DLL中包括了线程。不过只是显式调用了第二个DLL。
      

  7.   

    我第二个Dll类似vc中的函数库,里面都是公用函数,不想显示调用!能否象引用Windows Api 一样调用它?
      

  8.   

    你先用EXE隐式调用第二个DLL试一试。
      

  9.   

    ///////////完整的例子///////////unit Main;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        Edit1: TEdit;
        Edit2: TEdit;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;//  function GetInteger(I:Integer): Integer;stdcall;external 'DLLOne.dll';
    //  function GetDouble(F:Double): Double;stdcall;external 'DLLOne.dll';  TGetDouble = function (F:Double): Double; stdcall;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
    var D: Double;
        DLLHandle: THandle;
        Func: TGetDouble;
    begin  DLLHandle := LoadLibrary('DLLOne.dll');
      try
      @Func := GetProcAddress(DLLHandle, 'GetDouble');  //Edit1.Text := IntToStr(GetInteger(2));
      //D := GetDouble(2.2);
      if Assigned(@Func) then
      begin
        D := Func(2.2);
        Edit2.Text := FloatToStr(D);
      end;  finally
      FreeLibrary(DLLHandle);
      end;
    end;end.
    library DLLOne;uses
      SysUtils,
      Classes;{$R *.res}  function GetDoubleExt(F:Double): Double;stdcall;external 'DLLTwo.dll';
      function GetInt(I:Integer): Integer;stdcall;external 'DLLTwo.dll';  function GetInteger(I:Integer): Integer;stdcall;
      begin
        Result := GetInt(I);
      end;  function GetDouble(D:Double): Double;stdcall;
      begin
        Result := GetDoubleExt(D);
      end;exports
      GetInteger,
      GetDouble;begin
    end.
    library DLLTwo;uses
      SysUtils,
      Classes;{$R *.res}
      function GetDoubleExt(D:Double):Double ;stdcall;
      begin
        Result := D;
      end;
      function GetInt(I:Integer): Integer;stdcall;
      begin
        Result := I;
      end;exports
      GetDoubleExt,
      GetInt;begin
    end.
      

  10.   

    解决了,问题不在Freelibrary,而是对ShareMem单元的引用上,多谢 borlandor、 liusp 的热心帮助!
      

  11.   

    在Delphi应用Dll还要注意些什么?需要写入口函数DLLMain吗?望指教一二!