关于在COM中使用可选参数的研究

类别:Delphi 点击:0 评论:0 推荐:
关于在COM中使用可选参数的研究

作者:wangzhidong(Steven Bob)

创作日期:2002-3-27

在Delphi6中设计COM组件方法时,在 Parameter flags dialog 中,选择Optional选项,即输入参数为可选,现就给出如何实现可选参数实例。 

监测函数

function VarIsEmptyParam(const V: Variant): Boolean;

begin

  Result := (TVarData(V).VType = varError) and

            (TVarData(V).VError = $80020004); {DISP_E_PARAMNOTFOUND}

end;

实例

uses ComServ, Variants, Dialogs, SysUtils;

procedure TOptionalParamsDemo.GetParams(const Param1: WideString; Param2,Param3: OleVariant; Param4: Integer);

var

  s2, s3: WideString;

begin

  if VarIsEmptyParam(Param2) then

    s2 := ' empty '

  else

    S2 := ' not empty ';

  if VarIsEmptyParam(Param3) then

    s3 := ' empty '

  else

    S3 := ' not empty ';

  ShowMessage(Format('Param1[%s] Param2[%s] Param3[%s] Param4[%d]',[Param1, s2, s3, Param4]));

end;

 

procedure TForm1.Button1Click(Sender: TObject);

var

  V: Variant;

begin

  V := IDispatch(CoOptionalParamsDemo.Create());

  V.GetParams('123');

end;

 

procedure TForm1.Button2Click(Sender: TObject);

var

  AIntf: IOptionalParamsDemo;

  V: Variant;

begin

  AIntf := CoOptionalParamsDemo.Create();

 // AIntf.GetParams('', EmptyParam, 9, 4);

  V := IDispatch(AIntf);

 // V.GetParams('123', '123', '123', '123');

  V.GetParams();

end;

 

procedure TForm1.Button3Click(Sender: TObject);

var

  AIntf: IOptionalParamsDemo;

  V: Variant;

begin

  AIntf := CoOptionalParamsDemo.Create();

 // AIntf.GetParams('', EmptyParam, 9, 4);

  V := IDispatch(AIntf);

  V.GetParams('123', '123');

 // V.GetParams();

end;

 

procedure TForm1.Button4Click(Sender: TObject);

var

  V: Variant;

begin

  V := IDispatch(CoOptionalParamsDemo.Create());

  V.GetParams('123', '123', 123);

end;

procedure TForm1.Button5Click(Sender: TObject);

var

  V: Variant;

begin

  V := IDispatch(CoOptionalParamsDemo.Create());

  V.GetParams('123', 123, 123, 6);

end;

实例下载: http://www.megspace.com/computers/coreware/

本文地址:http://com.8s8s.com/it/it5606.htm