Re: Help on VARIANT/VT/VARTYPE
"Jess" <firstname.lastname@example.org> wrote in message
I am confused by the differences between VARIANT, VT and VARTYPE,
could someone give me some help please?
VARIANT is a structure, what is known in computer science as
"discriminated union". It has a field named "vt" (for Variant Type) to
indicate what kind of data is stored in the variant, and a union of
fields for all possible types. Possible values for "vt" field are listed
in VARTYPE enum.
In particular, I see we have
types like VARIANT_BOOL as well as VT_BOOL, what is the difference?
VT_BOOL is one of the values in VARTYPE enum, a possible type that a
VARIANT can hold.
VARIANT_BOOL is just a typedef for short, with the understanding that
the logical false is represented by zero (or VARIANT_FALSE, defined as
zero), and logical true as a bit pattern with all bits set
(VARIANT_TRUE, defined as -1 or 0xFFFF). This weird definition is for
compatibility with Visual Basic (why VB did it this way I have no idea).
One of the fields in the union inside VARIANT is named "boolVal", of
type VARIANT_BOOL. This is how one sets up a VARIANT holding a boolean
v.vt = VT_BOOL;
v.boolVal = VARIANT_TRUE;
Moreover, what is VARTYPE used for?
VARIANT::vt field is of type VARTYPE
There is also CComVariant,what is the difference between this and
CComVariant is a C++ wrapper around VARIANT (which is a plain C
structure), adding a lot of convenience methods. Perhaps more
importantly, it clears the contents of the VARIANT in the destructor (by
calling VariantClear). Some of the types VARIANT can hold require
cleanup (e.g. string data represented as a BSTR needs to be freed with
SysFreeString; interface pointers need Release() called on them).
CComVariant makes it easy for you not to forget to call VariantClear.
For example, here's how you set up a variant holding a boolean value
With best wishes,
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925