L represents a long pointer this is for compatibility with 16-bit operating systems such as Windows 3.1, and in Win32 and other 32-bit 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
STR indicates that this variable is a string in detail. 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.
So LPCTSTR represents a string that points to a constant fixed address that can change semantics based on some macro definitions.
We use a type definition with T most of the time in the program.
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, operations are fast), CString directly defines a type conversion function:
Operator LPCTSTR ()
The function returns the maintained string directly.
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, it is char*, it means that you can change the data inside at any time, this requires memory management (such as the string is longer, 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.
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 can be seen similar to LPSTR 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:
const char *lpctstr= (LPCTSTR) cStr;
LPCTSTR Turn CString:
CString and Lpcstr Differences (RPM)