https://blog.csdn.net/suyouli/article/details/58594957
Transfer from: http: //kuaixingdong.blog.hexun.com/29627840_d.html
Let's look at some type of COM supports basic categories:
( Microsoft provided, as defined in the comdef.h)
Class standard class used in COM follows:
_bstr_t: BSTR type to be packaged, and to provide useful methods and operations;
_com_error: defined error objects thrown;
_com_ptr_t: Package COM interface pointers
_variant_t: VARIANT types of packaging, and to provide useful methods and operations;
A, _variant_t like a brief introduction:
_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.
(1) Method _variant class provides:
1> Constructor
_variant_t
_Variant_t variable initialization call to the constructor of _variant_t. We generally used to initialize a variable digital class with a variable of type int, which _variant_t variable is not allowed.
The reason is simple, the constructor _variant_t's no use integer (Int) constructor initializes the
May be first converted to integer long, then initialize
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
_Variant_t a string assigned to the object;
7> Operators
Assignment, to existing objects _variant_t assign a new value;
8> operator ==, !=
Comparative _variant_t two objects are equal;
9> Extractors
Extract data from the encapsulated VARIANT object.
(2) _variant_t definition:
_variant_t class closed VARIANT data type, VARIANT is a structure type, we can look at its definition
[cpp] view plain copy
- typedef struct tagVARIANT
- {
- VARTYPE vt; // store data type
- 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 canter; // 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.
- };
- };
Note: vt for storing internal variable type (variable stored consortium), the consortium is used to store a value corresponding to the type of
Second, the data type conversion
_bstr_t _variant_t class as the class with the basic data type conversion mediation
Conversion between (1) _variant_t and CString
1> CString converted to _variant_t:
[cpp] view plain copy
- CString str;
- _variant_t str1=(LPCTSTR)str;
Use _variant_t SetString member functions to objects _variant_t assignment will be better;
2> _variant_t convert CString:
[cpp] view plain copy
- _variant_t vt;
- CString tempstr = (LPCSTR) _bstr_t (vt); // _ constructor are processed bstr_t type _variant_t
(2) _variant_t with conversion between char *
1> char * convert _variant_t
CString method similar to the method of converting _variant_t:
[cpp] view plain copy
- char * cValue;
- _variant_t vValue=(LPSTR)cValue;
2> _variant_t converted to char *:
Error Method:
[cpp] view plain copy
- _variant_t vValue;
- char * value=(LPSTR)(LPCSTR)_bstr_t(vValue)
value pointing to a pile of garbage ...
The reason: You can not use char * directly at (LPSTR) (LPCSTR) _bstr_t (_variant_t), because after this conversion is actually a string, rather than a char *
The correct way:
The conversion is performed only with strcpy (), copies the pointed LPSTR char * characters pointed to memory;
In the following example:
[cpp] view plain copy
- _variant_t vValue;
- char cValue[16]={0};
- strcpy(cValue, (LPCSTR)_bstr_t(vValue));
(3) determination of the value type _variant_t
The following code conversion is determined according to the type of vt _variant_t, then the data value is converted CString type (which may be converted to other data types as intermediary)
[cpp] view plain copy
- CString str;
- // The following code demonstrates how to convert a standard C string
- switch(var.vt)
- {
- case VT_BSTR:
- {
- 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 = Cole Date Time (were) .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);
- break;
- }
- }