Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access to public methods and properties inside a Delphi BPL

Tags:

module

delphi

bpl

I have an application that loads a BPL that as inside a simple form.

This form is an optional option of the main application.

The BPL loads correctly, the form is shown correctly, but I don’t know how to access the public methods and properties of the form inside the bpl.

Can anyone provide a simple example?

my code:

// Load the BPL on aplication Load
LoadPackage( 'About.bpl' );

// CAll for TForm1 inside the About.BPL
var
  AClass: TClass;
  AForm: TForm;
begin

    AClass := GetClass('TForm1');
    if AClass <> nil then
  begin
        Application.CreateForm(TComponentClass(AClass), AForm);
        AForm.Show;
    end;

// The unit TForm1 inside the BPL package
unit Unit1;

interface

uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs, StdCtrls;

type
    TForm1 = class(TForm)
        Button1: TButton;
        Label1: TLabel;
        procedure Button1Click(Sender: TObject);
    private
        { Private declarations }
    public
        { Public declarations }
        PublicMthd;
    end;

var
    Form1: TForm1;

implementation

{$R *.dfm}

Procedure TForm1.PublicMthd;
Begin
    ShowMessage('Inside call');
End;

initialization
    RegisterClass(TForm1);

finalization
    UnRegisterClass(TForm1);

end.

How can i access "PublicMthd" in Tform1 ?

like image 791
DRokie Avatar asked Dec 18 '11 23:12

DRokie


1 Answers

One of the interest of having TOptionalForm in a dynamically loaded bpl (assuming this from the "optional" bit)) is to avoid for your application to hold the definition of the TOptionalForm class specifically (it's in the unit contained in the package and only there).

That means that your application cannot know anything about it unless you use either:
- a shared base Class
- an Interface declaring the properties and methods you want to access
- some basic RTTI to access published properties and methods
- some extended RTTI to access public properties and methods (if you have D2010 or later)
- some external routines from the bpl accepting a base class parameter (or TObject/pointer) typecasting it as TOptionalForm internally.

This is very vague and general and more precision about your code would be needed to refine...

like image 55
Francesca Avatar answered Nov 15 '22 02:11

Francesca