Dynamically Load Components From Packages at Run Time (cont'd)
Anatomy of a Run-Time Package
The source code for a Delphi package is very straightforward. It starts with the package name, followed by a list of compiler directives. If you start a new package, Delphi generates the compiler directives list for you, and you generally should leave them alone unless you have a good reason to change any of them. The list of required packages (usually at least vcl50, but the list can be longer) follows, and last is the list of units, which contain the source code for the components that you want to put into this new package.
The listing below contains the source code of a package called Plugin42, which we will use as the example package in this solution. It requires only the vcl50 package and contains one single unit, namely Edit42 (in Edit42.pas), for an enhanced T42Edit component.
The unit Edit42.pas contains the definition of a new component called T42Edit. The implementation of this new component is not important (let's just assume it adds some special functionality to an edit box that isn't already available, such as the fact that it's right-aligned, which is handy for numerical input).
Design-Time Support
Remember that before a component can be used at design time, it needs to be registered. That's why the Delphi Component Wizard (File | New—select the Component icon from the Object Repository) automatically generates a Register procedure. For the T42Edit, this procedure is implemented as follows:
procedure Register;
begin
RegisterComponents('DrBob42', [T42Edit]);
end;
This procedure means that the T42Edit component will be registered as a component for the Delphi Component Palette and will appear on the DrBob42 tab. Note that the code for the Register procedure is used at design time only.
Using Run-Time Packages
But enough about design-time packages, let's look at how we can make our application use run-time packages. It's very simple, actually. All you have to do is select the "Build with runtime packages" option on the Packages tab of the Project Options dialog. You can use the editbox below this option to list the individual, specific (run-time) packages you want to use at run-time.
Note that you don't need to make any source code changes to your original project files. As a consequence, there is only one way to compile the project using run-time packages: by specifying this option in the Project Options dialog (or by using the -LU<package> command-line compiler switch).
Your executable now requires the presence of the runtime packages or it won't load. So if you don't deploy all specified run-time packages, your application will not run. The technique that I show you is how to dynamically load and use run-time packages.
How do I dynamically load packages and use their components at run-time?
Write Delphi packages, dynamically load them, and create dynamic component instances in a way that can be the basis for a dynamic application plug-in architecture.
Find Out More
The T42Edit and T42Label components are implemented on Bob Swart's Web site in the Tip-of-the-Hat section.
What are your experiences with component use at Run-Time? Do you have a better way to build dynamic application plug-in architectures. Let us know in the database development discussion groups!