What are TCHAR, WCHAR, LPSTR, LPWSTR, LPCTSTR (etc.)?
2012-03-12 17:18
531 查看
Many C++ Windows programmers get confused over what bizarre identifiers like
Here, in brief, I would try to clear out the fog.
In general, a character can be 1 byte or 2 bytes. Let's say 1-byte character is ANSI, using which English characters are represented. And let's say 2-byte character is Unicode, which can represent
ALL languages in the world.
VC++ support
native datatypes for ANSI and Unicode characters respectively.
What if you want your C/C++ program to be Character-mode independent?
Use generic characters. That means, instead of replacing:
Collapse | Copy
Code
with
Collapse | Copy
Code
You can simply code it:
Collapse | Copy
Code
Thus, when your project is being compiled as Unicode, the
translate to
Likewise, instead of using
the secure versions suffixed with _s); or
secure), you can simply use
When you need to express hard-coded
you can use:
Collapse | Copy
Code
The non-prefixed
ANSI
string is Unicode, and
be either, depending on compilation.
two versions using macro. There are two classes named
Unicode. When you use
it translates to either of two classes.
Okay. The
was for a single character. You can definitely declare an array of
What if you want to express a character-pointer,
or a const-character-pointer - Which one of the following?
Collapse | Copy
Code
After reading about
you'd definitely select the last one as your choice. But here is a better alternative. Before that, note that TCHAR.H header
file declares only
and for the following stuff, you need to include Windows.h (defined in WinNT.h).
NOTE: If your project implicitly or explicitly includes Windows.h,
you need not include TCHAR.H
char* replacement:
const char* replacement:
WCHAR* replacement:
const WCHAR* replacement:
since
TCHAR* replacement:
const TCHAR* replacement:
Now, I hope you understand the following signatures
:
Collapse | Copy
Code
Continuing. You must have seen some functions/methods asking you to pass number of characters,
or returning the number of characters. Well, like
number of characters, and notnumber of bytes. For example::
Collapse | Copy
Code
On the other side, if you need to allocate number or characters, you must allocate proper number of bytes. In C++, you can simply use
Collapse | Copy
Code
But if you use memory allocation functions like
etc; you must specify the number of bytes!
Collapse | Copy
Code
Typecasting the return value is required, as you know. The expression in
argument ensures that it allocates desired number of bytes - and makes up room for desired number of characters.
TCHAR,
LPCTSTRare.
Here, in brief, I would try to clear out the fog.
In general, a character can be 1 byte or 2 bytes. Let's say 1-byte character is ANSI, using which English characters are represented. And let's say 2-byte character is Unicode, which can represent
ALL languages in the world.
VC++ support
charand
wchar_tas
native datatypes for ANSI and Unicode characters respectively.
What if you want your C/C++ program to be Character-mode independent?
Use generic characters. That means, instead of replacing:
Collapse | Copy
Code
char cResponse; // 'Y' or 'N' char sUsername[64]; // str* functions
with
Collapse | Copy
Code
wchar_t cResponse; // 'Y' or 'N' wchar_t sUsername[64]; // wcs* functions
You can simply code it:
Collapse | Copy
Code
#include<TCHAR.H> // Implicit or explicit include TCHAR cResponse; // 'Y' or 'N' TCHAR sUsername[64]; // _tcs* functions
Thus, when your project is being compiled as Unicode, the
TCHARwould
translate to
wchar_t. If it is being compiled as ANSI/MBCS, it would be translated to
char.
Likewise, instead of using
strcpy,
strlen,
strcat(including
the secure versions suffixed with _s); or
wcscpy,
wcslen,
wcscat(including
secure), you can simply use
_tcscpy,
_tcslen,
_tcscatfunctions.
When you need to express hard-coded
string,
you can use:
Collapse | Copy
Code
"ANSI String"; // ANSI L"Unicode String"; // Unicode _T("Either string, depending on compilation"); // ANSI or Unicode // or use TEXT macro, if you need more readability.
The non-prefixed
stringis
ANSI
string, the L prefixed
string is Unicode, and
stringspecified in
_Tor
TEXTwould
be either, depending on compilation.
Stringclasses, like MFC/ATL's
CStringimplement
two versions using macro. There are two classes named
CStringAfor ANSI,
CStringWfor
Unicode. When you use
CString(which is a macro/typedef),
it translates to either of two classes.
Okay. The
TCHARtype-definition
was for a single character. You can definitely declare an array of
TCHAR.
What if you want to express a character-pointer,
or a const-character-pointer - Which one of the following?
Collapse | Copy
Code
// ANSI characters foo_ansi(char*); foo_ansi(const char*); /*const*/ char* pString; // Unicode/wide-string foo_uni(WCHAR*); // or wchar_t* foo_uni(const WCHAR*); /*const*/ WCHAR* pString; // Independent foo_char(TCHAR*); foo_char(const TCHAR*); /*const*/ TCHAR* pString;
After reading about
TCHARstuff,
you'd definitely select the last one as your choice. But here is a better alternative. Before that, note that TCHAR.H header
file declares only
TCHARdatatype
and for the following stuff, you need to include Windows.h (defined in WinNT.h).
NOTE: If your project implicitly or explicitly includes Windows.h,
you need not include TCHAR.H
char* replacement:
LPSTR
const char* replacement:
LPCSTR
WCHAR* replacement:
LPWSTR
const WCHAR* replacement:
LPCWSTR(C before W,
since
constis before
WCHAR)
TCHAR* replacement:
LPTSTR
const TCHAR* replacement:
LPCTSTR
Now, I hope you understand the following signatures
:
Collapse | Copy
Code
BOOL SetCurrentDirectory( LPCTSTR lpPathName ); DWORD GetCurrentDirectory(DWORD nBufferLength,LPTSTR lpBuffer);
Continuing. You must have seen some functions/methods asking you to pass number of characters,
or returning the number of characters. Well, like
GetCurrentDirectory, you need to pass
number of characters, and notnumber of bytes. For example::
Collapse | Copy
Code
TCHAR sCurrentDir[255]; // Pass 255 and not 255*2 GetCurrentDirectory(sCurrentDir, 255);
On the other side, if you need to allocate number or characters, you must allocate proper number of bytes. In C++, you can simply use
new:
Collapse | Copy
Code
LPTSTR pBuffer; // TCHAR* pBuffer = new TCHAR[128]; // Allocates 128 or 256 BYTES, depending on compilation.
But if you use memory allocation functions like
malloc,
LocalAlloc,
GlobalAlloc,
etc; you must specify the number of bytes!
Collapse | Copy
Code
pBuffer = (TCHAR*) malloc (128 * sizeof(TCHAR) );
Typecasting the return value is required, as you know. The expression in
malloc's
argument ensures that it allocates desired number of bytes - and makes up room for desired number of characters.
相关文章推荐
- What are TCHAR, WCHAR, LPSTR, LPWSTR, LPCTSTR (etc.)?
- What are TCHAR, WCHAR, LPSTR, LPWSTR, LPCTSTR (etc.)?
- What are TCHAR, WCHAR, LPSTR, LPWSTR, LPCTSTR (etc.)?
- What are TCHAR, WCHAR, LPSTR, LPWSTR, LPCTSTR (etc.)?
- What are TCHAR, WCHAR, LPSTR, LPWSTR, LPCTSTR (etc.)?
- What are TCHAR, WCHAR, LPSTR, LPWSTR, LPCTSTR (etc.)?
- What are TCHAR, WCHAR, LPSTR, LPWSTR, LPCTSTR
- (转)What are TCHAR, WCHAR, LPSTR, LPWSTR, LPCTSTR (etc.)?
- TCHAR, WCHAR, LPSTR, LPWSTR, LPCTSTR<一>
- TCHAR,WCHAR,LPSTR,LPWSTR和LPCTSTR
- 【原译】什么是TCHAR,WCHAR,LPSTR,LPWSTR,LPCTSTR.等等
- windows编程中的char, TCHAR, WCHAR, LPSTR, LPWSTR, LPCTSTR区别
- VC++中关于TCHAR,WCHAR,LPSTR,LPWSTR,LPCTSTR的解释[转载]
- VC++中关于TCHAR,WCHAR,LPSTR,LPWSTR,LPCTSTR的解释
- What are TCHAR, WCHAR, LPSTR, LPWSTR, LPCTSTR
- TCHAR, WCHAR, LPSTR, LPWSTR, LPCTSTR<二>
- VC++中关于TCHAR,WCHAR,LPSTR,LPWSTR,LPCTSTR的解释
- TCHAR, WCHAR, LPSTR, LPWSTR, LPCTSTR<三>
- TCHAR, WCHAR, LPSTR, LPWSTR, LPCTSTR区别
- TCHAR, WCHAR, LPSTR, LPWSTR, LPCTSTR 之间的联系与区别