This is a mirror of official site: http://jasper-net.blogspot.com/

What are TCHAR, WCHAR, LPSTR, LPWSTR, LPCTSTR (etc.)?

| Sunday, March 11, 2012
Many C++ Windows programmers get confused over what bizarre identifiers like TCHAR, LPCTSTR are. 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 char and wchar_t as 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:

char cResponse; // 'Y' or 'N'
char sUsername[64];

// str* functions

with

wchar_t cResponse; // 'Y' or 'N'
wchar_t sUsername[64];

// wcs* functions
 
You can simply code it:

#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 TCHAR would 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, _tcscat functions. 

Read more: Codeproject
QR: Inline image 1

Posted via email from Jasper-net

0 comments: