PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Why TCHAR?

As far as I can tell, GapiDraw, and WinCE only work with wide chars for text. My question is, why use TCHAR over standard wchar_t? Isn't TCHAR basically just a typedef of wchar_t? As far as I can tell that is all it is, but you have to use seperate functions for it (_stprintf vs swprintf). What are the advantages to this, or the reason?
I think the difference isn't in the WinCE version, I think it is in the x86 (xp) version. In the WinCE SDKs, TCHARs are defined as 2 byte characters, while in the regular Win SDK, they are defined as 1 byte chars. Thus, with TCHAR, you get simple portability.

Now, I don't know if wchar_t has the same portability, but I'm guessing that it doesn't. I also see no reason why wsprintf wouldn't work with a TCHAR in the WinCE compile even if you have to cast it. _stprintf is also portable, since it was built specifically around TCHARs.

I may not be entirely accurate, but I'm pretty sure that that is the reasoning. A much more seasoned developer than I will have to confirm that.
A TCHAR can be either a char or a wide char, depending on if the compiler has unicode defined or not. This way, the same functions can be used for both. It can be very helpful for cross-platform. Pocket PCs use unicode, and usually Windows does not. Just be aware, that a file with a tchar stored in it may be created differently with a Windows system compared to a PPC. It took me for ever to figure that out.
Just a little note on CpuWhiz105's reply: Windows actually uses only unicode (2 byte) strings, but it also accepts ASCII strings if you don't define UNICODE.
Since it simply converts the text to Unicode back and forth, it's wise to always use Unicode because you'll be saving lots of string conversions on each API call.
The TCHAR macro is typically char on PC, wchar_t on Pocket PC.

The _stprintf macro is typically sprintf on PC, wsprintf on Pocket PC.

They are just macros and typedefs, so I suppose you could use TCHAR on Pocket PC and use wsprintf directly.

It's probably best to either use the generic text macros and typedefs (if you want portability), or to not use them if you don't.
Actually, Windows 95, 98, and possibly ME (don't know for sure about ME) are ANSI only. The NT family (NT 4.0, 2000 and XP) support Unicode.

The primary thing you have to look out for with UNICODE is to pay attention if a function needs the size of a buffer in bytes, or the number of chars. With ANSI that is synonomous, while it is not with UNICODE. So anywhere you need the size of a buffer in characters, you can do something like this:

Code:

TCHAR buffer[256];
int size_of_buf_in_chars=sizeof(buffer)/sizeof(TCHAR);


That works properly for both ANSI and UNICODE builds.

Dan East