The origin and significance of LPTSTR, LPCSTR, LPCTSTR and LPSTR

Source: Internet
Author: User

UNICODE: It is a method that represents a character in two bytes. For example, the character ' a ' under ASCII is a character, can ' a ' under Unicode is two characters, characters is filled with 0, and the Kanji ' path ' is two bytes below ASCII, and still two bytes under Unicode.

The use of Unicode is to set the length of the world text , according to statistics, with two bytes can encode all the existing text without ambiguity.

MBCS: (Multi-bytechactactersystem) It is a multibyte character set, which is an indefinite length that represents the encoding of world text.

MBCS means the alphabet is the same as ASCII (which is why we tend to confuse MBCS with ASCII), but it requires multiple bytes to represent other text.

The programming under WINDOWS can support both MBCS and Unicode two encoded strings, depending on whether you define MBCS macros or Unicode macros.

The string pointers corresponding to MBCS macros are char* , or LPSTR;

Unicode corresponds to the pointer is unsigned short* is lpwstr, in order to write a program convenient for Microsoft to define the type LPTSTR, under MBCS he is char*, under Unicode It is unsigned char*, This allows you to redefine a macro for different character set conversions.

The meaning of LPTSTR, LPCSTR, LPCTSTR and LPSTR:

The Lpstr:32bit pointer points to a string of 1 bytes per character

The LPcstr:32-bit pointer points to a constant string, each character occupies a 1-byte LPCtstr:32-bit pointer to a constant string, each character may account for 1 bytes or 2 bytes, depending on whether Unicode is defined lptstr:32 -bit pointers may account for 1 bytes or 2 bytes per character, depending on whether Unicode is defined

Windows uses two character sets, ANSI and Unicode, which are generally used in single-byte mode, but it is inconvenient to handle double-byte characters such as Chinese, which is prone to half Chinese characters. The latter is a double-byte way to handle double-byte characters conveniently.

All character-related functions of windowsnt are available in two different versions, while windows9x only supports ANSI mode. _t is generally associated with a literal constant, such as _t ("Hello"). If you compile a program for ANSI mode, _t does not actually play any role. If you compile a program that is Unicode, the compiler saves the "Hello" string in Unicode mode. The difference between _t and _l is that _l no matter how you compile it, you save it in Unicode mode.

The first chapter of Windows core programming.

L is to indicate that the string resource is Unicode.

For example wchar_t str[] = L "Hello world!"; This is the Gemini-day storage character.

_t is an adaptable macro ~

When the #ifdef _unicode _t is L no #ifdef _UNICODE when _t is ANSI.

Like what

LPTSTR lpStr = new TCHAR[32]; tchar* szbuf = _t ("Hello"); The above two sentences are compiled correctly either under Unicode compilation conditions.

And MS recommends that you use a matching string function. For example, when dealing with LPTSTR or LPCTSTR, do not use strlen, but use _tcslen

Otherwise, strlen cannot process the wchar_t* string under the Unicode compilation condition.

T is a very interesting symbol (TCHAR, LPCTSTR, LPTSTR, _t (), _text () ... ), which represents the use of an intermediate type, which does not explicitly indicate the use of MBCS, nor does it explicitly use UNICODE. What kind of character set does it use?

In VC + + has a variety of string representations, as you say.

First, char* is a pointer to an ANSI character array, where each character occupies 8 bits (valid data is the other 7 bits that remove the highest bit), which preserves compatibility with the traditional c,c++.

The meaning of LP is the long pointer (pointer).

LPSTR is a pointer to an ANSI character array ending with '/0 ', which can be used interchangeably with char*, using LPSTR more frequently in Win32. The added meaning of ' C ' in LPCSTR is "CONSTANT" (constant), which indicates that an instance of this data type cannot be changed by using its API function, except that it is equivalent to LPSTR.

To meet the need for code internationalization, the industry has introduced the Unicode standard, which provides a simple and consistent way to express strings, where the bytes in all characters are 16-bit values, and the number can satisfy the coding needs of almost all written language characters in the world. Using Unicode (Type wchar_t) when developing a program is a encouraged practice.

LPWSTR and LPCWSTR, which are similar in meaning to LPSTR and LPCSTR, only character data is a 16-bit wchar_t instead of char.

Then, in order to achieve the common use of the two encodings, the definition of TCHAR is presented:

If _UNICODE is defined, it is declared as follows: typedef wchar_t TCHAR;

If _UNICODE is not defined, it is declared as follows: typedef char TCHAR;

