library prjDLL;
{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }
uses
SysUtils,
Classes,
Windows,
Forms,
childUnit in 'childUnit.pas' {frmChild},
ChildDM in 'ChildDM.pas' {DM: TDataModule};
{$R *.res}
var
DLLApp: TApplication;
DLLScreen:TScreen;
function ShowDllForm(App:TApplication;
ACaption:String;
sc:TScreen):Longint;stdcall;
begin
Application := App;
Screen:=sc;
if not Assigned(frmChild) then
frmChild:=TfrmChild.Create(Application);
Result:=Longint(frmChild);
frmChild.Caption := ACaption;
frmChild.Show;
end;
procedure DLLUnloadProc(Reason : Integer);
begin
if Reason = DLL_PROCESS_DETACH then
begin
Application := DLLApp;//恢复
Screen:=DLLScreen;
end;
end;
exports
ShowDllForm;
begin
DLLApp := Application;
//保存 DLL 中初始的 Application 对象
DLLScreen:=Screen;
DLLProc := @DLLUnloadProc;
//保证 DLL 卸载时恢复原来的 Application
end.
unit childUnit;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, publicUnit, Grids, DBGridEh, DB, ADODB;
type
TfrmChild = class(TForm)
DataSource1: TDataSource;
DBGridEh1: TDBGridEh;
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
frmChild: TfrmChild;
implementation
{$R *.dfm}
procedure TfrmChild.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
FreeAndNil(frmChild);
//action := caFree;
end;
end.
unit MainUnit;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, Menus, publicUnit, Grids, DBGridEh, DB, ADODB;
type
TfrmMain = class(TForm)
Panel1: TPanel;
Button2: TButton;
MainMenu1: TMainMenu;
N1: TMenuItem;
Button1: TButton;
procedure Button2Click(Sender: TObject);
procedure N1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
frmArray: array of TForm;
frmCount: Integer;
public
{ Public declarations }
procedure ChildClose(var message: TMESSAGE);
message SM_CHILD_CLOSE;
end;
var
frmMain: TfrmMain;
function ShowDllForm(App:TApplication;
ACaption:String;
sc:TScreen):Longint;stdcall;external 'prjDll.dll';
implementation
{$R *.dfm}
procedure TfrmMain.Button2Click(Sender: TObject);
begin
Showmessage(IntToSTr(Self.MDIChildCount));
end;
procedure TfrmMain.N1Click(Sender: TObject);
begin
ShowDllForm(Application,'子窗体',Screen);
end;
procedure TfrmMain.FormCreate(Sender: TObject);
begin
frmCount:=0;
end;
procedure TfrmMain.ChildClose(var message: TMESSAGE);
begin
frmCount:=frmCount-1;
end;
procedure TfrmMain.Button1Click(Sender: TObject);
begin
frmArray[0].Close;
end;
end.