What is the type of lpctstr?

Source: Internet
Author: User

Explanation 1:

Lp-long pointer
C-const
T-Unicode/ANSI compatibility
Str-string

Explanation 2:

Lpcstr a 32-bit pointer to a constant character string.
Lpstr a 32-bit pointer to a character string.
Lpctstr a 32-bit pointer to a constant character string that is portable for Unicode and DBCS.
Lptstr a 32-bit pointer to a character string that is portable for Unicode and DBCS.

Explanation 3:

Lpcstr is a static char * Static 8-bit windows character (ANSI) without an ending string pointer
Lpctstr is static wchar_t * If Unicode has been defined, it is lpcwstr, otherwise it is

L indicates the long pointer, which is left behind by Windows 3.1 and other 16-bit operating systems. In Win32 and other 32-bit operating systems, long pointers, near pointers, and far modifiers are designed for compatibility. No practical significance.
P indicates that this is a pointer.
C Indicates a constant.
T in the Win32 environment, there is a _ t macro. This macro is used to indicate whether your character uses Unicode. If your program defines Unicode or other related macros, this character or string will be used as a unicode string, otherwise it will be a standard ANSI string.
STR indicates that this variable is a string.
Therefore, lpctstr indicates a string that points to a fixed address and can change the semantics according to some macro definitions.
Similarly, the lpcstr can only be an ANSI string. We need to use the T-type definition most of the time in the program.
Lpctstr = const tchar *

There are also:

Msdn, lpctstr: A 32-bit pointer to a constant character string

Bool a Boolean value.
BSTR a 32-bit character pointer.
Byte an 8-bit integer that is not signed.
Colorref a 32-bit value used as a color value.
DWORD a 32-bit unsigned integer or the address of a segment and its associated offset.
Long a 32-bit signed integer.
Lparam a 32-bit value passed as a parameter to a window procedure or callback function.
Lpcstr a 32-bit pointer to a constant character string.
Lpstr a 32-bit pointer to a character string.
Lpctstr a 32-bit pointer to a constant character string that is portable for Unicode and DBCS.
Lptstr a 32-bit pointer to a character string that is portable for Unicode and DBCS.
Lpvoid a 32-bit pointer to an unspecified type.
Lresult a 32-bit value returned from a window procedure or callback function.
Uint a 16-bit unsigned integer on Windows versions 3.0 and 3.1; a 32-bit unsigned integer on win32.
Wndproc a 32-bit pointer to a window procedure.
Word a 16-bit unsigned integer.
Wparam a value passed as a parameter to a window procedure or callback function: 16 bits on Windows versions 3.0 and 3.1; 32 bits on win32.
Data Types unique to the Microsoft Foundation Class Library include the following:
Position A value used to denote the position of an element in a collection; used by MFC collection classes.
Lpcrect a 32-bit pointer to a constant (nonmodifiable) rect structure.

L indicates the long pointer.

This is to be compatible with Windows 3.1 and other 16-bit operating systems. In Win32 and other 32-bit operating systems, long pointers, near pointers, and far modifiers are designed for compatibility. No practical significance.
P indicates that this is a pointer.

C Indicates a constant.

T indicates that there is a _ t macro In the Win32 environment.

This macro is used to indicate whether your character uses Unicode. If your program defines Unicode or other related macros, this character or string will be used as a unicode string, otherwise, it is a standard ANSI string.

STR indicates that this variable is a string.
Therefore, lpctstr indicates a string that points to a fixed address and can change the semantics according to some macro definitions.
Similarly, the lpcstr can only be an ANSI string. We need to use the T-type definition most of the time in the program.

Lpctstr = const tchar *

Cstring and lpctstr are common. The reason is that the automatic type conversion defined by cstring is nothing special, and the simplest C ++ operator is heavy load.
The distinction between constant string ANSI and Unicode is determined by macro _ T. However, when _ T ("ABCD") is used, the string "ABCD" determines whether it is char * or w_char * based on whether it is specified as _ Unicode during compilation *. Similarly, tchar is the same as the target macro. You can see the definition. For the sake of simplicity, we will only introduce ANSI, Unicode, and so on.
In ANSI, The lpctstr is a const char *, which is a constant string (which cannot be modified ).
Lptstr is a char *, that is, a common string (very large, modifiable ).
Both are basic types, and cstring is C ++ class. compatible with these two basic types is the minimum task.
Since const char * is the simplest (constant, does not involve Memory changes, and the operation is fast), cstring directly defines a type conversion function.
Operator lpctstr () {...} directly returns the string maintained by the operator.
When you need a const char * and the cstring is passed in, the C ++ compiler automatically calls the cstring-heavy operator lpctstr () for implicit type conversion.
When cstring is required and const char * is passed in (in fact, char * can also be used), the C ++ compiler automatically calls the cstring constructor to construct a temporary cstring object.
Therefore, cstring and lpctstr are common.
But lptstr is different. It is char *, which means you can modify the data in it at any time. This requires memory management. (For example, if the string gets longer, the original storage space is not enough, you need to re-adjust the allocated memory ).
Therefore, you cannot forcibly convert const char * To char.
Example
Lpstr = (lpstr) (lpctstr) string;
This is an insecure method.
In this case, forced type conversion is used, and you have forced conversion. The C ++ compiler certainly won't reject you, but he also thinks that you do know what you want to do. Therefore, no warning is given.
Forced conversion of any type is a powerful feature of C (++), but it is also a major drawback. This problem is gradually improved in Versions later than vc6 (for VC only) (you need a more explicit type conversion Statement ).
In fact, you can see similar
Lpstr = (lpstr) (lpctstr) string;
This is generally because the constraints of the function are not well defined. For example, if a function accepts the input of a string parameter, there is no modification to the string, this parameter should be defined as const char *, but many beginners cannot understand the usage of const, or are too lazy. In short, it is written as char * at will *. In this way, the cstring must be forcibly converted.
This method is not safe and is not recommended. You must fully understand and confirm that the string is not modified.
Cstring to lptstr (char *). The predefined method is to call the getbuffer function of cstring, after use, you generally need to call the releasebuffer function to confirm the modification (in some cases, releasebuffer is not called, and you need to be very clear why this can be done in such a way, the general application environment does not consider this situation ).
At the same time, it should be noted that, between getbuffer and releasebuffer, the memory allocated by cstring is handled by you, so you cannot call other cstring functions.

Cstring to lpctstr:
Cstring CSTR;
Const char * maid = (lpctstr) CSTR;
To convert the string type to the cstring type:
Maid;
Cstring CSTR = lpctstr;

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.