Visual C + +. String conversion method in net

Source: Internet
Author: User
Tags constant
Visual C + +. NET involves Atl/atl Server, MFC and managed C + + and so on a variety of programming methods, not only powerful but also widely used. In programming, we often encounter a string conversion operation that is ANSI, Unicode, and BSTR different encoding types. This article first introduces the basic string types, then describes the related classes, such as CComBSTR, _bstr_t, CStringT, and so on, and finally discusses their conversion methods, which also includes conversion classes and macros that use the latest ATL7.0, such as CA2CT, Ca2tex, and so on.

I. BSTR, LPSTR and LPWSTR

In Visual C + +. NET, we often use some of these basic string types, such as BSTR, LPSTR, and LPWSTR. These data types appear similar to these because of data interchange between different programming languages and support for ANSI, Unicode, and multibyte character sets (MBCS).

So what are BSTR, LPSTR and LPWSTR?

The BSTR (Basic string,basic string) is a Unicode string of type olechar*. It is described as a type that is compatible with automation. Because the operating system provides the appropriate API functions (such as sysallocstring) to manage it and some of the default dispatch code, BSTR is actually a COM string, but it is widely used in a variety of contexts other than automation technology. Figure 1 depicts the structure of the BSTR, where the DWORD value is the actual number of bytes in the string, and its value is twice times the Unicode character in the string.

LPSTR and LPWSTR are a string data type used by Win32 and VC + +. LPSTR is defined as a pointer to an array of 8-bit ANSI characters that ends with null (' "), and LPWSTR is a pointer to a null-terminated 16-bit double-byte character array. In VC + +, there are similar string types, such as LPTSTR, LPCTSTR, and so on, their meaning as shown in Figure 2.

For example, LPCTSTR means "long pointer to a constant generic string", which means "a long pointer type that points to a generic string constant," with the C + + const char* Mirror, while LPTSTR is mapped to char*.

Generally, there are also the following types of definitions:

#ifdef UNICODE
typedef LPWSTR LPTSTR;
typedef LPCWSTR LPCTSTR;
#else
typedef LPSTR LPTSTR;
typedef LPCSTR LPCTSTR;
#endif

Ii. CString, CStringA and CStringW

Visual C + +. NET, CStringT is used as the "generic" string class for Shared ATL and MFC, and it has CString, CStringA, and CStringW three forms, which manipulate strings of different character types respectively. These character types are TCHAR, char, and wchar_t. TCHAR is equivalent to WCHAR (16-bit Unicode character) in the Unicode platform and is equivalent to char in ANSI. wchar_t is usually defined as unsigned short. Because CString is often used in MFC applications, this is not repeated here.

Iii. variants, COleVariant and _variant_t

In OLE, ActiveX, and COM, a Variant data type provides a very efficient mechanism that, because it contains both the data itself and the type of the data, enables the transmission of various types of automated data. Now let's look at a simplified version of the variant definition in the OAIDL.H file:

struct tagVARIANT {
VARTYPE VT;
Union {
Short ival; Vt_i2.
Long lval; VT_I4.
float Fltval; Vt_r4.
Double dblval; VT_R8.
Date date; Vt_date.
BSTR Bstrval; Vt_bstr.
...
Short * PIVAL; Vt_byref| Vt_i2.
long * plval; Vt_byref| VT_I4.
float * PFLTVAL; 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, then we can read out the value of a variant from the ival. Also, when you assign a value to a Variant variable, you first indicate its type. For example:

VARIANT va;
:: VariantInit (&VA); Class
int a = 2002;
VA.VT = VT_I4; Indicates a Long data type
Va.lval = A; assigning values
For easy handling of variant variables, Windows also provides some very useful functions:

variantinit--the variable to vt_empty;

variantclear--eliminates and initializes a variant;

variantchangetype--change the variant type;

variantcopy--releases the memory attached to the target variant and copies the source Variant.

The COleVariant class is a wrapper over a variant structure. Its constructors are extremely powerful in that the object is constructed by first calling VariantInit for initialization, then invoking the corresponding constructor based on the standard type in the parameter, and using Variantcopy to perform the conversion assignment, when the variant object is not in the valid range. Its destructor is invoked automatically, and the corresponding memory is automatically purged because the destructor calls VariantClear. In addition, the COleVariant assignment operator provides great convenience for us with variant type conversions. 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, with the value "This is a test"
COleVariant v3 ((Long) 2002);
COleVariant v4 = (long) 2002;
The result is a VT_I4 type with a value of 2002

_variant_t is a Variant class for COM, and its functionality is similar to COleVariant. However, in Visual C + +. NET MFC application, you need to add the following two sentences before the code file:

#include "comutil.h"

#pragma comment (lib, "Comsupp.lib")

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.