In the previous article we saw how to call a routine from a package as well as how to dynamically load a package into the main application. In this second part of a two-part article explaining plug-ins, we are going to look at how to create a real world plug-in.
Creating an About Box Plug-in - Adding some more code (Page 4 of 4 )
In the host form, double click on the "About" menuitem and add the following code:
procedure TForm1.About1Click(Sender: TObject); begin packhandle := LoadPackage('Myplugin1.bpl'); if packhandle <> 0 then begin AClass := GetClass('TForm2'); if AClass <> nil then begin with TComponentClass(AClass).Create(Application) as TCustomForm do begin Show; Free; end; end else begin showmessage('Could not load class'); end; end else begin showmessage('Package handle empty'); end; end;
Also add the following two variables to the forms "var" section:
packhandle: HModule; AClass: TPersistentClass;
The code should be familiar to you by now. First we load the plug-in by calling the loadpackage() function, and then store the returned value in the "packhandle" variable:
packhandle := LoadPackage('Myplugin1.bpl');
Then we check to see whether the handle is empty. If it is not empty, we retrieve the class that is exposed by the package:
if packhandle <> 0 then begin AClass := GetClass('TForm2'); if AClass <> nil then begin with TComponentClass(AClass).Create(Application) as TCustomForm do begin Show; end;
The AClass variable holds the exposed class(TForm2) and is then instantiated and called "TCustomForm." This new object gives us access to all the usual methods of a normal form. One of these is "show," which as the name implies shows a form.
Then we simply use the "show" function to view the about box, and then free the TCustomForm object. I have not done this in this code, but it would be better to first check to see whether the package exists by using the fileexists() function (or any other method), and then based on that condition continue with the rest of the code.
That is how simple and easy it is to create a plug-in. Of course you could always improve the code by including some kind of interface class that will reduce the chance of errors. But this should give you enough information to start creating your own plug-ins.
Conclusion
Delphi packages are ideal for building plug-ins. They provide a great way to make your applications small in size and modular. They also make your applications more useful by letting other developers extend their capabilities.
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.