String Change Method (ZT) in Visual C ++. Net

xiaoxiao2021-03-06  19

Visual C . Net involves a variety of programming methods such as ATL / ATL Server, MFC, and hosting C , not only powerful and widely used. In programming, we often encounter ANSI, Unicode, and BSTR different coding type string conversion operations. This article introduces the basic string type, then explains the related classes, such as CCOMBSTR, _BSTR_T, CSTRINGT, etc., and finally discusses their conversion methods, including the use of the latest ATL7.0 conversion classes, such as Ca2CT, Ca2Tex, etc. 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, wherein 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 . CSTRINGT as ATL and MFC sharing "General" string classes, which have three forms of CString, CSTRINGA, and CStringW, which operate different character types of strings. 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 data is included, it can achieve a variety of different The transmission of automated data. 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 - eliminate and initialize Variant; VariantChangeType - Change the type of Variant; VariantCopy - release and goals VARIANT is connected to 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 the 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 are an ATL class encapsulated on the BSTR data type, 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) ;

Sixth, conclusion

Almost all procedures are used to use strings, while Visual C . NET is more frequent due to powerful, and the conversion between strings is more frequent. This article is almost involved in all current conversion methods. Of course, for the .NET framework, the CONVERT and TEXT classes can be used to perform mutual conversion between characters and character encoding.

转载请注明原文地址:https://www.9cbs.com/read-44635.html

New Post(0)