Search code examples
delphipluginsjvcl

Best way to have a return value with TJvPluginManager


im currently working in a simple program that implements plugins whith dll libraries (using the TJvPluginManager from the JVCL Framework).

So far I figure out how to use this component to handle commands but what if i want to have a return value from a custom function inside the library?. It is posible to call a certain function from the host by using the TJvPluginManager? How should I implement this?.

The hole idea is to have a function that returns a string inside each dll so it can be called by using a simple cicle. I think I can do this by hand (using dinamic loading) but I want to work with TJvPluginManager as much as possible.

Thank you for your time. John Marko


Solution

  • The way I do it is to implement an Interface in the plugin and call it from the host e.g.

    MyApp.Interfaces.pas
    
    uses
      Classes;
    
    type
      IMyPluginInterface = interface
      ['{C0436F76-6824-45E7-8819-414AB8F39E19}']
        function ConvertToUpperCase(const Value: String): String;
      end;
    
    implmentation
    
    end.
    

    The plugin:

    uses
      ..., MyApp.Interfaces;
    
    type
      TMyPluginDemo = class(TJvPlugIn, IMyPluginInterface)
      public
        function ConvertToUpperCase(const Value: String): String;
      ...
    
    implmentation
    
    function TMyPluginDemo.ConvertToUpperCase(const Value: String): String;
    begin
      Result := UpperCase(Value);
    end;
    
    ...
    

    The host:

    uses
      ..., MyApp.Interfaces;
    
    ...
    
    function TMyHostApp.GetPluginUpperCase(Plugin: TjvPlugin; const Value: String): String;
    var
      MyPluginInterface: IMyPluginInterface;
    begin
      if Supports(Plugin, IMyPluginInterface, MyPluginInterface) then
        Result := MyPluginInterface.ConvertToUpperCase(Value)
      else
        raise Exception.Create('Plugin does not support IMyPluginInterface');
    end;
    

    Hope this helps.