CSDN博客

img ocsoft

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

发表于2002/5/24 12:32:00  548人阅读

关于在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/

0 0

相关博文

我的热门文章

img
取 消
img