Tchar can be interpreted as char and wchar_t according to the defined compilation variables. You must add a tchar. h file to the object.
Lpxxx is actually a pointer to a string of the corresponding type (LP -- long pointer)
For details, see the following:
In general, a character can be 1 byte or 2 bytes. lets say 1-byte character is ANSI, using which English characters are represented. and lets say 2-byte character is Unicode, which can
Represent all versions in the world.
VC ++ supportchar
Andwchar_t
As
Native PES ypes For ANSI and Unicode characters respectively.
What if you want your C/C ++ program to be character-mode independent?
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 includeTCHAR cResponse; // 'Y' or 'N'TCHAR sUsername[64];// _tcs* functions
Thus, when your project is being compiled as Unicode,TCHAR
Wocould
Translatewchar_t
. If it is being compiled as ANSI/MBCS, it wowould translatedchar
.
Likewise, instead of usingstrcpy
,strlen
,strcat
(Including
The secure versions suffixed with _ s); orwcscpy
,wcslen
,wcscat
(Including
Secure), you can simply use_tcscpy
,_tcslen
,_tcscat
Functions.
When you need to express hard-coded string, you can use:
"ANSI String"; // ANSIL"Unicode String"; // Unicode_T("Either string, depending on compilation"); // ANSI or Unicode// or use TEXT macro, if you need more readability.
The non-prefixed string is ANSI string, the L prefixed
String is Unicode, and string specified in_T
OrTEXT
Wocould
Be either, depending on compilation.
String classes, like MFC/ATL'sCString
Implement two version using
Macro. There are two classes namedCStringA
For ANSI,CStringW
For
Unicode. When you useCString
(Which is a macro/typedef ),
It translates to either of two classes.
Okay.TCHAR
Type-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?
// ANSI charactersfoo_ansi(char*);foo_ansi(const char*);/*const*/ char* pString;// Unicode/wide-stringfoo_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 aboutTCHAR
Stuff, you 'd definitely select the last
One as your choice. But here is better alternative. Before that, note thatTCHAR.H
Header file declares onlyTCHAR
Datatype
And for the following stuff, you need to includeWindows.h
(Defined inWinNT.h
).
NOTE: If your project implicitly or explicitly has desWindows.h
,
You need not to includeTCHAR.H
- Char *Replacement:
LPSTR
- Const char *Replacement:
LPCSTR
- Wchar *Replacement:
LPWSTR
- Const wchar *Replacement:
LPCWSTR
(CBeforeW, Sinceconst
Is beforeWCHAR
)
- Tchar *Replacement:
LPTSTR
- Const tchar *Replacement:
LPCTSTR
Now, I hope, you understand the following signatures:
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 not number
Of bytes. For example ::
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 usenew
:
LPTSTR pBuffer; // TCHAR* pBuffer = new TCHAR[128]; // Allocates 128 or 256 BYTES, depending on compilation.
But if you use memory allocation functions likemalloc
,LocalAlloc
,GlobalAlloc
Etc;
You must specify the number of bytes!
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.