C++、BASIC、Java、Pascal、Script......计算机语言多种多样,而它们各自又都有自己的数据类型,COM 产生目的,其中之一就是要跨语言(注3)。而 VARIANT 数据类型就具有跨语言的特性,同时它可以表示(存储)任意类型的数据。从C语言的角度来讲,VARIANT 其实是一个结构,结构中用一个域(vt)表示------该变量到底表示的是什么类型数据,同时真正的数据则存贮在 union 空间中。结构的定义太长了(虽然长,但其实很简单)大家去看 MSDN 的描述吧,这里给出如何使用的简单示例:
学生:我想用 VARIANT 表示一个4字节长的整数,如何做?
老师:VARIANT v; v.vt=VT_I4; v.lVal=100;
学生:我想用 VARIANT 表示布尔值“真”,如何做?
老师:VARIANT v; v.vt=VT_BOOL; v.boolVal=VARIANT_TRUE;
学生:这么麻烦?我能不能 v.boolVal=true; 这样写?
老师:不可以!因为
类型 | 字节长度 | 假值 | 真值 |
bool | 1(char) | 0(false) | 1(true) |
BOOL | 4(int) | 0(FALSE) | 1(TRUE) |
VT_BOOL | 2(short int) | 0(VARIANT_FALSE) | -1(VARIANT_TRUE) |
所以如果你 v.boolVal=true 这样赋值,那么将来 if(VARIANT_TRUE==v.boolVal) 的时候会出问题(-1 != 1)。但是你注意观察,任何布尔类型的“假”都是0,因此作为一个好习惯,在做布尔判断的时候,不要和“真值”相比较,而要与“假值”做比较。
学生:谢谢老师,你太牛了。我对老师的敬仰如滔滔江水,连绵不绝......
学生:我想用 VARIANT 保存字符串,如何做?
老师:VARIANT v; v.vt=VT_BSTR; v.bstrVal=SysAllocString(L"Hello,你好");
学生:哦......我明白了。可是这么操作真够麻烦的,有没有简单一些的方法?
老师:有呀,你可以使用现成的包装类 CComVariant、COleVariant、_variant_t。比如上面三个问题就可以这样书写:CComVariant v1(100),v2(true),v3("Hello,你好"); 简单了吧?!(注4)
学生:老师,我再问最后一个问题,我如何用 VARIANT 保存一个数组?
老师:这个问题很复杂,我现在不能告诉你,我现在告诉你怕你印象不深......(注5)
学生:~!@#$%^&*()......晕!
下面是msdn的内容:
VARIANT and VARIANTARG
Use VARIANTARG to describe arguments passed within DISPPARAMS, and VARIANT to specify variant data that cannot be passed by reference. When a variant refers to another variant by using the VT_VARIANT | VT_BYREF vartype, the variant being referred to cannot also be of type VT_VARIANT | VT_BYREF. VARIANTs can be passed by value, even if VARIANTARGs cannot. The following definition of VARIANT is described in OAIDL.H automation header file:
typedef struct FARSTRUCT tagVARIANT VARIANT; typedef struct FARSTRUCT tagVARIANT VARIANTARG; typedef struct tagVARIANT { VARTYPE vt; unsigned short wReserved1; unsigned short wReserved2; unsigned short wReserved3; union { Byte bVal; // VT_UI1. Short iVal; // VT_I2. long lVal; // VT_I4. float fltVal; // VT_R4. double dblVal; // VT_R8. VARIANT_BOOL boolVal; // VT_BOOL. SCODE scode; // VT_ERROR. CY cyVal; // VT_CY. DATE date; // VT_DATE. BSTR bstrVal; // VT_BSTR. DECIMAL FAR* pdecVal // VT_BYREF|VT_DECIMAL. IUnknown FAR* punkVal; // VT_UNKNOWN. IDispatch FAR* pdispVal; // VT_DISPATCH. SAFEARRAY FAR* parray; // VT_ARRAY|*. Byte FAR* pbVal; // VT_BYREF|VT_UI1. short FAR* piVal; // VT_BYREF|VT_I2. long FAR* plVal; // VT_BYREF|VT_I4. float FAR* pfltVal; // VT_BYREF|VT_R4. double FAR* pdblVal; // VT_BYREF|VT_R8. VARIANT_BOOL FAR* pboolVal; // VT_BYREF|VT_BOOL. SCODE FAR* pscode; // VT_BYREF|VT_ERROR. CY FAR* pcyVal; // VT_BYREF|VT_CY. DATE FAR* pdate; // VT_BYREF|VT_DATE. BSTR FAR* pbstrVal; // VT_BYREF|VT_BSTR. IUnknown FAR* FAR* ppunkVal; // VT_BYREF|VT_UNKNOWN. IDispatch FAR* FAR* ppdispVal; // VT_BYREF|VT_DISPATCH. SAFEARRAY FAR* FAR* pparray; // VT_ARRAY|*. VARIANT FAR* pvarVal; // VT_BYREF|VT_VARIANT. void FAR* byref; // Generic ByRef. char cVal; // VT_I1. unsigned short uiVal; // VT_UI2. unsigned long ulVal; // VT_UI4. int intVal; // VT_INT. unsigned int uintVal; // VT_UINT. char FAR * pcVal; // VT_BYREF|VT_I1. unsigned short FAR * puiVal; // VT_BYREF|VT_UI2. unsigned long FAR * pulVal; // VT_BYREF|VT_UI4. int FAR * pintVal; // VT_BYREF|VT_INT. unsigned int FAR * puintVal; //VT_BYREF|VT_UINT. }; };
To simplify extracting values from VARIANTARGs, Automation provides a set of functions for manipulating this type. Use of these functions is strongly recommended to ensure that applications apply consistent coercion rules.
The vt value governs the interpretation of the union as follows:
Value | Description |
---|---|
VT_EMPTY | No value was specified. If an optional argument to an Automation method is left blank, do not pass a VARIANT of type VT_EMPTY. Instead, pass a VARIANT of type VT_ERROR with a value of DISP_E_PARAMNOTFOUND. |
VT_EMPTY | VT_BYREF | Not valid. |
VT_UI1 | An unsigned 1-byte character is stored in bVal. |
VT_UI1 | VT_BYREF | A reference to an unsigned 1-byte character was passed. A pointer to the value is inpbVal. |
VT_UI2 | An unsigned 2-byte integer value is stored in uiVal. |
VT_UI2 | VT_BYREF | A reference to an unsigned 2-byte integer was passed. A pointer to the value is inpuiVal. |
VT_UI4 | An unsigned 4-byte integer value is stored in ulVal. |
VT_UI4 | VT_BYREF | A reference to an unsigend 4-byte integer was passed. A pointer to the value is inpulVal. |
VT_UINT | An unsigned integer value is stored in uintVal. |
VT_UINT | VT_BYREF | A reference to an unsigned integer value was passed. A pointer to the value is inpuintVal. |
VT_INT | An integer value is stored in intVal. |
VT_INT | VT_BYREF | A reference to an integer value was passed. A pointer to the value is inpintVal. |
VT_I1 | A 1-byte character value is stored in cVal. |
VT_I1 | VT_BYREF | A reference to a 1-byte character was passed. A pointer the value is inpcVal. |
VT_I2 | A 2-byte integer value is stored in iVal. |
VT_I2 | VT_BYREF | A reference to a 2-byte integer was passed. A pointer to the value is inpiVal. |
VT_I4 | A 4-byte integer value is stored in lVal. |
VT_I4 | VT_BYREF | A reference to a 4-byte integer was passed. A pointer to the value is inplVal. |
VT_R4 | An IEEE 4-byte real value is stored in fltVal. |
VT_R4 | VT_BYREF | A reference to an IEEE 4-byte real value was passed. A pointer to the value is inpfltVal. |
VT_R8 | An 8-byte IEEE real value is stored in dblVal. |
VT_R8 | VT_BYREF | A reference to an 8-byte IEEE real value was passed. A pointer to its value is inpdblVal. |
VT_CY | A currency value was specified. A currency number is stored as 64-bit (8-byte), two's complement integer, scaled by 10,000 to give a fixed-point number with 15 digits to the left of the decimal point and 4 digits to the right. The value is incyVal. |
VT_CY | VT_BYREF | A reference to a currency value was passed. A pointer to the value is inpcyVal. |
VT_BSTR | A string was passed; it is stored in bstrVal. This pointer must be obtained and freed by the BSTR functions, which are described inConversion and Manipulation Functions. |
VT_BSTR | VT_BYREF | A reference to a string was passed. A BSTR* that points to a BSTR is inpbstrVal. The referenced pointer must be obtained or freed by the BSTR functions. |
VT_DECIMAL | Decimal variables are stored as 96-bit (12-byte) unsigned integers scaled by a variable power of 10. VT_DECIMAL uses the entire 16 bytes of the Variant. |
VT_DECIMAL | VT_BYREF | A reference to a decimal value was passed. A pointer to the value is inpdecVal. |
VT_NULL | A propagating null value was specified. (This should not be confused with the null pointer.) The null value is used for tri-state logic, as with SQL. |
VT_NULL | VT_BYREF | Not valid. |
VT_ERROR | An SCODE was specified. The type of the error is specified in scodee. Generally, operations on error values should raise an exception or propagate the error to the return value, as appropriate. |
VT_ERROR | VT_BYREF | A reference to an SCODE was passed. A pointer to the value is inpscode. |
VT_BOOL | A 16 bit Boolean (True/False) value was specified. A value of 0xFFFF (all bits 1) indicates True; a value of 0 (all bits 0) indicates False. No other values are valid. |
VT_BOOL | VT_BYREF | A reference to a Boolean value. A pointer to the Boolean value is in pbool. |
VT_DATE | A value denoting a date and time was specified. Dates are represented as double-precision numbers, where midnight, January 1, 1900 is 2.0, January 2, 1900 is 3.0, and so on. The value is passed indate. This is the same numbering system used by most spreadsheet programs, although some specify incorrectly that February 29, 1900 existed, and thus set January 1, 1900 to 1.0. The date can be converted to and from an MS-DOS representation usingVariantTimeToDosDateTime, which is discussed in Conversion and Manipulation Functions. |
VT_DATE | VT_BYREF | A reference to a date was passed. A pointer to the value is inpdate. |
VT_DISPATCH | A pointer to an object was specified. The pointer is inpdispVal. This object is known only to implement IDispatch. The object can be queried as to whether it supports any other desired interface by callingQueryInterface on the object. Objects that do not implementIDispatch should be passed using VT_UNKNOWN. |
VT_DISPATCH | VT_BYREF | A pointer to a pointer to an object was specified. The pointer to the object is stored in the location referred to byppdispVal. |
VT_VARIANT | Invalid. VARIANTARGs must be passed by reference. |
VT_VARIANT | VT_BYREF | A pointer to another VARIANTARG is passed in pvarVal. This referenced VARIANTARG,pvarVal, cannot be another VT_VARIANT|VT_BYREF. This value can be used to support languages that allow functions to change the types of variables passed by reference. |
VT_UNKNOWN | A pointer to an object that implements the IUnknown interface is passed in punkVal. |
VT_UNKNOWN | VT_BYREF | A pointer to the IUnknown interface is passed inppunkVal. The pointer to the interface is stored in the location referred to byppunkVal. |
VT_ARRAY | <anything> | An array of data type <anything> was passed. (VT_EMPTY and VT_NULL are invalid types to combine with VT_ARRAY.) The pointer inpparray points to an array descriptor, which describes the dimensions, size, and in-memory location of the array. The array descriptor is never accessed directly, but instead is read and modified using the functions described inConversion and Manipulation Functions. |