Let's first define some common type variables to explain
INT i = 100;
LONG L = 2001;
Float f = 300.2;
Double D = 12345.119;
CHAR Username [] = "Cheng Peijun";
CHAR TEMP [200];
CHAR * BUF;
CString Str;
_VARIANT_T V1;
_BSTR_T V2;
First, other data types are converted to strings
INT
ITOA (I, TEMP, 10); /// converts I to a string into the TEMP, the last number represents decimal
ITOA (I, TEMP, 2); // Press binary conversion
Long integer (long)
LTOA (L, TEMP, 10);
Float (Float, Double)
Use FCVT to complete the conversion, this is an example in MSDN:
INT Decimal, SIGN;
Char * buffer;
Double Source = 3.1415926535;
Buffer = _fcvt (Source, 7, & Decimal, & Sign);
Run results: Source: 3.1415926535 BUFFER: '31415927' Decimal: 1 Sign: 0
Decimal represents the position of the decimal point, SIGN represents the symbol: 0 is positive, 1 is negative
CSTRING variable
Str = "2008 Beijing Olympics";
BUF = (LPSTR) (LPCTSTR) STR;
BSTR variable
BSTR BSTRVALUE = :: sysallocstring (l "programmer");
Char * buf = _com_util :: ConvertBSTRTSTRING (BSTRVALUE);
Sysfreeestring (BStrValue);
AfxMessageBox (BUF);
DELETE (BUF);
CCOMBSTR variable
CCOMBSTR BSTRVAR ("Test");
Char * buf = _com_util :: convertBSTRTSTRING (BSTRVAR.M_STR);
AfxMessageBox (BUF);
DELETE (BUF);
_BSTR_T variable
_BSTR_T type is package to BSTR, because it has been overloaded = operator, so it is easy to use
_BSTR_T BSTRVAR ("Test");
Const char * buf = bstrvar; // / Do not modify the content in BUF
AfxMessageBox (BUF);
General method (for non-COM data type)
Complete conversion with Sprintf
Char buffer [200];
CHAR C = '1';
INT i = 35;
Long J = 1000;
Float f = 1.7320534F;
Sprintf (buffer, "% c", c);
Sprintf (buffer, "% d", i);
Sprintf (buffer, "% d", j);
Sprintf (buffer, "% f", f);
Second, the string is converted to other data types
STRCPY (TEMP, "123");
INT
I = ATOI (TEMP);
Long integer (long)
l = atol (TEMP);
Double
D = ATOF (TEMP);
CSTRING variable
CString name = TEMP;
BSTR variable
BSTR BSTRVALUE = :: sysallocstring (l "programmer");
... // Complete using sysfreestring (BStrValue) for BSTRVALUE;
CCOMBSTR variable
CCOMBSTR type variable can be directly assigned
CCOMBSTR BSTRVAR1 ("TEST");
CCOMBSTR BSTRVAR2 (TEMP);
_BSTR_T variable
_BSTR_T type variable can be directly assigned
_BSTR_T BSTRVAR1 ("TEST");
_BSTR_T BSTRVAR2 (TEMP);
Third, other data types are converted to cstring
Use the cstring member function Format to convert, for example:
Integer (Int)
Str.Format ("% d", i);
Floating point number (float)
Str.Format ("% f", i);
String pointer (char *), etc., has been supported by the CSTRING constructor can be assigned
Str = username;
For data types that Format are not supported, you can transfer to Char * by transformation of other data types to the char *, and then assign the CString variable.
Fourth, BSTR, _BSTR_T and CCOMBSTR
CCOMBSTR is the package of ATL to BSTR, _BSTR_T is C to BSTR package, BSTR is a 32-bit pointer, but does not directly point to buffers of strings.
CHAR * Convert to BSTR can be like this:
BSTR B = _COM_UTIL :: ConvertStringTobstr ("Data"); /// Before using Comutil.h and ComSupp.lib
Sysfreeestring (BStrValue);
Otherwise
Char * p = _com_util :: ConvertBSTRTRTSTRING (B);
Delete P;
Specifically, refer to the specific description of the second paragraph.
CCOMBSTR is overloaded with _BSTR_t, which can be done directly =,! =, ==, etc., so it is very convenient to use.
Especially _bstr_t, it is recommended to use it.
Five, Variant, _variant_t and Colevariant
Variant's structure can refer to the definition of struct tagvariant in the header file VC98 / include / OAIDL.H.
For the assignment of Variant variables: First assign a value to the VT member, indicate the data type, and then assign a value to the same data type in the joint structure, and give an example:
Variant VA;
INT A = 2001;
va.vt = vt_i4; // indicates integer data
va.lval = a; // / assignment
For Variant that doesn't assign values, it is best to use Void Variantinit (Variantarg Far * Pvarg); in initialization, the essence is set to vt_empty, the following table, the following table, the following table, the following table, the corresponding relationship of VT and common data:
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 | * .bete 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 CVAL; // 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.
_Variant_t is a Variant's package class whose assignment can use forced type conversion, and its constructor automatically processes these data types.
Plus #include
E.g:
Long L = 222;
ING I = 100;
_VARIANT_T LVAL (L);
LVAL = (long) i;
The use of Colevariant is basically the same as _variant_t, please refer to the following example:
Colevariant V3 = "String", V4 = (long) 1999;
CString str = (bstr) v3.pbstrval;
Long i = v4.lval;
Sixth, some other COM data type
Get CLSID according to Progid
HRESULT CLSIDFROGID (LPCOLESTR LPSZPROGID, LPCLSID PCLSID);
CLSID CLSID;
CLSIDFROMPROGID (L "Mapi.Folder", & clsid;
Get Progid according to CLSID
Winoleapi PROGIDFROMCLSID (REFCLSID CLSID, LPoLESTR * LPLPSZPROGID);
For example, we have defined a CLSID_IAPPLICATION, the following code gets progid
LPolestr pprogid = 0; progidFromclsid (CLSID_IApplication, & PProgID);
... // / Can use PPROGID
CotaskMemfree (PPROGID); / / Don't forget to release
Seven, ANSI and Unicode
Unicode is called a wide character string, and it is all Unicode strings in COM.
Convert ANSI to Unicode
(1) Implementing this macro, for example: CLSIDFROMPROGID (L "Mapi.Folder", & clsid;
(2) Implement conversions through the multibytetowideChar function, for example:
Char * szprogid = "mapi.folder";
Wchar szwideprogid [128];
CLSID CLSID;
Long Llen = MultibyToWideChar (CP_ACP, 0, SZProgid, StrogID), SzwideProgid, SizeOf (SzwideProgid);
SzwideProgid [llen] = '/ 0';
(3) Implementation through A2W macro, for example:
Uses_Conversion;
CLSIDFROMPROGID (A2W (SZProgid), & clsid;
Convert Unicode to ANSI
(1) Use WideChartomultibyte, for example:
/ / Suppose already has a Unicode string wszsomeString ...
Char szanstruing [max_path];
WideChartomultibyte (CP_ACP, WC_CompositeCheck, Wszsomestring, -1, Szanstructed, Sizeof (Szanstructed), NULL, NULL;
(2) Use W2a macro to achieve, for example:
Uses_Conversion;
PTEMP = W2A (WSZSomstring);
Eight, other
In the processing of the message, we often need 32-bit data (DWORD) such as WPARAM or LPARAM to be decomposed into two 16-bit data (Word), for example:
LParam Lparam;
Word Lovalue = loword (lparam); /// Take 16 bits
Word HiValue = HiWord (LPARAM); /// Take 16
For 16-bit data (Word) we can decompose to two 8-bit data (byte) with the same method, for example:
Word wvalue;
Byte Lovalue = LobyTe (WVALUE); /// Take 8 digits
BYTE HIVALUE = Hibyte (Wvalue); /// Take 8 digits
Two 16-bit data (Word) synthesis 32-bit data (DWORD, LRESULT, LPARAM, or WPARAM)
Long Makelong (Word Wlow, Word WHIGH);
WParam MakewParam (Word Wlow, Word WHIGH);
LParam MakelParam (Word Wlow, Word WHIGH);
LResult MakelResult (Word Wlow, Word WHIGH);
Two 8-bit data (Byte) synthesized 16-bit data (Word)
Word Makeword (Byte Blow, Byte Bhigh);
From R (RED), G (Green), B (Blue) three colors get color value of ColorRef types
ColorRef RGB (byte Byred, Byte Byte Byblue);
For example, ColorRef Bkcolor = RGB (0x22, 0x98, 0x34);
From the color type color value to RGB three color values byte red = getRvalue (bkcolor); // Nest red color
Byte green = getGvalue (bkcolor); // get green color
Byte blue = getbvalue (bkcolor); // gets the blue color
Nine, precautions
If you need to use the CONVERTBSTRTSTRING such function, you need to add the header file Comutil.h and add comsupp.lib or directly below #pragma Comment (Lib, "COMSUPPP.LIB").