This article is reposted from other blogs and will introduce some common data types.
Let's first define some common type variables to illustrate
Int I = 100; <br/> long l = 2001; <br/> float F = 300.2; <br/> double D = 12345.119; <br/> char username [] = "Zhao San"; <br/> char temp [200]; <br/> char * Buf; <br/> cstring STR; <br/> _ variant_t V1; <br/> _ 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. <br/> ITOA (I, temp, 2 ); /// convert in binary mode
Long (long)
Ltoa (L, temp, 10 );
Floating Point Number (float, double)
Fcvt can be used to complete the conversion. This is an example in msdn:
Int decimal, sign; <br/> char * buffer; <br/> double source = 3.1415926535; <br/> buffer = _ fcvt (source, 7, & decimal, & sign );
Running result: Source: 3.1415926535 Buffer: '20180101' decimal: 1 sign: 0
Decimal indicates the decimal point position, sign indicates the symbol: 0 is a positive number, and 1 is a negative number.
Cstring variable
STR = "2008 Beijing Olympics"; <br/> Buf = (lpstr) (lpctstr) STR;
BSTR variable
BSTR bstrvalue =: sysallocstring (L "programmer"); <br/> char * Buf = _ com_util: convertbstrtostring (bstrvalue); <br/> sysfreestring (bstrvalue ); <br/> afxmessagebox (BUF); <br/> Delete (BUF );
Ccombstr variable
Ccombstr bstrvar ("test"); <br/> char * Buf = _ com_util: convertbstrtostring (bstrvar. m_str); <br/> afxmessagebox (BUF); <br/> Delete (BUF );
_ Bstr_t variable
_ Bstr_t is the encapsulation of BSTR. It is easy to use because = operator has been overloaded.
_ Bstr_t bstrvar ("test"); <br/> const char * Buf = bstrvar; // do not modify the Buf content <br/> afxmessagebox (BUF );
General method (for non-com data types)
Use sprintf to complete the conversion
Char buffer [200]; <br/> char c = '1'; <br/> int I = 35; <br/> long J = 1000; <br/> float F = 1.7320534f; <br/> sprintf (buffer, "% C", c); <br/> sprintf (buffer, "% d ", i); <br/> sprintf (buffer, "% d", J); <br/> sprintf (buffer, "% F", F );
Ii. 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 );
Cstring variable
Cstring name = temp;
BSTR variable
BSTR bstrvalue =: sysallocstring (L "programmer"); <br/>... // use bstrvalue <br/> sysfreestring (bstrvalue );
Ccombstr variable
Ccombstr variables can be directly assigned values.
Ccombstr bstrvar1 ("test"); <br/> ccombstr bstrvar2 (temp );
_ Bstr_t variable
_ Bstr_t type variables can be directly assigned values
_ Bstr_t bstrvar1 ("test"); <br/> _ bstr_t bstrvar2 (temp );
Iii. convert other data types to cstring
Use the cstring member function Format for conversion. For example:
INTEGER (INT)
Str. Format ("% d", I); <br/>
Float)
Str. Format ("% F", I );
Data Types supported by cstring constructors, such as string pointers (char *), can be directly assigned values.
STR = username;
For data types not supported by format, you can convert the data type to char * by using the method described above, and then assign the value to the cstring variable.
Iv. BSTR, _ bstr_t and ccombstr
Ccombstr is the encapsulation of BSTR by ATL, _ bstr_t is the encapsulation of BSTR by C ++, and BSTR is a 32-bit pointer, but it does not directly point to the buffer of the string.
Char * can be converted to BSTR as follows:
Bstr B = _ com_util: convertstringtobstr ("data"); // comutil must be added before use. H and comsupp. lib <br/> sysfreestring (bstrvalue); <br/>
Otherwise, you can use
Char * P = _ com_util: convertbstrtostring (B); <br/> Delete P;
For details, refer to the specific descriptions in sections 1 and 2.
Ccombstr and _ bstr_t overload a large number of operators. You can directly perform = ,! =, = And so on, so it is very convenient to use.
Especially _ bstr_t. We recommend that you use it.
V. 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; <br/> int A = 2001; <br/> va. vt = vt_i4; // specify the integer data <br/> 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:
Byte bval; // vt_ui1. <br/> short ival; // vt_i2. <br/> long lval; // vt_i4. <br/> float fltval; // vt_r4. <br/> double dblval; // vt_r8. <br/> variant_bool boolval; // vt_bool. <br/> scode; // vt_error. <br/> Cy cyval; // vt_cy. <br/> date; // vt_date. <br/> BSTR bstrval; // vt_bstr. <br/> decimal far * pdecval // vt_byref | vt_decimal. <br/> iunknown far * punkval; // vt_unknown. <br/> idispatch far * pdispval; // vt_dispatch. <br/> safearray far * parray; // vt_array | *. <br/> byte far * pbval; // vt_byref | vt_ui1. <br/> short far * pival; // vt_byref | vt_i2. <br/> long far * plval; // vt_byref | vt_i4. <br/> float far * pfltval; // vt_byref | vt_r4. <br/> double far * pdblval; // vt_byref | vt_r8. <br/> variant_bool far * pboolval; // vt_byref | vt_bool. <br/> scode far * pscode; // vt_byref | vt_error. <br/> Cy far * pcyval; // vt_byref | vt_cy. <br/> date far * pdate; // vt_byref | vt_date. <br/> BSTR far * pbstrval; // vt_byref | vt_bstr. <br/> iunknown far * ppunkval; // vt_byref | vt_unknown. <br/> idispatch far * ppdispval; // vt_byref | vt_dispatch. <br/> safearray far * pparray; // vt_array | *. <br/> variant far * pvarval; // vt_byref | vt_variant. <br/> void far * byref; // generic byref. <br/> char cval; // vt_i1. <br/> unsigned short uival; // vt_ui2. <br/> unsigned long ulval; // vt_ui4. <br/> int intval; // vt_int. <br/> unsigned int uintval; // vt_uint. <br/> char far * pcval; // vt_byref | vt_i1. <br/> unsigned short far * puival; // vt_byref | vt_ui2. <br/> unsigned long far * pulval; // vt_byref | vt_ui4. <br/> int far * pintval; // vt_byref | vt_int. <br/> unsigned int far * puintval; // vt_byref | vt_uint. <br/>
_ 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.
# Include <comdef. h> must be added for use.
For example:
Long L = 222; <br/> ing I = 100; <br/> _ variant_t lval (l); <br/> 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; <br/> cstring STR = (BSTR) v3.pbstrval; <br/> long I = v4.lval;
6. Other COM Data Types
Obtain CLSID Based on progid
Hresult clsidfromprogid (lpcolestr lpszprogid, lpclsid pclsid); <br/> CLSID; <br/> clsidfromprogid (L "mapi. folder", & CLSID );
Obtain the progid Based on clsid.
Winoleapi progidfromclsid (refclsid CLSID, lpolestr * lplpszprogid );
For example, we have defined clsid_iapplication. The following code gets the progid
Lpolestr pprogid = 0; <br/> progidfromclsid (clsid_iapplication, & pprogid); <br/>... /// you can use pprogid <br/> cotaskmemfree (pprogid); // do not forget to release
VII. ANSI and Unicode
Unicode is a string of the wide character type, and all Unicode strings are used in COM.
Convert ANSI to Unicode
(1) Use the macro L, for example, clsidfromprogid (L "mapi. folder", & CLSID );
(2) Implement conversion through the multibytetowidechar function, for example:
Char * szprogid = "mapi. folder "; <br/> wchar szwideprogid [128]; <br/> CLSID; <br/> long llen = multibytetowidechar (cp_acp, 0, szprogid, strlen (szprogid ), szwideprogid, sizeof (szwideprogid); <br/> szwideprogid [llen] = '/0 ';
(3) using the a2w macro, for example:
Uses_conversion; <br/> clsidfromprogid (a2w (szprogid), & CLSID );
Convert Unicode to ANSI
(1) Use widechartomultibyte, for example:
// Assume that you already have a unicode string wszsomestring... <br/> char szansistring [max_path]; <br/> widechartomultibyte (cp_acp, wc_compositecheck, wszsomestring,-1, szansistring, sizeof (szansistring), null, null );
(2) Use the w2a macro, for example:
Uses_conversion; <br/> ptemp = w2a (wszsomestring );
8. 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; <br/> word lovalue = loword (lparam); // get 16 bits <br/> word hivalue = hiword (lparam ); /// 16 digits 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; <br/> byte lovalue = lobyte (wvalue); // get 8 bits <br/> byte hivalue = hibyte (wvalue ); /// get the 8-digit height
Two 16-bit data (Word) to synthesize 32-bit data (DWORD, lresult, lparam, or wparam)
Long makelong (word wlow, word whigh); <br/> wparam makewparam (word wlow, word whigh); <br/> lparam makelparam (word wlow, word whigh ); <br/> lresult makelresult (word wlow, word whigh );
Two 8-bit data (byte) to synthesize 16-bit data (word)
Word makeword (byte blow, byte bhigh );
Obtain the colorref color value from R (red), g (green), and B (blue ).
Colorref RGB (byte byred, byte bygreen, byte byblue );
For example, colorref bkcolor = RGB (0x22,0x98,0X34 );
From colorref color value to RGB color value
Byte Red = getrvalue (bkcolor); // get the red color <br/> byte Green = getgvalue (bkcolor ); /// obtain the green color <br/> byte Blue = getbvalue (bkcolor); // obtain the blue color.
IX. Precautions
To use convertbstrtostring, add the header file comutil. H, add comsupp. lib to setting, or directly add # pragma comment (Lib, "comsupp. lib ")