我必须为我正在开发的非 gui 应用程序开发插件系统。我将其设想为具有基本功能集的核心应用程序,并且可以通过插件进行扩展。
现在,我发现可能最好的方法是将插件制作为 DLL,并将它们加载到主机应用程序中。插件应该能够修改应用程序核心的某些部分(访问某些方法/变量),这是棘手的部分。
我的想法是制作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);
...
我想听听关于这种方法的任何建议,以及我想要实现的目标是否有更好的解决方案?