[英]Basic plugin framework
我必須為我正在開發的非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);
...
我想聽聽有關此方法的任何建議,是否有針對我要實現的目標的更好的解決方案?
顯然您以前使用過接口,但是您不知道COM的組件注冊功能嗎? 使用新的項目向導以自動化對象啟動ActiveX庫,瀏覽類型庫編輯器,查看庫運行並注冊自身時發生的情況(都在System.Win.ComServ.pas中)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.