接口定义单元:。。
unit uInterMain;interfacetype
IMan=interface(IInterface)
  ['{F6F1A26D-7A7D-427D-820D-09B0B2F71407}']
  procedure hello;end;implementationend.接口实现单元:
unit uOprMain;interfaceuses
uInterMain,Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls,Activex, ScktComp,uDataMain;type
   TMan=class(TInterfacedObject,IMan)    public
      procedure hello;
      constructor create;
    end;implementation  constructor TMan.create;
  begin
    inherited create;
  end;  procedure TMan.hello;
  begin
    showmessage('asdf');
  end;end.
dll单元。
library pDllMain;
uses
  SysUtils,
  Classes,
  uInterMain in 'uInterMain.pas',
  uOprMain in 'uOprMain.pas';{$R *.res}
function createMan:IMan;stdcall;
begin
  result:=TMan.create;
end;exports
createMan;
begin
end.
主窗口单元。
unit uFromMain;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,uInterMain, StdCtrls,Activex, ScktComp;type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
      AMan:IMan;
    { Private declarations }
  public
    { Public declarations }
  end;var
  Form1: TForm1;  //function createMan:IMan;stdcall;external 'pDllMain.dll';implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
var
  createMan:function:IMan;stdcall;
  ALibrary:Thandle;
begin
  ALibrary:=LoadLibrary('pDllMain.dll');
  try
    if ALibrary=0 then
    exit;
    createMan:=GetProcAddress(ALibrary,PChar('createMan'));
    if createMan=nil then
    exit;
    AMan:=createMan;
  finally
    freelibrary(ALibrary);
  end;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
  AMan.hello;
end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
AMan:=nil;
end;end.运行后。。出错
EAccessViolation错误类型。
是怎么回事啊。
静态调用事可以的。  

解决方案 »

  1.   

    delphi6 開發人員指南 中應該有說到 dll 與 exe 共用接口的問題
      

  2.   

    我可是找開發人員指南上刻出来的
    我再简化一下代码吧
    dll代码。library pDllMain;
    uses
      SysUtils,
      Classes;{$R *.res}function a:integer;stdcall;
    begin
      result:=1;
    end;exports
    a;
    begin
    end.
    调用代码。var
      a:function:integer;stdcall;
      ALibrary:Thandle;
    begin
     
      ALibrary:=LoadLibrary('pDllMain.dll');
      try
        if ALibrary=0 then
        exit;
        @a:=GetProcAddress(ALibrary,PChar('a'));
        if @a=nil then
        exit;
        showmessage(inttostr(a));
      finally
        freelibrary(ALibrary);
      end;
    老是运行到if @a=nil then
        exit;
    运行exit;也就是@a=nil      晕死了
      

  3.   

    >>a:function:integer;stdcall;
    這句有問題a:=GetProcAddress(ALibrary,'a');
      

  4.   

    AMan:IMan;
    好像是这一句声明有问题。记得不能够定义接口对象吧!!
    记得不能够定义接口对象吧!!
    记得不能够定义接口对象吧!!
    记得不能够定义接口对象吧!!
    记得不能够定义接口对象吧!!
    记得不能够定义接口对象吧!!
    记得不能够定义接口对象吧!!
    记得不能够定义接口对象吧!!
      

  5.   

    这没问题。
    我上面所有的代码。。
    把动态调用dll换成静态调用就没问题了。
    怎么回事啊???????
      

  6.   

    实现代码都在DLL里面,unload以后,就会被移出程序的内存空间,你freelibrary(ALibrary);
    ,然后离开FormCreate以前,临时分配的IMan被调用_Release(),可是这时实现代码已经unload了,怎么能不出错呢?