Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Loadpackage and RegisterClasses

953 views
Skip to first unread message

Marcus Karsten

unread,
Mar 31, 2002, 1:45:43 AM3/31/02
to
Hello,

I have a problem to catch the name of a class in a package.
I know with LoadPackage you can a load a package all right.
But then I like to get the names of the Classes inside the package to
register
them, like Delphi does.
For example, I load a Test.bpl.
If I know there is a TTestClass inside, I can register them with
RegisterClass ("TTestClass");

But how to get the TTestClass, if I don´t know the name ?

Thanks for your help
- Marcus

Peter Below (TeamB)

unread,
Mar 31, 2002, 4:48:22 AM3/31/02
to

Obviously it is the *package* that should do the registering, not the
packages client. If you build the host app with run-time packages
(minimum VCL60.bpl) then it will share the class list used by
RegisterClass/FindClass with all loaded packages. Of course you would
still need to know the name of the classes to be able to create an
instance in the host app. This is a general problem with dynamically
loaded packages, the way you need to use them is different from
statically loaded packages. It requires careful planning and the design
of a sensible interface the package needs to expose to the host app.

A dynamically loaded package should have a standard, documented
interface, like a DLL. The interface consists of
a set of exported functions (exported via Exports clauses, use Name
clauses on the exports statements to force the functions to be
exported by ungarbled names). You connect to these functions via
GetProcAddress, like you would for a DLL. In the design i prefer the
package exports only one function and that returns an instance of a
standard interface type the package is required to support. All
further access (like enumerating supported classes for the user to pick
from, or creating an instance of such a class) is done via methods of
the interface.

An alternative scheme is to have a central registration package that is
used statically by application and dynamically loaded packages. The
loaded package registers the classes it has available with the
registration package and the app uses methods of the registration
package (for which it can directly Use a unit containing the stuff) to
create instances of the registered classes. The registration unit
would go considerable beyond of what RegisterClass/FindClass gives
you, i.e. you can ask it "which classes does package XYZ support" and
thus create a class instance without having to know the name of the
class up front.

There is a good article on building package-based frameworks on
http://www.xapxone.com/html/builderpattern.htm

This older post may also give you an example about the exported
function interface for a package:

<quote>
MDI child forms in a package, dynamcially loaded

Create a new project group, add a new form, set formstyle to fsMDIForm,
rename the form to MDIMainForm, add a TToolbar, add a button to it,
double-click on button to create OnClick handler. Switch to unit, add a
private field hPackage: THandle to the form, modify OnClick handler as

procedure TMDIMainForm.ToolButton1Click(Sender: TObject);
var
createProc: Procedure;
begin
If hPackage = 0 Then
hPackage := LoadPackage( ExtractFilePath( Paramstr(0) )+
'ChildPackage.bpl' );
If hPackage = 0 Then
ShowMessage('LoadPackage failed')
Else Begin
@createProc := GetProcAddress( hPackage, 'CreateChildForm' );
If Assigned( CreateProc ) Then
CreateProc
Else
ShowMessage('GetProcAddress failed');
End;
end;

Save the project as MDIMain.dpr. Go into the project options and make
sure the "compile with run-time packages" checkbox is checked.

Add a new package to the project group, add a form to the package, set
its style to fsMDIChild, name it MDIChildform, change to the unit and
modify the Implementation section like this

Procedure CreateChildform;
Begin
TMDIChildform.Create(Application).Show;
End;

exports
CreateChildform name 'CreateChildForm';

initialization
end.

Save the package as ChildPackage. Open its Options dialog and set the
BPL output directory to the projects directory (so the BPL ends up in
the same directory as the main EXE). Build all projects in the group.
Activate and run the EXE, hit button -> new child appears, no
exceptions <g>.
</quote>

--
Peter Below (TeamB)
Use the newsgroup archives :
http://www.mers.com/searchsite.html
http://www.tamaracka.com/search.htm
http://groups.google.com
http://www.prolix.be


Marcus Karsten

unread,
Apr 4, 2002, 12:07:06 AM4/4/02
to 10011...@compuxxserve.com
Thanks Peter,
I am catching now the Register Function in the BPL and call them.
But the classes are still not registered at runtime (like it is with
RegisterClasses).

->>If you build the host app with run-time packages


(minimum VCL60.bpl) then it will share the class list used by

RegisterClass/FindClass with all loaded packages. <<-
Do I understand you right, that I have to get the class list from the VCL
package and
Register every class in that list?

Or is there a way to get the RTTI from the package? How does Delphi get the

Informations if you load a package into the IDE ?

Once again, thanks for your help.

- Marcus Karsten

Marcus Karsten

unread,
Apr 4, 2002, 12:08:26 AM4/4/02
to
Thanks Peter,
I am catching now the Register Function in the BPL and call them.
But the classes are still not registered at runtime (like it is with
RegisterClasses).

->>If you build the host app with run-time packages


(minimum VCL60.bpl) then it will share the class list used by

RegisterClass/FindClass with all loaded packages. <<-
Do I understand you right, that I have to get the class list from the VCL
package and
Register every class in that list?

Or is there a way to get the RTTI from the package? How does Delphi get the

Informations if you load a package into the IDE ?

Once again, thanks for your help.

- Marcus Karsten

Peter Below (TeamB)

unread,
Apr 4, 2002, 6:20:41 AM4/4/02
to
In article <3CABDF7A...@compuserve.com>, Marcus Karsten wrote:
> I am catching now the Register Function in the BPL and call them.
> But the classes are still not registered at runtime (like it is with
> RegisterClasses).

It' not Register you need but RegisterClass(es). Each class you want to
access from outside the package has to be registered with RegisterClass. The
packages client, if build with the standard packages, can then use FindClass
to get the class reference from the class name.

>
> ->>If you build the host app with run-time packages
> (minimum VCL60.bpl) then it will share the class list used by
> RegisterClass/FindClass with all loaded packages. <<-
> Do I understand you right, that I have to get the class list from the VCL
> package and Register every class in that list?

No, you place RegisterClass calls for only the classes of interest into the
Initialization section of the units the classes are declared in.


> Or is there a way to get the RTTI from the package? How does Delphi get the
> Informations if you load a package into the IDE ?

It looks for exported functions with the name Register and calls them. But
you do not need to do it this way (and it is a bit difficult to do it this
way since you would have to analyze the package info for it).

By the way: clip your quotes or you'll get into trouble with the thread
police <g>.

0 new messages