在Dll中重用两个窗体,用其中一个窗体上的buttonclick事件来显示另一个窗体为什么会出错!代码如下:
.........................................主程序,用来调用DLL
unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;type
  Ttextdll = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  textdll: Ttextdll;implementation
procedure ShowAboutDlg;stdcall;far;external'Project1.dll';//声明调用
{$R *.dfm}procedure Ttextdll.Button1Click(Sender: TObject);
begin
  ShowAboutDlg;//调用的过程
end;
.....................................................Dll的单元1,有三个button,为什么第三个会错
unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls;type
  TForm1 = class(TForm)
    Button1: TButton;
    Label1: TLabel;
    Image1: TImage;
    Button2: TButton;
    Button3: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  Form1: TForm1;
  procedure     ShowAboutDlg;export;//输出
implementationuses Unit2;{$R *.dfm}
procedure   ShowAboutDlg;//函数
begin
    Form1:=TForm1.Create(Application);
    Try
            Form1.ShowModal;    finally
              Form1.Free;
    end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
  close;//没有报错
end;procedure TForm1.Button2Click(Sender: TObject);
begin
  showmessage('howoldareyou');//没有报错,正常使用
end;procedure TForm1.Button3Click(Sender: TObject);export;
begin
  form2.show;//这里出错,为什么form2出不来呢,且还报Access violation at address 003A1ADD in moudoue 'project1.dll'
end;end.
.................................................DLL 的单元2。这里窗体什么也没做,只是让能够显示
unit Unit2;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;type
  TForm2 = class(TForm)
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  Form2: TForm2;implementation{$R *.dfm}end.
.......................................................................dll的工程单元
library Project1;uses
  SysUtils,
  Classes,
  Unit1 in 'Unit1.pas' {Form1},
  Unit2 in '..\..\..\..\Program Files\Borland\Delphi7\Projects\Unit2.pas' {Form2};{$R *.res}
exports
  ShowAboutDlg;//向调用程序输出过程
beginend.

解决方案 »

  1.   

    form2有创建了,出错的那句?
    另外dll的application对象和exe的application不是一样的。
    需要传入exe的application,保存dll的application.返回时重新置回
      

  2.   

    如果我没有猜错,你调showAboutDlg时,show出form1的时候,在任务栏里面,是另外再开一个的.如果想只使用同一个,这样改:
    procedure  ShowAboutDlg(H: HWND);
    begin
      Application.Handle := H;
      Form1:=TForm1.Create(Application); 
      Try 
        Form1.ShowModal; 
      finally 
        Form1.Free; 
        Application.Handle := 0;
      end;
    end;
    主程序调用的时候:
    ShowAboutDlg(Application.Handle);procedure TForm1.Button3Click(Sender: TObject);export; 
    begin 
      form2.show;//这里出错,为什么form2出不来呢,且还报Access violation at address 003A1ADD in 
      这里报错,是因为Form2没有创建,根本没这个对象,当然会出现地址错误
    moudoue 'project1.dll' 
    end; 
      

  3.   

    你的form根本没有创建啊,dll中每个对象都要手工创建,手工释放