The meaning of LPTSTR and LPCTSTR is that each character is such a TCHAR.

The characters in the CString class are declared as TCHAR types, which provide a well-encapsulated class for easy use by the user.

If you need further information, please refer to Http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt_data_type_ Mappings.asp and other related information.

Conversions between LPTSTR, LPCSTR, LPCTSTR, and LPSTR:

How do I understand the LPCTSTR type? 2007-11-10 21:43

L indicates long pointer

This is for compatibility with 16-bit operating systems such as Windows 3.1, in Win32 and in the other 32 operating systems, the long pointer and the near pointer and the far modifier are all for compatibility purposes. No practical significance.
P indicates that this is a pointer

C means a constant.

T means that in a Win32 environment, there is a _t macro

This macro is used to indicate whether your character uses Unicode, and if your program defines Unicode or other related macros, then the character or string will be used as a Unicode string, otherwise the standard ANSI string.

STR indicates that this variable is a string
So LPCTSTR represents a string that points to a constant fixed address that can change semantics based on some macro definitions. Similarly, LPCSTR can only be an ANSI string, and in the program we use a type definition with T most of the time.

LPCTSTR = = Const TCHAR *

CString and LPCTSTR can be said to be universal. The reason for this is the automatic type conversion defined by CString, nothing fancy, the simplest C + + operator overloading.
The distinction between a constant string ANSI and Unicode is determined by the macro _t. But with _t ("ABCD"), the string "ABCD" will be based on the compile-time negation of a _unicode to determine whether it is char* or w_char*. Similarly, TCHAR is the same as the target Word macros. Just look at the definition and understand. For simplicity, the following is only a case of ANSI, Unicode can be analogous.
In ANSI case, LPCTSTR is the const char*, which is a constant string (cannot be modified). And LPTStr is char*, that is, the ordinary string (the very amount, can be modified). Both of these are basic types, and CString is a C + + class, and it is the minimum task to be compatible with both of these basic types.
Because the const char* is the simplest (constant, does not involve memory changes, the operation is fast), CString directly defines a type conversion function operator LPCTSTR () {...}, directly returning the string that he maintains.
When you need a const char* and pass in CString, the C + + compiler automatically calls the CString overloaded operator LPCTSTR () for implicit type conversions. The C + + compiler automatically calls CString's constructor to construct a temporary CString object when CString is required and the const char* is passed in (in fact char* can).
therefore CString and LPCTSTR can be generally used.
But LPTStr is different, he is char*, means that you can change the data inside, this requires memory management (such as String length, the original storage space is not enough, you need to readjust the allocation of memory). Therefore, you cannot arbitrarily convert const char* to char* use. Example of the landlord lift LPSTR LPSTR = (LPSTR) (LPCTSTR) string; This is the unsafe way to use it.
This place uses coercion type conversions, you're all cast, and the C + + compiler certainly won't reject you, but at the same time he thinks you do know what you're doing. Therefore, no warning is given. The coercion of any type conversion is a powerful part of C (+ +), but it is also a big disadvantage. This problem is gradually improved in the later versions of VC6 (for VCs only) (you need a more explicit type conversion declaration).
In fact, in many places you can see similarLPSTR LPSTR = (LPSTR) (LPCTSTR) string; Usage, this is generally the reason why the constraint definition of a function is not perfect, such as a function accepts a string parameter input, there is no modification to the string, then the parameter should be defined as const char*, but many beginners can not clear the const usage, or lazy, In a word, I wrote the char* at random. This way, when you pass in CString, you need a forced conversion.
This practice is unsafe and is not recommended, and you must fully understand and confirm that the string has not been modified.
CString conversion to LPTSTR (char*), the intended practice is to call CString GetBuffer function, after use is generally called releasebuffer function to confirm the modification (in some cases, there is no call ReleaseBuffer , you also need to be very specific about why you should do this, and the general application environment may not consider this situation.
It is also important to note that between GetBuffer and ReleaseBuffer, CString allocates memory for you to handle, so you can no longer invoke other CString functions.

CString turn lpctstr:cstring cStr; const char *lpctstr= (LPCTSTR) cStr;
LPCTSTR turn Cstring:lpctstr LPCTSTR; CString Cstr=lpctstr;

This article from Csdn Blog, reproduced please indicate the source: http://blog.csdn.net/yuhuimin111/archive/2009/05/08/4161462.aspx

The origin and significance of LPTSTR, LPCSTR, LPCTSTR and LPSTR

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.