We first define some common types of 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, you can 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* 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; 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;
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