给你一个例子
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Menus;
type
TLoadFrm = procedure(App: TApplication;
Scr: TScreen );
StdCall;
TForm2 = class(TForm)
MainMenu1: TMainMenu;
N1: TMenuItem;
N2: TMenuItem;
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
procedure N2Click(Sender: TObject);
private
DllHandle: Cardinal;
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
{$R *.DFM}
procedure TForm2.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
if DllHandle <> 0 then
FreeLibrary(DllHandle);
end;
procedure TForm2.N2Click(Sender: TObject);
var
LoadFrm: TLoadFrm;
begin
DllHandle := LoadLibrary(Pchar('F:/Component/Dllfrm/Order/prDll.dll'));
if DllHandle <= 0 then
Exit;
LoadFrm := GetProcAddress(DllHandle, PChar('LoadFrm'));
if @LoadFrm = nil then
Exit;
LoadFrm(Application, Screen);
end;
end.
2.DLL部分:
library prDll;
uses
SysUtils,
Classes,
Forms,
Windows,
Unit3 in 'Unit3.pas' {Form3},
Unit4 in 'Unit4.pas';
{$R *.RES}
Exports
LoadFrm;
var
GApplication: TApplication ;
GScreen: TScreen;
procedure InitDll(dWseason: DWORD);
begin
case dWseason of
DLL_PROCESS_ATTACH: begin
GApplication := Application;
GScreen := Screen;
end;
DLL_PROCESS_DETACH: begin
Application := GApplication;
Screen := GScreen;
end;
end;
end;
begin
DllProc := @InitDll;
InitDll(DLL_PROCESS_ATTACH);
end.
DLL 包含的窗体:
unit Unit3;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm3 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form3: TForm3;
implementation
{$R *.DFM}
procedure TForm3.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
Action := caFree;
Form3 := nil;
end;
end.
DLL 包含的公共单元(主要是函数书写):
unit Unit4;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, Unit3;
procedure LoadFrm(App: TApplication;
Scr: TScreen);
StdCall;
implementation
procedure LoadFrm(App: TApplication;
Scr: TScreen );
var
LPtr: PLongint;
begin
if Form3 <> nil then
Exit;
Application := App;
Screen := Scr;
LPtr := @Application.MainForm;
LPtr^ := Longint(App.MainForm);
Form3 := TForm3.Create(App.MainForm);
Form3.Show;
end;
end.