Toflidium.com Articles

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.

  • In the type library Delphi generates you'll notice you are provided with a default interface for your automation "object". Delete this! Since we are going to use this class to implement an interface that already exists, we don't need this default interface.
  • Now go to the root of the type library tree and on the uses tab, show all type libraries and add the library that has the interface.
  • Next select the CoClass and on the implements tab, right click, add an interface, and select the interface from the list.
  • Click refresh implementation, you should find that all the methods of the interface have been added to the implementation unit produced. Add the Typelibrary .pas file for the implemented interface to your uses list and that's it.
  • In Delphi 4 you'll also need to remove the declaration in the generated class of the original default interface created.

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.


procedure TForm1.LVPluginsDblClick(Sender: TObject);
var PlugIn : ITSaveinFormat; //Declaration of variable as Implemented interface. 
SaveMsg : String; //return message 
begin
  if assigned(LVPlugins.selected) then
  begin
    With LVPlugins.Selected do
    begin
      {String value of guid needs to be converted to a TGuid ,
      type cast return as interface we want }
      PlugIn := createcomObject(StringtoGuid(LVPlugins.selected.caption))
      			as ITSaveinformat;
      {Call the Save method on ITSaveInFormat interface}
      SaveMsg := Plugin.Save('MyFilename.txt','Some Text');
      {Destroy}
      Plugin := nil;
      Showmessage(SaveMsg);
    end;
  end;
end;

    That's all that's needed.

    Sample Code Download

    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

    About Us | Site Map | Contact Us | ©2003 Toby Allen - Toflidium Software