转自:http://hi.baidu.com/ruiqing_2008/blog/item/d9488a9b7caa6ab9c8eaf47f.html
_variant_t是一个类,是VARIANT的完全包装类,它提供多种构造函数和数据转换函数。它继承自一个结构体,其结构如下:
struct tagVARIANT
{
union
{
struct __tagVARIANT
{
VARTYPE vt;
WORD wReserved1;
WORD wReserved2;
WORD wReserved3;
union
{
LONG lVal;
BYTE bVal;
SHORT iVal;
FLOAT fltVal;
DOUBLE dblVal;
VARIANT_BOOL boolVal;
_VARIANT_BOOL bool;
SCODE scode;
CY cyVal;
DATE date;
BSTR bstrVal;
IUnknown __RPC_FAR *punkVal;
IDispatch __RPC_FAR *pdispVal;
SAFEARRAY __RPC_FAR *parray;
BYTE __RPC_FAR *pbVal;
SHORT __RPC_FAR *piVal;
LONG __RPC_FAR *plVal;
FLOAT __RPC_FAR *pfltVal;
DOUBLE __RPC_FAR *pdblVal;
VARIANT_BOOL __RPC_FAR *pboolVal;
_VARIANT_BOOL __RPC_FAR *pbool;
SCODE __RPC_FAR *pscode;
CY __RPC_FAR *pcyVal;
DATE __RPC_FAR *pdate;
BSTR __RPC_FAR *pbstrVal;
IUnknown __RPC_FAR *__RPC_FAR *ppunkVal;
IDispatch __RPC_FAR *__RPC_FAR *ppdispVal;
SAFEARRAY __RPC_FAR *__RPC_FAR *pparray;
VARIANT __RPC_FAR *pvarVal;
PVOID byref;
CHAR cVal;
USHORT uiVal;
ULONG ulVal;
INT intVal;
UINT uintVal;
DECIMAL __RPC_FAR *pdecVal;
CHAR __RPC_FAR *pcVal;
USHORT __RPC_FAR *puiVal;
ULONG __RPC_FAR *pulVal;
INT __RPC_FAR *pintVal;
UINT __RPC_FAR *puintVal;
struct __tagBRECORD
{
PVOID pvRecord;
IRecordInfo __RPC_FAR *pRecInfo;
} __VARIANT_NAME_4;
} __VARIANT_NAME_3;
} __VARIANT_NAME_2;
DECIMAL decVal;
} __VARIANT_NAME_1;
};
其中,vt表示的是_variant_t的数据类型,它的取值是以下枚举结构的值:
enum VARENUM
{
VT_NULL =1 [V] [P] SQL style Null
VT_I2 =2 [V][T][P][S] 2 byte signed int
VT_I4 =3 [V][T][P][S] 4 byte signed int
VT_R4 = 4 [V][T][P][S] 4 byte real
VT_R8 = 5 [V][T][P][S] 8 byte real
VT_CY = 6 [V][T][P][S] currency
VT_DATE =7 [V][T][P][S] date
VT_BSTR =8 [V][T][P][S] OLE Automation string
VT_DISPATCH =9 [V][T][P][S] IDispatch *
VT_ERROR =10 [V][T][P][S] SCODE
VT_BOOL =11 [V][T][P][S] True=-1, False=0
VT_VARIANT =12 [V][T][P][S] VARIANT *
VT_UNKNOWN =13 [V][T] [S] IUnknown *
VT_DECIMAL =14 [V][T] [S] 16 byte fixed point
VT_RECORD =36 [V] [P][S] user defined type
VT_I1 = 16 [V][T][P][s] signed char
VT_UI1 =17 [V][T][P][S] unsigned char
VT_UI2 =18 [V][T][P][S] unsigned short
VT_UI4 =19 [V][T][P][S] unsigned short
VT_I8 =20 [T][P] signed 64-bit int
VT_UI8 =21 [T][P] unsigned 64-bit int
VT_INT =22 [V][T][P][S] signed machine int
VT_UINT =23 [V][T] [S] unsigned machine int
VT_VOID =24 [T] C style void
VT_HRESULT =25 [T] Standard return type
VT_PTR =26 [T] pointer type
VT_SAFEARRAY =27 [T] (use VT_ARRAY in VARIANT)
VT_CARRAY = 28 [T] C style array
VT_USERDEFINED =29 [T] user defined type
VT_LPSTR = 30 [T][P] null terminated string
VT_LPWSTR =31 [T][P] wide null terminated string
VT_FILETIME = 64 [P] FILETIME
VT_BLOB = 65 [P] Length prefixed bytes
VT_STREAM =66 [P] Name of the stream follows
VT_STORAGE =67 [P] Name of the storage follows
VT_STREAMED_OBJECT =68 [P] Stream contains an object
VT_STORED_OBJECT =69 [P] Storage contains an object
VT_BLOB_OBJECT =70 [P] Blob contains an object
VT_CF = 71 [P] Clipboard format
VT_CLSID =72 [P] A Class ID
VT_VECTOR = 0x1000 [P] simple counted array
VT_ARRAY = 0x2000 [V] SAFEARRAY*
VT_BYREF = 0x4000 [V] void* for local use
VT_BSTR_BLOB =0xfff Reserved for system use
VT_RESERVED = 0x8000,
VT_ILLEGAL = 0xffff,
VT_ILLEGALMASKED = 0xfff,
VT_TYPEMASK = 0xfff
};
_variant_t变量初始化调用的是_variant_t的构造函数,如果用一个int型的变量对数字类的变量进行初始化,会出错,因为_variant_t的构造函数中没用用整型对其初始化的构造函数,可先将整型转化成long对其进行初始化,赋值操作与构造函数类似,也可如下操作:
int a = 2000;
_variant_t var;
var.vt = VT_I2;
var.lVal = a;
对于没有构造函数的数据类型都可以这样赋值。
对于有构造函数的就可以直接利用构造函数赋值,例如:
long l=222;
_variant_t lVal(l);