008.Delphi插件之QPlugins,服务的两种调用方法

这个QPlugins自带的DEMO,大概的意思就是,创建2个服务类,程序启动的时候注册这2个服务类。点击不同的按钮,使用不同的方法来调用这个服务。

效果界面如下

008.Delphi插件之QPlugins,服务的两种调用方法

 

 

unit Frm_Main;

interface

uses
  Winapi.Windows,
  Winapi.Messages,
  System.SysUtils,
  System.Variants,
  System.Classes,
  Vcl.Graphics,
  Vcl.Controls,
  Vcl.Forms,
  Vcl.Dialogs,
  Vcl.StdCtrls,
  QPlugins,
  qplugins_params,
  qplugins_base;

type
  TForm_Main = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

  // 定义一个接口,子类中实现接口方法
  IQMyServiceExt = interface
    [{46DD594D-0A3D-49E6-BE86-61E653EB3589}]
    procedure SayHello(AStr: string);
  end;

  // 服务扩展的实例
  TQMyServiceExt = class(TQInterfacedObject, IQMyServiceExt)
  private
    // 接口中Sayhello方法的实现
    procedure SayHello(AStr: string = ‘‘);
  public
    constructor Create; override;
  end;

  // IQMultiInstanceExtension  服务扩展多实例接口
  TQMyMultiInstanceExt = class(TQMyServiceExt, IQMultiInstanceExtension)
  protected
    function GetInstance(var AResult: IInterface): Boolean; stdcall;
  end;

var
  Form_Main: TForm_Main;

implementation

uses
  qstring;
{$R *.dfm}
{ TQMyServiceExt }

// 创建
constructor TQMyServiceExt.Create;
begin
  // 调用祖先类的Create函数
  inherited;
end;

// 接口中Sayhello方法的实现
procedure TQMyServiceExt.SayHello(AStr: string);
begin
  // 输出
  if AStr <> ‘‘ then
  begin
    Form_Main.Memo1.Lines.Add(AStr);
  end;
  Form_Main.Memo1.Lines.Add(扩展接口类名为: + ClassName);
end;

// 创建
procedure TForm_Main.FormCreate(Sender: TObject);
var
  AService: TQService;
  AExt: IQMyServiceExt;
begin
  // 注册TQMyServiceExt服务
  AService := TQService.Create(NewId, Message);
  AService.AddExtension(TQMyServiceExt.Create);
  RegisterServices(Services, [AService]);

  // 注册TQMyMultiInstanceExt服务
  AService := TQService.Create(NewId, MultiIntance);
  AExt := TQMyMultiInstanceExt.Create;
  AService.AddExtension(AExt);
  RegisterServices(Services, [AService]);
end;

{ TQMyMultiInstanceExt }

// 取服务扩展的实例
function TQMyMultiInstanceExt.GetInstance(var AResult: IInterface): Boolean;
begin
  AResult := TQMyServiceExt.Create;
  Result := true;
end;

// 按钮_直接调接口
procedure TForm_Main.Button2Click(Sender: TObject);
begin
  (PluginsManager as IQMyServiceExt).SayHello(‘‘);
end;

// 按钮_按路径调接口
procedure TForm_Main.Button1Click(Sender: TObject);
var
  AService: IQService;
begin
  AService := GetService(/Services/Message);
  (AService as IQMyServiceExt).SayHello(‘‘);
end;

// 按钮_多实例扩展
procedure TForm_Main.Button3Click(Sender: TObject);
var
  AService: IQService;
  AExt: IQMyServiceExt;
begin
  // 获取指定路径的服务实例
  AService := GetService(/Services/MultiIntance);
  AExt := AService as IQMyServiceExt;
  AExt.SayHello(Format(接口实例地址:%X, [IntPtr(AExt)]));
end;

end.

 

008.Delphi插件之QPlugins,服务的两种调用方法

上一篇:在iOS7中修改状态栏字体的颜色


下一篇:查看window用户登录日志