The variant data type is defined in the file oaidl. IDL as follows:
Struct tagvariant {
Union {
Struct _ tagvariant {
Vartype VT;
Word wreserved1;
Word wreserved2;
Word wreserved3;
Union {
Ulonglong ullval;/* vt_ui8 */
Longlong llval;/* vt_i8 */
Long lval;/* vt_i4 */
Byte bval;/* vt_ui1 */
Short ival;/* vt_i2 */
Float fltval;/* vt_r4 */
Double dblval;/* vt_r8 */
Variant_bool boolval;/* vt_bool */
_ Variant_bool bool;/* (obsolete )*/
Scode;/* vt_error */
Cy cyval;/* vt_cy */
Date;/* vt_date */
BSTR bstrval;/* vt_bstr */
Iunknown * punkval;/* vt_unknown */
Idispatch * pdispval;/* vt_dispatch */
Safearray * parray;/* vt_array */
Byte * pbval;/* vt_byref | vt_ui1 */
Short * pival;/* vt_byref | vt_i2 */
Long * plval;/* vt_byref | vt_i4 */
Longlong * pllval;/* vt_byref | vt_i8 */
Float * pfltval;/* vt_byref | vt_r4 */
Double * pdblval;/* vt_byref | vt_r8 */
Variant_bool * pboolval;/* vt_byref | vt_bool */
_ Variant_bool * pbool;/* (obsolete )*/
Scode * pscode;/* vt_byref | vt_error */
Cy * pcyval;/* vt_byref | vt_cy */
Date * pdate;/* vt_byref | vt_date */
BSTR * pbstrval;/* vt_byref | vt_bstr */
Iunknown ** ppunkval;/* vt_byref | vt_unknown */
Idispatch ** ppdispval;/* vt_byref | vt_dispatch */
Safearray ** pparray;/* vt_byref | vt_array */
Variant * pvarval;/* vt_byref | vt_variant */
Pvoid byref;/* generic byref */
Char cval;/* vt_i1 */
Ushort uival;/* vt_ui2 */
Ulong ulval;/* vt_ui4 */
Int intval;/* vt_int */
Uint uintval;/* vt_uint */
Decimal * pdecval;/* vt_byref | vt_decimal */
Char * pcval;/* vt_byref | vt_i1 */
Ushort * puival;/* vt_byref | vt_ui2 */
Ulong * pulval;/* vt_byref | vt_ui4 */
Ulonglong * pullval;/* vt_byref | vt_ui8 */
Int * pintval;/* vt_byref | vt_int */
Uint * puintval;/* vt_byref | vt_uint */
Struct _ tagbrecord {
Pvoid pvrecord;
Irecordinfo * precinfo;
} _ Variant_name_4;/* vt_record */
} _ Variant_name_3;
} _ Variant_name_2;
Decimal decval;
} _ Variant_name_1;
};
The variant data structure contains two fields (if reserved fields are not considered ). The VT field describes the data type of the Second Field. To enable multiple types to appear in the second domain, we define a union structure. Therefore, the name of the second domain changes with the input value in the VT domain. Constants used to specify the value of the VT field are provided in the comments of each row in the Union definition.
The use of the variant and variantarg data structures must be completed in two steps. For example, let's consider the following code:
Long lvalues = 999;
Variant vparam;
Vparam. Vt = vt_i4;
Vparam. lval = lvalue;
Specify the data type in the first row. The constant vt_i4 indicates that a long data type will appear in the Second Field. According to the definition of type variant, we can know that when a long data is stored in the variant type, the second domain name is lval.
Passing parameters using variant means that non-strong languages (such as VBScript) can call methods implemented using strong languages (C ++. The implementation of the invoke () method can check whether the value encapsulated by the Variant Parameter conforms to its correct data type. If yes, this type is extracted and passed to the calling method. Otherwise, the invoke () method can use the variantchangetype () API function to convert the value to the correct type.