插件管理框架 for Delphi(三)

本文介绍了Delphi的插件框架(untDllManager),并给出使用举例。详细阐述了客户端组件和服务端组件的定义与使用,客户端组件从TDll派生,自动获取相关接口地址;服务端组件继承自TDll,自动获取QueryInterface地址,还给出了客户端组件使用的代码示例。

1       前言

2       插件框架(untDllManager

3       使用举例

3.1   类图

插件类图

3.2   客户端组件

TDll派生出TClientDll

根据真实的动态库接口添加相关属性;

3.2.1    组件定义

unit untClientDll;

 

interface

 

uses

  Windows, Classes, SysUtils, untDllManager, untProcDefine;

 

type

 

  EClientDllError = Class(Exception);

 

  { TClientDll

    o 继承自 TDll;

    o 自动获取 ClientInitialize 地址并保存在 ClientInitialize 属性中;

    o 自动获取 ClientInsertTrigger 地址并保存在 ClientInsertTrigger 属性中;

  }

 

  TClientDll = Class(TDll)

  private

    FClientInitialize: TClientInitialize;

    FClientInsertTrigger: TClientInsertTrigger;

    FClientGetDescription: TClientGetDescription;

    FClientSetup: TClientSetup;

    FDescription: String;

    FUseTrigger: Bool;

  protected

    procedure DoDllLoaded; override;

    procedure DoDllUnLoaded; override;

  public

    constructor Create; override;

    property ClientGetDescription: TClientGetDescription read FClientGetDescription;

    property ClientInitialize: TClientInitialize read FClientInitialize;

    property ClientInsertTrigger: TClientInsertTrigger read FClientInsertTrigger;

    property ClientSetup: TClientSetup read FClientSetup;

    property Description: String read FDescription write FDescription;

    property UseTrigger: Bool read FUseTrigger write FUseTrigger;

  end;

 

implementation

 

{ TClientDll }

 

constructor TClientDll.Create;

begin

  inherited;

  FClientInitialize := nil;

  FClientInsertTrigger := nil;

  FClientGetDescription := nil;

  FClientSetup := nil;

end;

 

procedure TClientDll.DoDllLoaded;

begin

  FClientInitialize := GetProcAddress(csClientInitialize);

  if not Assigned(FClientInitialize) then

    raise EClientDllError.Create('No found of Proc "ClientInitialize".');

  FClientInsertTrigger := GetProcAddress(csClientInsertTrigger);

  if not Assigned(FClientInsertTrigger) then

    raise EClientDllError.Create('No found of Proc "ClientInsertTrigger".');

  //可选接口,即使不存在也不报错。

  FClientGetDescription := GetProcAddress(csClientGetDescription);

  FClientSetup := GetProcAddress(csClientSetup);

  inherited;

end;

 

procedure TClientDll.DoDllUnLoaded;

begin

  inherited;

  FClientInitialize := nil;

  FClientInsertTrigger := nil;

  FClientGetDescription := nil;

  FClientSetup := nil;

end;

 

end.

 

3.2.2    组件使用

procedure TXXXXServer.LoadClientDll(const FileName: String);

//功能:加载一个ClientDll,并将相关数据传递进去

var

  Index: Integer;

  Description: String;

  UseTrigger: Bool;

  AClientDll: TClientDll;

begin

  Index := FClientDlls.Add(FileName);

  if Index < 0 then

    raise EXXXXError.CreateFmt('ClientDll "%s" 之前已经装载.', [FileName]);

  //尝试读取地址

  try

    FClientDlls[Index].Loaded := True;

  finally

    if not FClientDlls[Index].Loaded then

      FClientDlls[Index].Free;

  end;

  //初始化该Client,同时将相关信息传入

  UseTrigger := False;

  AClientDll := TClientDll(FClientDlls[Index]);

  if Assigned(AClientDll.ClientSetup) then

    AClientDll.ClientSetup(mscAppPath + 'Client/', False);

end;

3.3   服务端组件

3.3.1    组件定义

unit untServerDll;

 

interface

 

uses

  Windows, Classes, SysUtils, untDllManager, untProcDefine;

 

type

 

  EServerDllError = Class(Exception);

 

  { TServerDll

    o 继承自 TDll;

    o 自动获取 QueryInterface 地址并保存在QueryInterface属性中;

  }

 

  TServerDll = Class(TDll)

  private

    FFunctions: TObject;

    FQueryInterface: TProcQueryInterface;

  protected

    procedure DoDllLoaded; override;

    procedure DoDllUnLoaded; override;

  public

    procedure RefreshAllFunctionsPermit;

    property Functions: TObject read FFunctions write FFunctions;

    property QueryInterface: TProcQueryInterface read FQueryInterface;

  end;

 

implementation

 

uses

  untFunctionProc;

 

{ TServerDll }

 

procedure TServerDll.DoDllLoaded;

begin

  FQueryInterface := GetProcAddress(csQueryInterface);

  if not Assigned(FQueryInterface) then

    raise EServerDllError.Create('No found of "QueryInterface" Proc.');

  inherited; //此句需要放在后面

end;

 

procedure TServerDll.DoDllUnLoaded;

begin

  inherited;

  FQueryInterface := nil;

end;

 

procedure TServerDll.RefreshAllFunctionsPermit;

var

  I: Integer;

begin

  Assert(FFunctions <> nil);

  for I := 0 to TFunctionList(FFunctions).Count - 1 do

    if TFunction(TFunctionList(FFunctions)[I]).Dll = Self then

      TFunction(TFunctionList(FFunctions)[I]).Permit := Permit;

end;

 

end.

 

3.3.2    组件使用

略。

[文终]

 

 

Delphi高级辅助工具精解》,中国铁道出版社出版,作者:谭燕,赵磊,李之明。简介: Delphi作为一个面向对象程序设计的系统构建的集成工具,已经拥有了比较稳定的第方工具,借助于这些工具可以最大限度地提高相应的应用程序开发效率。本书针对这一特点, 重点对人秋IDE(集成开发环境)增强工具的GExperts、最受欢迎的第方代码编写工具CodeRush、调试工具CodeSite和系统建模工具ModelMaker做了比较全面而系统的介绍。 全书语言通俗,重点突出,实用性强,适合初、中级Delphi程序开发人员参阅。 目录:第1章 领略GExperts 1-1 序 言 1-2 GExperts的安装 1-3 Delphi IDE中的GExperts 1-4 功能组件的介绍 1-4-1 Procedure List(过程列表工具) 1-4-2 Expert Manager(专家管理器) 1-4-3 Grep search 1-4-4 Message Dialog(提示对话框制作) 1-4-5 Backup project(项目备份) 1-4-6 Clear directories(清除目录中的垃圾文件) 1-4-7 Clipboard History(剪贴板历史) 1-4-8 Favorite Files(收藏的文件) 1-4-9 Urce Export(源代码文件输出)1-4-10 Code Librarian(代码库工具)1-4-11 ASCII chart(ASCII图表) 1-4-12 Replace Components(组件替换工具) 1-4-13 Component Grid(组件网格) 1-4-14 Components To Code(产生组件的代码) 1-4-15 Editor Experts(编辑器专家) 1-5 GExperts的配置环境 1-5-1 File Location(文件及目录位置) 1-5-2 Editor Experts(代码编辑器专家)1-5-3 IDE扩展 1-5-4 Palette(面板) 第2章 初见CodeRush 2-1 初见CodeRush 2-1-1 CodeRush概述2-1-2 什么是新的内容 2-1-3 安装CodeRush 2-2 键盘模板(Key Templates) 2-2-1 键盘模板的介绍 2-2-2 使用模板生成变量2-2-3 使用模板生成方法2-2-4 设置变量 2-2-5 返回变量的自动化 2-2-6 定义程序结构 2-2-7 键盘模板的配置2-2-8 Template Coach2-2-9 Clipboard History 2-2-10 Bookmarks 2-2-11 Diagram repository 2-2-12 Flowchart 2-2-13 Samples(示例) 2-2-14 Sequence(顺序)2-2-15 Files(文件查找与显示) 2-2-16 Search Files(搜索文件)2-2-17 Statistics(统计工具) 2-2-18 尾声 第3章 CodeRush的窗体设计工具 3-1 序 言 3-2 Align Palette 3-3 Anchor Palette3-4 LOCK/Unlock Controls 3-5 Magnifier(放大器) 3-6 Quick Connect(快速连接) 3-7 Reveal Links 3-8 Show Active DataSets(显示激活的数据集)3-9 Tab Order 第4章 CodeRush的代码辅助增强工具 4-1 程序代码结构增强显示 4-1-1 配置环境 4-1-2 Block Selection(选择块) 4-1-3 Colors(颜色) 4-1-4 Jumping(跳转结构) 4-1-5 Lines(边线) 4-1-6 Options(选项)4-1-7 Timing(时间参数) 4-2 代码编写的自动化 4-2-1 Autofill(自动代码填充) 4-2-2 变量输入的自动化 4-2-3 函数输入自动化 4-2-4 过程、函数粘贴的自动化 4-2-5 注释自动化 4-2-6 结构代码输入自动化 第5章 首屈一指的调试工具CodeSite 5-1 序 言 5-2 CodeSite简介 5-2-1 CodeSite Object介绍 5-2-2 利用CSGlobalObject组件发送消息 5-2-3 利用CSObject组件发送消息 5-2-4 CSGlobalObject与CSObject组件的区别 5-3 CodeSite 调试消息发送 5-3-1 消息的类别 5-3-2 更多的消息发送方法 5-3-3 AddCheckPoint方法 5-
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值