|
[color=#FF0000] Dynamically call the BPL form[/color][code=Delphi(Pascal)]
unit UfrmMain;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TfrmMain = class(TForm)
btn1: TButton;
btn2: TButton;
btn3: TButton;
procedure btn1Click(Sender: TObject);
procedure btn2Click(Sender: TObject);
procedure btn3Click(Sender: TObject);
private
function CreateFormByClassName(const ClassName: string): TCustomForm;
function CreateDataModuleByClassName(const ClassName: string): TDataModule;
procedure UnloadAddInPackage(ModuleInstance: HMODULE);
public
{Public declarations}
end;
var
frmMain: TfrmMain;
implementation
var
ModuleInstance1: HMODULE;
{$R *.dfm}
//------------------------------------------------ ---------------
//Load Package dynamically
//------------------------------------------------ ---------------
procedure TfrmMain.btn1Click(Sender: TObject);
begin
ModuleInstance1 := LoadPackage('Package1.bpl');
end;
//------------------------------------------------ ---------------
//Bring out Form1 in Package
//------------------------------------------------ ---------------
procedure TfrmMain.btn2Click(Sender: TObject);
var
frm: TcustomForm;
begin
frm := CreateFormByClassName('TForm1');
try
frm.ShowModal;
finally
frm.Release;
end;
end;
//------------------------------------------------ ---------------
//Release Package
//------------------------------------------------ ---------------
procedure TfrmMain.btn3Click(Sender: TObject);
begin
UnloadAddInPackage(ModuleInstance1);
end;
//------------------------------------------------ ---------------
//Custom function-CreateFormByClassName(), create Form
//------------------------------------------------ ---------------
function TfrmMain.CreateFormByClassName(const ClassName: string): TCustomForm;
var
AClass: TPersistentClass;
begin
AClass := GetClass(ClassName);
if AClass=nil then exit;
Result := TComponentClass(AClass).Create(Application) as TCustomForm;
//Or Result := TCustomForm(TComponentClass(AClass).Create(Application));
end;
//------------------------------------------------ ---------------
//Custom function-CreateDataModuleByClassName(), create data module
//------------------------------------------------ ---------------
function TfrmMain.CreateDataModuleByClassName(const ClassName:string): TDataModule;
var
AClass: TPersistentClass;
begin
Result := nil;
AClass := GetClass(ClassName);
if AClass=nil then exit;
Result := TComponentClass(AClass).Create(Application) as TDataModule;
end;
//------------------------------------------------ ---------------
//Custom function-UnloadAddInPackage(), release Package
//------------------------------------------------ ---------------
procedure TfrmMain.UnloadAddInPackage(ModuleInstance: HMODULE);
var
i: Integer;
M: TMemoryBasicInformation;
begin
for i := Application.ComponentCount-1 downto 0 do
begin
VirtualQuery(GetClass(Application.Components[i].ClassName), M, SizeOf(M));
if (ModuleInstance = 0) or (HMODULE(M.AllocationBase) = ModuleInstance) then
Application.Components[i].Free;
end;
//The following two functions should be called as long as one of them
UnRegisterModuleClasses(ModuleInstance); //Directly unregister Package
UnloadPackage(ModuleInstance); //Indirect logout, call finalization in Package
end;
end.
[/code] |
|