一、原理: 通过事件来实现扩展
二、场景: 主DLL----A.DLL
扩展DLL---B.DLL
三、扩展实现:
1、A.DLL中 接口定义
//接口函数定义
TQueryFilterEvent = procedure(const AS: string) of object;
IService = Interface
//Get and Set事件
function GetQueryFilterEvent: TQueryFilterEvent;
procedure SetQueryFilterEvent(AEvent: TQueryFilterEvent);
property OnQueryFilterEvent: TQueryFilterEvent read GetQueryFilterEvent write setQueryFilterEvent;
End;
2、A.DLL 接口实现
TService = class(TObject, IService)
//实现接口中的方法,并且执行事件
FOnQueryFilter: TQueryFilterEvent;
Function GetQueryFilterEvent: TQueryFilterEvent;
Procedure SetQueryFilterEvent(AEvent: TQueryFilterEvent);
Procedure DoQuery
procedure DoQueryFilter (const AS: string);
End;
Procedure TService.DoQueryFilter(const AS: string);
Begin
if Assigned(FOnQueryFilter) then
FOnQueryFilter (AS);
End;
3、A.DLL 中调用TService;
a.Frame
FService: TService;
procedure Init;
var
s: string;
begin
//通过事件做扩展
FService.DoQueryFilter(s);
end;
4、B.DLL 中事件扩展对IService挂事件
TPlugin = function(const AService: IService): IGBQ4Plugin;
//导出函数
Exports
CreatePlugin;
function CreatePlugin(const AService: IService): IPlugin;
begin
Result := TPlugin.Create(AService);
end;
TPlugin = class(TObject, IPlugin)
protected
FService: IService;
private
procedure ResponseQueryFilterEvent (const AS: string);
protected
procedure Register; virtual;
procedure UnRegister; virtual;
end;
//Register
procedure TPlugin.Register;
begin
//对FService挂事件
FService. OnQueryFilterEvent:= ResponseQueryFilterEvent;
end;
5、A.DLL 中加载B.DLL
TPlugin = function(const AService: IService): IGBQ4Plugin;
FPluginLibHandle: THandle;
FPlugin: IPlugin;
pFunc: TPlugin;
//加载B.DLL
FPluginLibHandle := LoadLibrary(B.DLL);
//得到导出函数
@ pFunc:= GetProcAddress(FPluginLibHandle, ‘CreatePlugin‘);
//得到扩展类
FPlugin := pFunc(FService);
FPlugin.Register;