3

我目前正在使用一个简单的程序来实现带有 dll 库的插件(使用 JVCL 框架中的 TJvPluginManager)。

到目前为止,我弄清楚了如何使用这个组件来处理命令,但是如果我想从库中的自定义函数获得返回值呢?是否可以使用TJvPluginManager 从主机调用某个函数?我应该如何实现这个?

漏洞的想法是有一个函数在每个 dll 中返回一个字符串,以便可以通过使用简单的 cicle 来调用它。我想我可以手动完成(使用动态加载),但我想尽可能多地使用 TJvPluginManager。

感谢您的时间。约翰·马尔科

4

1 回答 1

6

我这样做的方法是在插件中实现一个接口并从主机调用它,例如

MyApp.Interfaces.pas

uses
  Classes;

type
  IMyPluginInterface = interface
  ['{C0436F76-6824-45E7-8819-414AB8F39E19}']
    function ConvertToUpperCase(const Value: String): String;
  end;

implmentation

end.

插件:

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;

...

主人:

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;

希望这可以帮助。

于 2010-11-25T12:40:14.323 回答