Recently, I meet some tasks about the char/string on windows platform. I see that they are different char type like char, TCHAR, WCHAR, LPSTR, LPWSTR, LPCTSTR. Can someone give me some information about it? And how to use like the regular char and char *. I cam confused about these types?
Best Regards,
They are documented on MSDN. Here’s a few:
TCHAR: AWCHARifUNICODEis defined, aCHARotherwise.WCHAR: A 16-bit Unicode character.CHAR: An 8-bit Windows (ANSI) character.LPTSTR: AnLPWSTRifUNICODEis defined, anLPSTRotherwise.LPSTR: A pointer to a null-terminated string of 8-bit Windows (ANSI) characters.LPWSTR: A pointer to a null-terminated string of 16-bit Unicode characters.LPCTSTR: AnLPCWSTRifUNICODEis defined, anLPCSTRotherwise.LPCWSTR: A pointer to a constant null-terminated string of 16-bit Unicode characters.LPCSTR: A pointer to a constant null-terminated string of 8-bit Windows (ANSI) characters.Note that some of these types map to something different depending on whether
UNICODEhas been#define‘d. By default, they resolve to the ANSI versions:When you
#define UNICODEbefore#include <windows.h>, they resolve to the Unicode versions.They are in reality
typedefs to some fundamental types in the C and C++ language. For example:On compilers like Visual C++, there’s really no difference between an
LPCSTRand aconst char*or aLPCWSTRand aconst wchar_t*. This might differ between compilers however, which is why these data types exist in the first place!It’s sort of like the Windows API equivalent of
<cstdint>or<stdint.h>. The Windows API has bindings in other languages, and having data types with a known size is useful, if not required.