I. BSTR, LPSTR and LPWSTR
In all programming methods of Visual C . Net, we often use some basic string types such as BSTR, LPSTR, and LPWSTR. These data types similar to those described above are due to data exchange between different programming languages and support for ANSI, Unicode and multi-character set (MBCS).
So what is BSTR, LPSTR, and LPWSTR?
BSTR (Basic String, Basic String) is an OLECHAR * type Unicode string. It is described as a type with automation compatibility. Since the operating system provides the corresponding API function (such as sysallocstring) to manage it and some default scheduling code, BSTR is actually a COM string, but it is widely used in many occasions other than automation technology. Figure 1 depicts the structure of the BSTR, where the DWORD value is the number of bytes that actually occupied in the string, and its value is twice the Unicode character in the string.
LPSTR and LPWSTR are a string data type used by Win32 and VC . LPSTR is defined as a 8-bit ANSI character array pointer to NULL ('/ 0'), and LPWSTR is a 16-bit double-word character array pointer that is pointed to NULL. In VC , there are similar string types, such as LPTSTR, LPCTSTR, etc., the meaning of them is shown in Figure 2.
For example, LPCTSTR refers to "Long Pointer TO A constant gener string", which means "a long pointer type pointing to the general string constant", which is mapped with C / C const char *, and LPTSTR is mapped to char *.
Generally, there is also the following types of definitions:
#ifDef unicode typeflpwstr lptstr; typedef lpcwstr; #else typedf lpstr lptstr; Typedef lpcstr lpctstr; #ENDIF
Second, cstring, cstringa and cstringw
Visual C . NET will use CStringt as a "general" string class for the shared of ATL and MFC, which has three forms of CString, CSTRINGA, and CStringW, which operate different character types. These character types are TCHAR, CHAR, and WCHAR_T. TCHAR is equivalent to Wchar (16-bit Unicode characters) in the Unicode platform, in ANSI medium price in char. Wchar_t is usually defined as unsigned short. Since CString is often used in the MFC application, it is no longer repeated.
Third, Variant, Colevariant and _variant_t
In OLE, ACTIVEX, and COM, the Variant data type provides a very effective mechanism, since it contains both the data itself, and the type of data is included, so it can achieve various different automated data transmission. Let's take a look at a simplified version of the Variant definition in the OAIDL.H file:
Struct tagvariant {var; union {short ival; // vt_i2. long lval; // vt_i4. float float float fltval; // vt_r4. Double dblval; // vt_r8. date date; // vt_date. bstr bstrval; // vt_bstr. ... short * pipe; // vt_byref | vt_i2. Long * plval; // vt_byref | vt_i4. Float * pfletval; // vt_byref | vt_r4. Double * pdblval; // vt_byref | vt_r8. Date * pdate; // vt_byref | vt_date BSTR * PBSTRVAL; // vt_byref | vt_bstr.};}; Obviously, the Variant type is a C structure that contains a type member VT, some reserved bytes, and a large Union type. For example, if VT is VT_i2, we can read the value of Variant from IVAL. Similarly, when it assigns a Variant variable, it must first specify its type. E.g:
Variant Va; :: Variantinit (& VA); // Initialization INT A = 2002; VA.VT = VT_I4; / / Indicates that long data type va.lval = a; // assignment
In order to facilitate the processing of Variant type variables, Windows also provides some very useful functions:
VariantInit - initialize the variable to vt_empty;
VARIANTCLEAR - Eliminating and initializing Variant;
VariantchangeType - Change the type of Variant;
VariantCopy - Releases memory connected to target Variant and copy the source Variant.
The Colevariant class is a package of Variant structure. Its constructor has an extremely powerful feature that first calls VariantInit when the object configuration is initialized, and then call the corresponding constructor according to the standard type in the parameter, and use VariantCopy to convert the assignment operation, when the Variant object is not valid, Its destructor will be automatically called, since the destructor is called VariantClear, so the corresponding memory will be automatically cleared. In addition, Colevariant's assignment operator provides us with great convenience in the conversion with Variant type. For example, the following code:
Colevariant V1 ("This Is A Test"); // Direct Construction Colevariant V2 = "This Is A Test"; / / The result is the VT_BSTR type, the value is "this is a test" Colevariant V3 ((long) 2002); Colevariant V4 = (long) 2002; / / The result is the VT_i4 type, the value is 2002
_variant_t is a Variant class for COM, which is similar to Colevariant. However, in Visual C . NET's MFC application needs to add the following two sentences in front of the code file:
#include "comutil.h"
#pragma comment (lib, "comsupp.lib")
Fourth, CCOMBSTR and _BSTR_T
CCOMBSTR is an ATL class package for the BSTR data type package, which is more convenient. E.g:
CCOMBSTR BSTR1; BSTR1 = "BYE"; // Direct assignment OLECHAR * STR = OLESTR ("ta ta"); // Length is 5 wide character ccombstr BSTR2 (WCSLEN (STR)); // Defines the length of 5WCSCPY (BSTR2 .m_str, str); // copy wide string to CCOMBSTR BSTR3 (5, OLESTR ("Hello World"); CCOMBSTR BSTR4; CCOMBSTR BSTR5 ("Hey There" ); Ccombstr BSTR6 ("hey there"); ccombstr BSTR7 (BSTR6); // When constructed, the content "Hey there" _bstr_t is C to the package of BSTR, its constructors and destructor calls sysallocstring, respectively. The sysfreestring function, other operations are borrowed the BSTR API function. Similar to _variant_t, add comutil.h and comsupp.lib when using it.
V. BSTR, CHAR * and CSTRING conversion
(1) Char * Convert to CString
If char * is converted into CString, in addition to direct assignment, CString :: Format can be used. E.g:
Char charray [] = "this is a test"; char * p = "this is a test";
or
LPSTR P = "this is a test";
Or in the UNICODE that has been defined
TCHAR * P = _t ("this is a test");
or
LPTSTR P = _T ("this is a test"); cstring theString = Charray; stay.format (_T ("% s"), charray; TheString = P;
(2) CSTRING is converted into char *
If the CString class is converted to a char * (lpstr) type, the following three methods are often used:
Method 1. Use forced conversion. E.g:
CString TheString ("this is a test"); lptstr lpsz = (lpctstr) theString;
Method 2, using strcpy. E.g:
CString TheString ("this is a test"); lptstr lpsz = new tchar [theString.getLength () 1]; _ TCSCPY (LPSZ, THSSTRING);
It should be noted that the second parameter of STRCPY (or Disabled Unicode / MBCS) is const wchar_t * (unicode) or const char * (ANSI), and the system compiler will automatically convert it.
Method 3, using cstring :: getBuffer. E.g:
CSTRING S ("this is a test"); lptstr p = s.getBuffer (); // Add the code IF (P! = Null) * p = _t ('/ 0') here S.ReleaseBuffer (); // Release after using it to use other CString member functions
(3) BSTR is converted into char *
Method 1, using ConvertBSTRTSTRING. E.g:
#include #pragma comment (lib, "comsupp.lib") int _tmain (int argc, _TCHAR * argv []) {BSTR bstrText = :: SysAllocString (L "Test"); char * lpszText2 = _com_util :: ConvertBSTRToString (bstrText ); Sysfreestring (bstrtext); // completely release delete [] lpsztext2; return 0;}
Method 2, using _BSTR_T's assignment operator overload. E.g:
_BSTR_T B = BSTRTEXT; char * lpsztext2 = B;
(4) CHAR * Convert to BSTR
Method 1. Use the API functions such as SysallocString. E.g:
BSTR bstrtext = :: sysallocstring (l "test"); bstr bstrtext = :: sysallocstringlen (l "test", 4); bstr bstrtext = :: sysallocstringBytelen ("test", 4);
Method 2, use Colevariant or _Variant_t. E.g:
// Colevariant Strvar ("this is a test"); _ variant_t strvar; bstr bstrtext = strvar.bstrval;
Method 3, using _bstr_t, this is the easiest way. E.g:
BSTR BSTRTEXT = _BSTR_T ("This Is A Test");
Method 4 uses CCOMBSTR. E.g:
BSTR BSTRTEXT = CCOMBSTR ("This Is A Test");
or
CCOMBSTR BSTR ("this is a test"); bstr bstrtext = BSTR.M_STR;
Method 5 uses ConvertStringTOBSTR. E.g:
Char * lpsztext = "test"; bstr bstrtext = _com_util :: ConvertStringTOBSTR (LPSZTEXT);
(5) CString Convert to BSTR
Usually achieved by using CStringt :: Allocsystring. E.g:
CString Str ("this is a test"); bstr bstrtext = str.allocsystring (); ... sysfreestring (bstrtext); // is released
(6) BSTR is converted into cstring
Generally, according to the following methods:
BSTR bstrtext = :: sysallocstring (l "test"); cstringa str; str.empty (); str = bstrtext;
or
CSTRINGA STR (BSTRTEXT);
(7) Conversion between ANSI, Unicode and Wide characters
Method 1. Use multibyTetowideCha to convert the ANSI characters to Unicode characters and use WideChartomultibyte to convert Unicode characters to an ANSI character.
Method 2, use "_t" to convert the ANSI to "General" type string, use "L" to convert ANSI to Unicode, and String an ANSI string is converted to string * objects in a managed C environment. For example: tchar TSTR [] = _t ("this is a test"); wchar_t wszstr [] = l "this is a test"; string * str = s "this is a test";
Method 3, using ATL 7.0 conversion macro and classes. ATL7.0 improves and adds many string conversion macros in the original 3.0 and provides the corresponding classes, which have a unified form shown in Figure 3:
Where the first C represents "class" to facilitate the difference between the ATL 3.0 macro, the second C represents the constant, 2 indicates "to", and the EX indicates to open up a certain size buffer. SourceType and DestinationType can be A, T, W and OLE, which are ANSI, Unicode, "General" types and OLE strings, respectively. For example, CA2CT is a string of ANSI to convert ANSI to a general type. Here are some sample code:
LPTSTR TSTR = Ca2Tex <16> ("this is a test"); lpctstr tcstr = Ca2CT ("this is a test"); wchar_t wszstr [] = l "this is a test"; char * chstr = CW2A (WSZSTR) ;
ASCII keyboard ASCII keyboard ASCII keyboard ASCII keyboard 27 ESC 32 Space 33! 34 "35 # 36 $ 37% 38 & 39 '40 (41) 42 * 43 44' 45 - 46. 47/48 0 49 1 50 2 51 3 52 4 53 5 54 6 55 7 56 8 57 9 58: 59; 60 <61 = 62> 63? 64 @ 65 A 66 B 67 C 68 D 69 E 70 F 71 G 72 H 73 i 74 J 75 K 76 L 77 M 78 N 79 o 80 P 81 q 82 R 83 S 84 T 85 u 86 V 87 W 88 x 89 Y 90 Z 91 [92/93] 94 ^ 95 _ 96 `97 A 98 B 99 C 100 D 101 E 102 F 103 G 104 H 105 i 106 J 107 K 108 L 109 M 110 N 111 o 112 P 113 q 114 R 115 S 116 T 117 U 118 V 119 W 120 x 121 y 122 z 123 {124 | 125} 126 ~
Test 1
A Character Pointer, String, And Bstr That Work with Ascii and Unicode Compilation.
LPCTSTR R;
R = text ("Hello");
CString S;
s = R;
BSTR B;
B = S.Allocsystring ();
Sysfreeestring (b);
When Compiled for Ascii R In Narrow, Type Const Char * S Is Narrow, Type Atl :: CStringt
Compiled in Unicode, The Cstring Type Turns ASCII String Litrals Into Wide Character strings.
CSTRING S1;
S1 = "Hello1";
CString S2 = "Hello2";
CString S3 ("Hello3");
When Compiled for ASCII, All 3 Strings Are Made Up of Narrow Characters. When Compiled for Unicode, All 3 Strings Are Made Up of Wide Characters, Even Though The String Litrals Are Not Escaped with L or Text ().
Notes
goal: a sample mfc app that i can compile ascii or unicode that has a string type that has functions like has, find, split, before that has a class that can produce a BSTR the class can produce ASCII text to write to a file even When Compiled for unicode string literals "Hello" L "Hello" text ("Hello") String Types CString LPCTSTSTSTSTSTSTSTSTSTST WCHAR * BSTR compile #ifdef _unicularode #else #ENDIF Functions MultibytetowideChar