006.Delphi插件之QPlugins,多服务演示
演示效果如下
演示工程,全部就一个文件,代码如下
unit Frm_Main; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, System.Rtti, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, qstring, QPlugins, qplugins_base, qplugins_params, Vcl.ExtCtrls; type TForm_Main = class(TForm) Panel1: TPanel; Button1: TButton; Memo1: TMemo; procedure FormCreate(Sender: TObject); procedure Button1Click(Sender: TObject); private {Private declarations} public {Public declarations} end; // 继承自TQService服务 TIntAddService = class(TQService) public function Execute(AParams: IQParams; AResult: IQParams): Boolean; override; stdcall; end; // 继承自TQService服务 TFloatAddService = class(TQService) public function Execute(AParams: IQParams; AResult: IQParams): Boolean; override; stdcall; end; var Form_Main: TForm_Main; implementation {$R *.dfm} // 判断参数类型 function IsIntType(AType: TQParamType): Boolean; begin Result := AType in [ptInt8, ptInt16, ptInt32, ptInt64, ptUInt8, ptUInt16, ptUInt32, ptUInt64]; end; // 判断参数类型 function IsFloatType(AType: TQParamType): Boolean; begin Result := AType in [ptFloat4, ptFloat8]; end; {TFloatAddService} function TFloatAddService.Execute(AParams, AResult: IQParams): Boolean; begin // 判断参数类型都是浮点型 if IsFloatType(AParams[0].ParamType) and IsFloatType(AParams[1].ParamType) then begin // 两个参数相加 AResult.Add('Result', ptFloat8).AsFloat := AParams[0].AsFloat + AParams[1].AsFloat; Result := True; end else Result := False; end; {TIntAddService} function TIntAddService.Execute(AParams, AResult: IQParams): Boolean; begin // 判断参数类型都是整数型 if IsIntType(AParams[0].ParamType) and IsIntType(AParams[1].ParamType) then begin // 两个参数相加 AResult.Add('Result', ptInt64).AsInt64 := AParams[0].AsInt64 + AParams[1].AsInt64; Result := True; end else begin Result := False; end; end; procedure TForm_Main.Button1Click(Sender: TObject); var AServices: IQServices; AParams, AResult: IQParams; procedure Calc; var I: Integer; begin // 注册2个服务,名字分别为TIntAddService和TFloatAddService for I := 0 to AServices.Count - 1 do begin if AServices[I].Execute(AParams, AResult) then begin Memo1.Lines.Add('通过服务 ' + AServices[I].Name + ' 计算完成。'); Break; end; end; end; begin // 创建2个参数 AParams := TQParams.Create; AResult := TQParams.Create; // 调用两个整数型相加 AServices := PluginsManager.ByPath('Services/Adds') as IQServices; AParams.Add('X', ptUInt8).AsInteger := 100; AParams.Add('Y', ptUInt8).AsInteger := 200; Calc; Memo1.Lines.Add(' 计算表达式 100+200=' + IntToStr(AResult[0].AsInt64)); AParams.Clear; AResult.Clear; // 调用两个浮点型相加 AParams.Add('X', ptFloat8).AsFloat := 100.3; AParams.Add('Y', ptFloat8).AsFloat := 20.05; Calc; Memo1.Lines.Add(' 计算表达式 100.3+20.05=' + FloatToStr(AResult[0].AsFloat)); end; // 创建 procedure TForm_Main.FormCreate(Sender: TObject); begin ReportMemoryLeaksOnShutdown := True; // 注册2个服务,名字分别为TIntAddService和TFloatAddService RegisterServices('/Services/Adds', [TIntAddService.Create(NewId, 'AddInt'), TFloatAddService.Create (NewId, 'AddFloat')]); end; end.