Differences between char, tchar, wchar, lpstr, lpwstr, and lpctstr in Windows Programming

Source: Internet
Author: User

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 ++ supportcharAndwchar_tAs
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,_tcscatFunctions.

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_TOrTEXTWocould
Be either, depending on compilation.


String classes, like MFC/ATL'sCStringImplement two version using
Macro. There are two classes namedCStringAFor ANSI,CStringWFor
Unicode. When you useCString(Which is a macro/typedef ),
It translates to either of two classes.


Okay.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?

// 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 aboutTCHARStuff, you 'd definitely select the last
One as your choice. But here is better alternative. Before that, note thatTCHAR.HHeader file declares onlyTCHARDatatype
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, SinceconstIs 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,GlobalAllocEtc;
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.

Related Article

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.