_variant_t
我们先看看COM所支持的一些类型的基本类:
Microsoft Specific
Standard classes are used to support some of the COM types. The classes are defined in comdef.h and the header files generated from the type library.
Class Purpose
_bstr_t
Wraps the BSTR type to provide useful operators and methods.
_com_error
Defines the error object thrown by _com_raise_error in most failures.
_com_ptr_t
Encapsulates COM interface pointers, and automates the required calls to AddRef, Release, and QueryInterface.
_variant_t
Wraps the VARIANT type to provide useful operators and methods.
===================================================================================
接下来我们在回过来看看_variant_t类的一些属性:
Microsoft Specific
A _variant_t object encapsulates the VARIANT data type. The class manages resource allocation and deallocation and makes function calls to VariantInit and VariantClear as appropriate.
Construction
_variant_t
Constructs a _variant_t object.
Operations
Attach
Attaches a VARIANT object into the _variant_t object.
Clear
Clears the encapsulated VARIANT object.
ChangeType
Changes the type of the _variant_t object to the indicated VARTYPE.
Detach
Detaches the encapsulated VARIANT object from this _variant_t object.
SetString
Assigns a string to this _variant_t object.
Operators
Operator =
Assigns a new value to an existing _variant_t object.
operator ==, !=
Compare two _variant_t objects for equality or inequality.
Extractors
Extract data from the encapsulated VARIANT object.
_variant_t类封闭了VARIANT数据类型,VARIANT是一个结构体类型,我们可以看一下它的定义
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.
};
};
vt用来存储存储变量的类型,联合体用来存储对用类型的值
对_variant_t变量初始化调用的是_variant_t的构造函数,我们一般习惯于用一个int型的变量对数字类的变量进行初始化,这对_variant_t变量是不允许的,原因很简单,_variant_t的构造函数中没用用整型对其初始化的构造函数,可先将整型转化成long对其进行初始化,赋值操作与构造函数类似.
_variant_t与CString之间的转化
CString--->_variant_t
CString str;
_variant_t str1=(LPCTSTR)str; //【但我更倾向于使用_variant_t的成员函数SetString来对_variant_t的对象赋值】
_variant_t--->CString
_variant_t vt;
CString tempstr=(LPCSTR)_bstr_t(vt); //_bstr_t的构造函数有对_variant_t类型的处理
===================================================================================
/*
将 _variant_t 类型的变量,转换成字符串(比如如果是数字,则转换成数字对应的字符串 :如 10.2 -> "10.200000")
*/
_variant_t var;
CString str; //转换以后的字符串
//以下代码演示如何转换为C标准字符串型
if (var.vt == VT_I4)
{
long lNum;
char szCh[21];
str=var.bstrVal;
WideCharToMultiByte
(CP_ACP, 0, var.bstrVal, -1,
szCh, sizeof(szCh), NULL, NULL);
}
//以下代码演示如何转换成逻辑型
if( var.vt == VT_BOOL)
{
BOOL bVar;
lNum=var.lVal;
bVar= var.boolVar==0? FALSE : TRUE;
}
//以下代码演示为其余类型(补充)
switch(var.vt)
{
case VT_BSTR: //var is BSTR type
str=var.bstrVal;
break;
case VT_I2: //var is short int type
str.Format("%d",(int)var.iVal);
break;
case VT_I4: //var is long int type
str.Format("%d",var.lVal);
break;
case VT_R4: //var is float type
str.Format("%10.6f",(double)var.fltVal);
break;
case VT_R8: //var is double type
str.Format("%10.6f",var.dblVal);
break;
case VT_CY: //var is CY type
str=COleCurrency(var).Format();
break;
case VT_DATE: //var is DATE type
str=COleDateTime(var).Format();
break;
case VT_BOOL: //var is VARIANT_BOOL
str= (var.boolVal==0) ?"FALSE": "TRUE";
break;
default:
str.Format("Unk type %d\n",var.vt);
TRACE("Unknown type %d\n",var.vt);
}