Delphi - Formular in einer DLL

DLL

{-----------------------------------------------------------------------------
 Project:     Formular in DLL
 Description: DLL mit Aufruf des Formulares
 Author:      Michael Puff, http://michael-puff.de      
 Date:        2011-12-22
-----------------------------------------------------------------------------}

library FormDLL;

uses
  SysUtils,
  DLLForm in 'DLLForm.pas' {Form1};

{$R *.res}

procedure ShowForm; stdcall;
var
  Form: TForm1;
begin
  Form := TForm1.Create(nil);
  try
    Form.ShowModal;
  finally
    Form.Free;
  end;
end;

exports
  ShowForm;

begin
end.
{-----------------------------------------------------------------------------
 Project:     Formular in DLL
 Description: Formular Unit fuer DLL
 Author:      Michael Puff, http://michael-puff.de
 Date:        2011-12-22
-----------------------------------------------------------------------------}

unit DLLForm;

interface

uses
  Windows, Messages, Controls, Forms,
  Dialogs, StdCtrls, Classes;

type
  TForm1 = class(TForm)
    edt1: TEdit;
    btn1: TButton;
    btn2: TButton;
    procedure btn1Click(Sender: TObject);
    procedure btn2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btn1Click(Sender: TObject);
begin
  ShowMessage(edt1.Text);
end;

procedure TForm1.btn2Click(Sender: TObject);
begin
  Close;
end;

end.

Hauptanwendung

{-----------------------------------------------------------------------------
 Project:     Formular in DLL
 Description: Aufruf eines Formulares aus einer DLL
 Author:      Michael Puff, http://michael-puff.de
 Date:        2011-12-22
-----------------------------------------------------------------------------}

unit Main;

interface

uses
  Windows, SysUtils, Classes, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    btn1: TButton;
    procedure btn1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btn1Click(Sender: TObject);
type
  TShowForm = procedure; stdcall;
var
  DLLForm: THandle;
  ShowForm: TShowForm;
begin
  DLLForm := LoadLibrary('FormDLL.dll');
  if DLLForm <> 0 then
  begin
    @ShowForm := GetProcAddress(DLLForm, 'ShowForm');
    if Assigned(ShowForm) then
    begin
      ShowForm;
    end
    else
      ShowMessage(SysErrorMessage(GetLastError));
  end
  else
    ShowMessage(SysErrorMessage(GetLastError));
end;

end.

FormInDLL.zip Thu, 2011-12-22T10:25:48 UTC+0100 396K

2012-01-26T23:14:38 +0100, mail[at]michael[Bindestrich]puff.de