我在Dll里写了一个包含自定义接口(ITest)的TMyButton(继承于TButton), 用一个函数创建这种类的对象并返回成TButton类型.
    在测试程序里, 我用动态加载的方法使用这个Dll, 创建了一个TMyButton, 然后调用TMyButton的方法GetInterface取得了它的ITest接口, 发现这个接口是可用的.
    问题出现在关闭测试程序时, 等到FormDestroy(或者重载的Destroy)返回后, 弹出一个EAccessViolation异常. 我试过, 使用静态链接的方法不会出现这个异常. 不知道是不是Dll的释放,接口的释放没处理好... 请高手指点啊!!!
    这里是源代码:
//-----------------Dll部分---------------------------
library Dll;uses
  SysUtils, Classes, Controls, StdCtrls, Dialogs, Intf in 'Intf.pas';{$R *.res}
type  TMyButton=class(TButton, ITest)
  published
    procedure SayHello;
  end;{ TMyButton }  procedure TMyButton.SayHello;
  begin
    ShowMessage(self.Caption + ' says Hello !');
  end;  function Create(Owner: TWinControl): TButton;
  begin
    result := TMyButton.Create(Owner);
    result.Parent := Owner;
  end;  exports Create;
begin
end.//-------------------公共使用的文件Intf.pas--------------------
unit Intf;interface
uses Controls, StdCtrls;type  ITest=interface
  ['{6BDA0612-5842-4865-9550-154C34F8FD98}']
    procedure SayHello;
  end;
  TCreate=function(Owner: TWinControl): TButton;implementationend.//-------------------测试程序部分---------------------------------
unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Intf, StdCtrls;type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    Button: TButton;
    DllInstance: HINST;
    TheInterface: ITest;
  public
  end;var
  Form1: TForm1;implementation{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
  DllInstance := LoadLibrary('Dll.dll');
  Button := TCreate(GetProcAddress(DllInstance, 'Create'))(self);
  with Button do
  begin
    Caption := 'Tom';
    if GetInterface(ITest, TheInterface)=false then
      ShowMessage('GetInterface failed.');
  end;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
  TheInterface.SayHello;
end;procedure TForm1.FormDestroy(Sender: TObject);
begin
  if TheInterface<>nil then
    TheInterface._Release;
  if Button<>nil then
    Button.Free;
  if DllInstance<>0 then
    FreeLibrary(DllInstance);
end;end.