Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check Variant is Unassigned with varByRef flag set?

Tags:

delphi

It is recommended to use VarIsClear to check is Variant is Null, Empty or Unassigned. But I found function can return False while variable Unassigned in debugger if varByRef flag is set. VarIsClear checking

function VarIsClear(const V: Variant): Boolean;
var
  LHandler: TCustomVariantType;
  LVarData: TVarData;
begin
  LVarData := FindVarData(V)^;
  with LVarData do
    if VType < CFirstUserType then
      Result := (VType = varEmpty) or
                (((VType = varDispatch) or (VType = varUnknown)) and
                  (VDispatch = nil))
    else if FindCustomVariantType(VType, LHandler) then
      Result := LHandler.IsClear(LVarData)
    else
      Result := False;
end;

and VType in my case is 16384 ($4000 = varByRef). How to check is it Null, Empty or Unassigned properly?

like image 518
user2091150 Avatar asked Oct 22 '25 18:10

user2091150


1 Answers

  • Check for unassigned with VarIsEmpty.
  • Check for an undefined value with VarIsClear.
  • Check for a null value with VarIsNull.

So it seems that you need VarIsEmpty since you wish to check for an unassigned variant.

According to the documentation your variant type of VT_EMPTY | VT_BYREF is invalid. This is explicitly called out in this text:

The VT_EMPTY and VT_NULL values MUST NOT be specified with the VT_BYREF bit flag.

So this means that your variant is invalid and it is not reasonable to expect anything of the RTL functions when they are presented with invalid input.

I suspect you'll need to either fix the code that serves up this invalid variant, or work around the broken code.

like image 86
David Heffernan Avatar answered Oct 24 '25 13:10

David Heffernan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!