| |

VerySource

 Forgot password?
 Register
Search
View: 831|Reply: 3

Ask the master I can not display the form in bpl?

[Copy link]

1

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-3-7 23:30:01
| Show all posts |Read mode
The classes in the package are registered:
initialization
  RegisterClass (TForm1);
  showmessage ('Loaded successfully');
finalization
  showmessage ('Uninstall succeeded!');
  UnRegisterClass (TForm1);

Calling method:
function TForm2.CreateFormByClassName (const ClassName: string): Integer;
var
 UClass: TPersistentClass;
 UForm: TCustomForm;
begin
  Result: = mrNone;
  UClass: = GetClass (className);
  if UClass = nil then
  begin
    ShowMessage ('Class not found');
    Exit;
  end;

  UForm: = TCustomForm (TComponentClass (UClass) .Create (Application));
  ShowMessage ('The class in the package was created successfully!');
  UForm.ShowModal;
  
end;
But the form in the package just displays "class not found" instead of uclass = nil
I don't know what's going on, please answer the experts.
Reply

Use magic Report

0

Threads

2

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-9-1 05:30:01
| Show all posts
It is recommended to use in the unit where TForm1 is located

initialization
    RegisterClass(TForm1);
    showmessage('Loaded successfully');
finalization
    showmessage('Uninstall successfully!');
    UnRegisterClass(TForm1);
----------------------------------------
There is the code for RegisterClass(TForm1),
Must be in this unit, or in the referenced unit,

The package file cannot be used in the new project!
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-9-1 08:30:01
| Show all posts
[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]
Reply

Use magic Report

0

Threads

2

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-9-1 09:30:01
| Show all posts
The code upstairs is horrible and wonderful!
But I still like to solve the problem directly!
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list