1、原理: 經過事件來實現擴展函數
2、場景: 主DLL----A.DLL spa
擴展DLL---B.DLL接口
3、擴展實現:事件
1、A.DLL中 接口定義string
//接口函數定義it
TQueryFilterEvent = procedure(const AS: string) of object;io
IService = Interfacefunction
//Get and Set事件class
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;