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; |
新闻热点
疑难解答