Visual C ++. NET String Conversion Method [1]

xiaoxiao2021-03-06  58

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, 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.

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

New Post(0)