Form enabling Packages
To see how Delphi uses dynamically loaded runtime packages we'll create an AboutBox form that displays some kind of text to the user and has two radio buttons - the idea is to have dynamically loaded form that can return some information from the user input.
First, we'll create a form and then place it inside a Package.
The easiest way to create a new form in Delphi is to simply start Delphi and use the default one. For this purpose add one Memo component, one TBitBtn component and two radio buttons. Save the pas file as 'AboutBoxForm.pas'. Let the form look something like:
uses NOT
Let's recall that when we want to use dynamically loaded Packages we do not list the units we use in the uses clause - as we do with statically linked Packages. This is the reason why we cannot (in the application that will call/show this form) use something like:
...
AboutBoxForm:=TAboutBoxForm.Create(Application);
try
AboutBoxForm.DoSomething;
finally
AboutBoxForm.Release
end;
|
The TAboutBoxForm will be 'Undeclared identifier' - since the AboutBoxForm.pas is not added to the uses list.
Everything inside...
To solve this problem we need a method of accessing a routine inside the AboutBoxForm that creates, shows, frees the form and returns some data to spice up the problem a little.
First the 'Execute' routine inside the AboutBoxForm.pas (in the implementation section) should look like:
function Execute
(const abText, rbCaptTrue, rbCaptFalse: string):
boolean;
begin
Result:=False;
AboutBox:= TAboutBox.Create(nil);
try
with AboutBox do begin
AboutMemo.Text := abtext;
rbAboutTrue.Caption := rbCaptTrue;
rbAboutFalse.Caption := rbCaptFalse;
rbAboutTrue.Checked:=True;
ShowModal;
Result:= rbAboutTrue.Checked;
end;
finally
AboutBox.Release;
end;
end;
|
The Execute function takes three parameters: abText is to be displayed in the memo component, the rbCaptTrue, rbCaptFalse string values are used to set the Captions of radio buttons. This routine, also, creates the AboutBoxForm, shows it modally and returns a boolean value. The value returned is True if the user has picked the first radio button (default) and false otherwise.
Now, since a Package is nothing more than a better DLL we need to export this function so it can be called dynamically:
{at the end of the unit}
exports
{Win32 export names are case sensitive!}
Execute;
end.
|
To the Package
Creating a package is easy, just follow the steps explained in here: "New runtime package". Note: skip the third step to leave the package empty for the AboutBoxForm. To store the AboutBoxForm inside a Package you simply click the Add button on the Package Editor window and select the AboutBoxFor.pas unit. Save the package as TestPack.dpk. After selecting the compile button the TestPack.bpl runtime Package is created. Be sure to select the 'Runtime package' option.
The Package Editor should look like: