_variant_t with other types of data conversion

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

  1. typedef struct tagVARIANT   
  2. {  
  3.     VARTYPE vt; // store data type  
  4.     unsigned short wReserved1;  
  5.     unsigned short wReserved2;  
  6.     unsigned short wReserved3;  
  7.     union   
  8.     {  
  9.         Byte bVal;                          // VT_UI1.  
  10.         Short iVal;                         // VT_I2.  
  11.         long lVal;                          // VT_I4.  
  12.         float fltVal;                       // VT_R4.  
  13.         double dblVal;                      // VT_R8.  
  14.         VARIANT_BOOL boolVal;               // VT_BOOL.  
  15.         SCODE scode;                        // VT_ERROR.  
  16.         CY cyVal;                           // VT_CY.  
  17.         DATE date;                          // VT_DATE.  
  18.         BSTR bstrVal;                       // VT_BSTR.  
  19.         DECIMAL FAR* pdecVal                // VT_BYREF|VT_DECIMAL.  
  20.         IUnknown FAR* punkVal;              // VT_UNKNOWN.  
  21.         IDispatch FAR* pdispVal;            // VT_DISPATCH.  
  22.         SAFEARRAY FAR* parray;              // VT_ARRAY|*.  
  23.         Byte FAR* pbVal;                    // VT_BYREF|VT_UI1.  
  24.         short FAR* piVal;                   // VT_BYREF|VT_I2.  
  25.         long FAR* plVal;                    // VT_BYREF|VT_I4.  
  26.         float FAR* pfltVal;                 // VT_BYREF|VT_R4.  
  27.         double FAR* pdblVal;                // VT_BYREF|VT_R8.  
  28.         VARIANT_BOOL FAR* pboolVal;         // VT_BYREF|VT_BOOL.  
  29.         SCODE FAR* pscode;                  // VT_BYREF|VT_ERROR.  
  30.         CY FAR* pcyVal;                     // VT_BYREF|VT_CY.  
  31.         DATE FAR* pdate;                    // VT_BYREF|VT_DATE.  
  32.         BSTR FAR* pbstrVal;                 // VT_BYREF|VT_BSTR.  
  33.         IUnknown FAR* FAR* ppunkVal;        // VT_BYREF|VT_UNKNOWN.  
  34.         IDispatch FAR* FAR* ppdispVal;      // VT_BYREF|VT_DISPATCH.  
  35.         SAFEARRAY FAR* FAR* pparray;        // VT_ARRAY|*.  
  36.         VARIANT FAR* pvarVal;               // VT_BYREF|VT_VARIANT.  
  37.         void FAR* byref;                    // Generic ByRef.  
  38.         char canter; // VT_I1.  
  39.         unsigned short uiVal;               // VT_UI2.  
  40.         unsigned long ulVal;                // VT_UI4.  
  41.         int intVal;                         // VT_INT.  
  42.         unsigned int uintVal;               // VT_UINT.  
  43.         char FAR * pcVal;                   // VT_BYREF|VT_I1.  
  44.         unsigned short FAR * puiVal;        // VT_BYREF|VT_UI2.  
  45.         unsigned long FAR * pulVal;         // VT_BYREF|VT_UI4.  
  46.         int FAR * pintVal;                  // VT_BYREF|VT_INT.  
  47.         unsigned int FAR * puintVal;        //VT_BYREF|VT_UINT.  
  48.     };  
  49. };  

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

  1. CString str;  
  2. _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

  1. _variant_t vt;  
  2. 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

  1. char * cValue;  
  2. _variant_t vValue=(LPSTR)cValue;  

2> _variant_t converted to char *:

 

Error Method:

[cpp] view plain copy

  1. _variant_t vValue;  
  2. 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

  1. _variant_t vValue;  
  2. char cValue[16]={0};  
  3. 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

  1. CString str;  
  2.   
  3. // The following code demonstrates how to convert a standard C string  
  4. switch(var.vt)  
  5. {  
  6.     case VT_BSTR:  
  7.     {  
  8.         str = var.bstrVal;  
  9.         break;  
  10.     }  
  11.     case VT_I2: //var is short int type   
  12.     {  
  13.         str.Format("%d",(int)var.iVal);  
  14.         break;  
  15.     }  
  16.     case VT_I4: //var is long int type  
  17.     {  
  18.         str.Format("%d", var.lVal);  
  19.         break;  
  20.     }  
  21.     case VT_R4: //var is float type  
  22.     {  
  23.         str.Format("%10.6f", (double)var.fltVal);  
  24.         break;  
  25.     }  
  26.     case VT_R8: //var is double type  
  27.     {  
  28.         str.Format("%10.6f", var.dblVal);  
  29.         break;  
  30.     }  
  31.     case VT_CY: //var is CY type  
  32.     {  
  33.         str=COleCurrency(var).Format();  
  34.         break;  
  35.     }  
  36.     case VT_DATE: //var is DATE type  
  37.     {  
  38.         str = Cole Date Time (were) .Format ();  
  39.         break;  
  40.     }  
  41.     case VT_BOOL: //var is VARIANT_BOOL  
  42.     {  
  43.         str= (var.boolVal==0) ?"FALSE": "TRUE";  
  44.         break;  
  45.     }  
  46.     default:  
  47.     {  
  48.         str.Format("Unk type %d\n",var.vt);  
  49.         TRACE("Unknown type %d\n",var.vt);  
  50.         break;  
  51.     }  
  52. }  

Guess you like

Origin blog.csdn.net/thanklife/article/details/87794357