Allow development of plug-ins for your application.
By Toby Allen
Tuesday, January 09, 2001
|
Ever wanted to allow an easy way for other application developers to provide plug-ins for your application? Well if you have, COM provides an easy solution. Other options are available, but by using COM interfaces you make it as easy as possible for developers using other languages to provide these plugins. By way of example imagine you have developed a word processor, and want to allow other application developers to provide DLLs or applications with the ability to save your files in other formats. You need to provide this facility and because there could be any number of plugins wishing to do this, it has to be a very general solution. To allow this, all you need to do is to define an Interface in your application that other developers can implement. In Delphi we can define this interface as an automation (COM) object. Follow these steps. Add an automation object to your application and define the methods and properties of your interface. Delphi will also generate a CoClass that implements this interface in your application. You have two choices for this CoClass, either you can use it to implement the different file formats you application saves to natively or you can raise an error in it to inform people it is not implemented and cannot be used. Also you need to provide some mechanism for registering a plug-in with your application. In the sample code I have provided a class that an application can create and call a method to register itself with my application. This register method provides information on the format description, the class GUID and the file extension. With this information we can completely integrate it into our application.Creating a plug-in. To create a plug in from another Delphi app you simply have to follow these simple instructions. First create a new application, or if you wish an ActiveX Library. Next import the type library of the DLL or exe with the interface defined in it. Then add an automation object and name it something suitable, such as TSaveFormatXML.
Now fill in the methods that Delphi generates for you to finish your plugin. NB. If you wish to test this in Visual Basic its even easier! All you need to do is create a class (non private) and type "Implements ITSaveinFormat" and the VB IDE will provide you with access to the methods of the class.Sample Code to call plugins. In the sample code included with this article, I have provide an interface to implement and two plugins that can register themselves with the application. I didn't get it to do anything like actually save a file, but it returns a string saying what plugin it is. For those of you who don't want to download the code, I've placed a small sample below. On startup my application, loads the values of all the plugins into a listview. When you double click on a row in the listview, the corresponding object is created and its Save method called. |
|
|
That's all that's needed. Read the readme.txt file before using. Also compile and run the main project before opening the plugin projects. |
Comment on this article in the forums