转自:http://kuaixingdong.blog.hexun.com/29627840_d.html
我们先看看COM所支持的一些类型的基本类:
(微软提供,在comdef.h中定义)
在COM中使用的标准类Class如下所示:
_bstr_t:对BSTR类型进行打包,并提供有用的操作和方法;
_com_error:定义抛出的error对象;
_com_ptr_t:封装COM接口指针
_variant_t:对VARIANT类型进行打包,并提供有用的操作和方法;
一、_variant_t类的简单介绍:
_variant_t对象封装了VARIANT数据类型。
The class manages resource allocation and deallocation and makes function calls to VariantInit and VariantClear as appropriate.
(1) _variant类提供的方法:
1> 构造函数
_variant_t
对_variant_t变量初始化调用的是_variant_t的构造函数。我们一般习惯于用一个int型的变量对数字类的变量进行初始化,这对_variant_t变量是不允许的。
原因很简单,_variant_t 的构造函数中没有用整型( Int )对其初始化的构造函数
可先将整型转化成long,然后再对其进行初始化
2> Attach()
Attaches a VARIANT object into the _variant_t object.
3> Clear()
Clears the encapsulated VARIANT object.
4> ChangeType
Changes the type of the _variant_t object to the indicated VARTYPE.
5> Detach
Detaches the encapsulated VARIANT object from this _variant_t object.
6> SetString
将一个string赋值给_variant_t对象;
7> Operators
赋值操作,给现有的_variant_t对象赋一个新值;
8> operator ==, !=
对比两个 _variant_t 对象是否相等;
9> Extractors
Extract data from the encapsulated VARIANT object.
(2) _variant_t的定义:
_variant_t类封闭了VARIANT数据类型,VARIANT是一个结构体类型,我们可以看一下它的定义
[cpp] view plain copy print ?
- typedef struct tagVARIANT
- {
- VARTYPE vt;
- unsigned short wReserved1;
- unsigned short wReserved2;
- unsigned short wReserved3;
- union
- {
- Byte bVal;
- Short iVal;
- long lVal;
- float fltVal;
- double dblVal;
- VARIANT_BOOL boolVal;
- SCODE scode;
- CY cyVal;
- DATE date;
- BSTR bstrVal;
- DECIMAL FAR* pdecVal
- IUnknown FAR* punkVal;
- IDispatch FAR* pdispVal;
- SAFEARRAY FAR* parray;
- Byte FAR* pbVal;
- short FAR* piVal;
- long FAR* plVal;
- float FAR* pfltVal;
- double FAR* pdblVal;
- VARIANT_BOOL FAR* pboolVal;
- SCODE FAR* pscode;
- CY FAR* pcyVal;
- DATE FAR* pdate;
- BSTR FAR* pbstrVal;
- IUnknown FAR* FAR* ppunkVal;
- IDispatch FAR* FAR* ppdispVal;
- SAFEARRAY FAR* FAR* pparray;
- VARIANT FAR* pvarVal;
- void FAR* byref;
- char cVal;
- unsigned short uiVal;
- unsigned long ulVal;
- int intVal;
- unsigned int uintVal;
- char FAR * pcVal;
- unsigned short FAR * puiVal;
- unsigned long FAR * pulVal;
- int FAR * pintVal;
- unsigned int FAR * puintVal;
- };
- };
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用来存储内部变量(联合体中保存的变量)的类型,联合体用来存储对应类型的值
二、数据类型转换
_bstr_t类可以作为_variant_t类与基本数据类型转换的中介
(1) _variant_t与CString之间的转化
1> CString转换为_variant_t:
[cpp] view plain copy print ?
- CString str;
- _variant_t str1=(LPCTSTR)str;
CString str;
_variant_t str1=(LPCTSTR)str;
使用_variant_t的成员函数SetString来对_variant_t的对象赋值会更好;
2> _variant_t转换为CString:
[cpp] view plain copy print ?
- _variant_t vt;
- CString tempstr=(LPCSTR)_bstr_t(vt);
_variant_t vt;
CString tempstr=(LPCSTR)_bstr_t(vt); //_bstr_t的构造函数有对_variant_t类型的处理
(2) _variant_t与char *之间的转换
1> char * 转换为_variant_t
方法与CString转换为_variant_t的方法类似:
[cpp] view plain copy print ?
- char * cValue;
- _variant_t vValue=(LPSTR)cValue;
char * cValue;
_variant_t vValue=(LPSTR)cValue;
2> _variant_t转换为char *:
错误方法:
[cpp] view plain copy print ?
- _variant_t vValue;
- char * value=(LPSTR)(LPCSTR)_bstr_t(vValue)
_variant_t vValue;
char * value=(LPSTR)(LPCSTR)_bstr_t(vValue)
value指向一堆乱码...
原因:不能用char *直接指向(LPSTR)(LPCSTR)_bstr_t( _variant_t ),因为这样转换之后实际上是一个string,而非一个char *
正确方法:
进行转换时,只能用strcpy(),将LPSTR指向的字符复制到char * 所指向的内存中;
如下例:
[cpp] view plain copy print ?
- _variant_t vValue;
- char cValue[16]={0};
- strcpy(cValue, (LPCSTR)_bstr_t(vValue));
_variant_t vValue;
char cValue[16]={0};
strcpy(cValue, (LPCSTR)_bstr_t(vValue));
(3) 判断_variant_t的值类型
下面的转换代码根据_variant_t的vt进行类型判断,然后将数据值转换为CString类型(可作为转换为其他数据类型的中介)
[cpp] view plain copy print ?
- CString str;
-
-
- switch(var.vt)
- {
- case VT_BSTR:
- {
- str=var.bstrVal;
- break;
- }
- case VT_I2:
- {
- str.Format("%d",(int)var.iVal);
- break;
- }
- case VT_I4:
- {
- str.Format("%d", var.lVal);
- break;
- }
- case VT_R4:
- {
- str.Format("%10.6f", (double)var.fltVal);
- break;
- }
- case VT_R8:
- {
- str.Format("%10.6f", var.dblVal);
- break;
- }
- case VT_CY:
- {
- str=COleCurrency(var).Format();
- break;
- }
- case VT_DATE:
- {
- str=COleDateTime(var).Format();
- break;
- }
- case VT_BOOL:
- {
- str= (var.boolVal==0) ?"FALSE": "TRUE";
- break;
- }
- default:
- {
- str.Format("Unk type %d\n",var.vt);
- TRACE("Unknown type %d\n",var.vt);
- break;
- }
- }