This article Reprinted from: http://ygdljg.blog.163.com/blog/static/546010462008101191835269/
Http://www.vckbase.com/document/viewdoc? Id = 1096
Http://www.vckbase.com/document/viewdoc? Id = 1082
CW2A (BSTR)
CW2A (_ bstr_t)
CW2A (VARIANT. bstrVal)
CW2A (_ varaint_t.bstrVal)
The macro W2T can be used.
Void func (BSTR * lpsz)
{
USES_CONVERSION;
CString x = W2T (lpsz );
}
_ Variant_t var;
// Convert all types to VT_BSTR
: VariantChangeType (& var, & var, 0, VT_BSTR );
CString str = var. bstrVal;
Use two functions:
Convert string: _ com_util: ConvertBSTRToString (BSTR pSrc );
Convert BSTR: _ com_util: ConvertStringToBSTR (const char * pSrc );
SES_CONVERSION;
OLE2T ()
T2OLE ()
// Create a BSTR and assign it to a Variant
BSTR x = SysAllocString (L "Hello ");
VARIANT myVariant;
MyVariant. vt = VT_BSTR;
MyVariant. bstrVal = x;
SysFreeString (x );
// Create a CString and change it to a variant;
CString myCString (_ T ("My String "));
CString mySecondString;
// This is required to use the T2COLE macro.
USES_CONVERSION;
BSTR y = SysAllocString (T2COLE (myCString ));
MyVariant. bstrVal = y;
MySecondString = y;
SysFreeString (y );
_ Bstr_t str2 = "hjljljl ";
Char * str1 = (char *) str2; // char * is an overload of the _ bstr_t class.
CString str3 = str1;
// Create two BSTRs and add them.
BSTR a = SysAllocString (L "One two ");
BSTR B = SysAllocString (L "three four .");
// Or if CString already exists.
MyCString = B;
BSTR bstr = L "afdjkl ";
CString str = bstr;
BSTR bstr2 = str. AllocSysString ();
Let's first define some common type variables to illustrate
Int I = 100;
Long l = 2001;
Float f = 300.2;
Double d = 12345.119;
Char username [] = "Woman Cheng peijun ";
Char temp [200];
Char * buf;
CString str;
_ Variant_t v1;
_ Bstr_t v2;
1. convert other data types to strings
Short INTEGER (int)
Itoa (I, temp, 10); // convert I to a string and put it into temp. the last digit indicates decimal.
Itoa (I, temp, 2); // convert in binary mode
Long (long)
Ltoa (l, temp, 10 );
2. Obtain the pointer to the string from other variables containing the string
CString variable
Str = "2008 Beijing Olympics ";
Buf = (LPSTR) (LPCTSTR) str;
_ Variant_t variable of the BSTR type
V1 = (_ bstr_t) "programmer ";
Buf = _ com_util: ConvertBSTRToString (_ bstr_t) v1 );
Iii. Convert strings to other data types
Strcpy (temp, "123 ");
Short INTEGER (int)
I = atoi (temp );
Long (long)
L = atol (temp );
Floating Point (double)
D = atof (temp );
4. convert other data types to CString
Use the CString member function Format for conversion. For example:
INTEGER (int)
Str. Format ("% d", I );
Float)
Str. Format ("% f", I );
Data Types supported by CString constructors, such as string pointers (char *), can be directly assigned values.
Str = username;
V. BSTR, _ bstr_t and CComBSTR
CComBSTR and _ bstr_t are encapsulation of BSTR, and BSTR is a 32-bit pointer to a string.
Char * can be converted to BSTR like this: BSTR B = _ com_util: ConvertStringToBSTR ("data"); // you need to add the header file comutil. h before use
Otherwise, use char * p = _ com_util: ConvertBSTRToString (B );
6. VARIANT, _ variant_t, and COleVariant
For the VARIANT Structure, refer to the definition of the tagVARIANT struct in the header file VC98 \ Include \ OAIDL. H.
Assign a value to the VARIANT variable: assign a value to the vt member to specify the data type, and then assign a value to the variable of the same data type in the union structure. For example:
VARIANT va;
Int a = 2001;
Va. vt = VT_I4; // specify Integer Data
Va. lVal = a; // value assignment
For VARIANT that is not immediately assigned a value, it is best to initialize with Void VariantInit (variantarg far * pvarg); in essence, the vt is set to VT_EMPTY, the following table lists the correspondence between vt and common data:
Unsigned char 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
IUnknown FAR * punkVal; VT_UNKNOWN
IDispatch FAR * pdispVal; VT_DISPATCH
Safearray far * parray; VT_ARRAY | *
Unsigned char 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 * ppunkVal; VT_BYREF | VT_UNKNOWN
IDispatch FAR * ppdispVal; VT_BYREF | VT_DISPATCH
Safearray far * pparray; VT_ARRAY | *
Variant far * pvarVal; VT_BYREF | VT_VARIANT
Void FAR * byref; VT_BYREF
_ Variant_t is the encapsulation class of VARIANT, and its value assignment can be forced type conversion. Its constructor will automatically process these data types.
For example:
Long l = 222;
Ing I = 100;
_ Variant_t lVal (l );
LVal = (long) I;
The use of COleVariant is basically the same as that of the _ variant_t method. See the following example:
COleVariant v3 = "string", v4 = (long) 1999;
CString str = (BSTR) v3.pbstrVal;
Long I = v4.lVal;
VII. Others
During message processing, we often need to split WPARAM, LPARAM, and other 32-bit data (DWORD) into two 16-bit data (WORD), for example:
LPARAM lParam;
WORD lovalue = LOWORD (lParam); // take 16 bits
WORD hivalue = HIWORD (lParam); // you can specify 16 characters in height.
For a 16-bit data (WORD), we can use the same method to break down the high and low 8-bit data (BYTE), for example:
WORD wvalue;
BYTE lovalue = LOBYTE (wvalue); // get 8 bits
BYTE hivalue = HIBYTE (wvalue); // get the 8-bit high
CString csStr;
BSTR a = csStr. AllocSysString ();
CString str;
Holder = theApp. m_pADOSet-> GetCollect ("typeid ");
Str = Holder. vt = VT_NULL? "" :( Char *) (_ bstr_t) Holder; // string
Holder = theApp. m_pADOSet-> GetCollect ("price ");
Str. Format ("%. 2f", Holder. vt = VT_NULL? 0: Holder. dblVal); // real number type
Holder = theApp. m_pADOSet-> GetCollect ("Date"); // Date type
CTime time;
Time = Holder;
Time. Format ("% Y-% M-% d ")
All variant types can be directly converted to string pointers.
For example, str = (_ BSTR_T) var;