我必须开发我正在开发的非gui应用程序的插件系统。我将其设想为具有基本功能的核心应用程序,并且可以使用插件进行扩展。
现在,我发现可能最好的方法是将插件作为DLL,并在主机应用程序中加载它们。插件应该能够修改app core的某些部分(访问某些方法/变量),这很棘手。
我想到的是THost
类,它实现了IHost
和IHostExposed
接口。当主机加载插件时,它会将IHostExposed
传递给插件,插件可以调用方法/访问该接口中的变量。像这样:
接口声明:
unit uHostInterfaces;
interface
type
IHost = interface
['{BAFA98BC-271A-4847-80CE-969377C03966}']
procedure Start;
procedure Stop;
end;
// this intf will get exposed to plugin
IHostExposed = interface
['{1C59B1A9-EC7A-4D33-A574-96DF8F5A7857}']
function GetVar1: Integer;
function GetVar2: String;
procedure SetVar1(const AValue: Integer);
procedure SetVar2(const AValue: String);
property Var1: Integer read GetVar1 write SetVar1;
property Var2: String read GetVar2 write SetVar2;
end;
implementation
end.
主机类声明:
unit uHost;
interface
uses
Winapi.Windows, Winapi.Messages,
uHostInterfaces, uInstanceController, uSettings;
type
THost = class(TInterfacedObject, IHost, IHostExposed)
private
FVar1 : Integer;
FVar2 : String;
FWindowHandle : HWND;
FInstanceController: TInstanceController;
FSettings : TSettings;
procedure WndProc(var AMessage: TMessage);
public
constructor Create;
destructor Destroy; override;
// methods from IHost
procedure Start;
procedure Stop;
// methods from IHostExposed, which get set Var1/Var2
function GetVar1: Integer;
function GetVar2: string;
procedure SetVar1(const AValue: Integer);
procedure SetVar2(const AValue: string);
end;
implementation
...
......以及我如何使用它:
type
TRegisterPlugin = procedure(const AHostExposed: IHostExposed);
var
hdll : THandle;
RegisterPlugin: TRegisterPlugin;
host : IHost;
begin
host := THost.Create;
hdll := LoadLibrary('plugin.dll');
if hdll <> 0 then
begin
@RegisterPlugin := GetProcAddress(hdll, 'RegisterPlugin');
if Assigned(RegisterPlugin) then
begin
// call the plugin function and pass IHostExposed interface to it
// from there on, plugin can use this interface to interact with core app
RegisterPlugin(host as IHostExposed);
...
我想听到的是关于这种方法的任何建议,以及是否有更好的解决方案来实现我的目标?
答案 0 :(得分:1)
显然您以前使用过接口,但是您不知道COM的组件注册功能?使用新项目向导启动带有Automation Objects的ActiveX库,浏览类型库编辑器,看看当库运行并注册自己时会发生什么(它在System.Win.ComServ.pas中都有)