經過DLL對主程序或其它DLL作擴展的一種方法

1、原理: 經過事件來實現擴展函數

 

2、場景: DLL----A.DLL spa

                     擴展DLL---B.DLL接口

 

3、擴展實現:事件

         1A.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;

 

         2A.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;

 

         3A.DLL 中調用TService;

         a.Frame

         FService: TService;

         procedure Init;

         var

                   s: string;

         begin

                   //經過事件作擴展

                   FService.DoQueryFilter(s);

         end;

 

         4B.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;

         5A.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;

相關文章
相關標籤/搜索