Introduction

To write Win32 application or MFC application using C++ you may decide to support Unicode or not. TCHAR library provides us macros so that we can easily switch between those two options.

Tips and Tricks

If you want to use Unicode in project properties if Visual Studio Solution Explorer, you have to select "Use Unicode Character Set" as character set. After that, you can easily use Unicode strings in your code wrapping with a macro. The same macro works fine even if you selected not to use "Unicode Character Set" and you use the character set that ANSI supports for example, english alphabets. Code remains same while you have the advantage.

To use a Unicode string as a function parameter, wrap it with macro _T or TEXT macro. For example:

AfxMessageBox(_T("You clicked it!"));

_T() and TEXT() are macros from tchar header file. TCHAR library automatically maps functions to Unicode when Unicode is defined. Using TCHAR library helps us to move code to multibyte stream (or unicode) whenever required. Try to avoid primitive data type char array or char *. This is because before using them in your controls, you have to convert them. Repetitive conversion may be tedious.

Use TCHAR instead of char and use TCHAR* instead of char*. TCHAR* can be written LPTSTR. For const TCHAR*, you may write LPCTSTR which is required when a string is passed as an argument to a function where modification should be restricted.

To calculate length of strings, use _tcslen function:

len = _tcslen(str);

To compare strings instead of strcmp and strncmp, use _tcscmp and _tcsncmp. Here’s an example:

Comments and Discussions

The first recommendation should be to avoid C-style strings alltogether. Whenever possible, use a proper string class, like CString or the std::string family. Thus, you get rid of many of the problems of using arrays of characters as strings, like how to grow them, having to remember to put NULL at the end, having to remember to check sizes before doing certain things...

Need a new cell phone? We supply most of the major carriers. Telus in Canada. Flash, Verizon, T-Mobile and Sprint in the USA. O2, talkmobile, tmobile, orange, three, and vodafone in Europe. See my website for details.Within you lies the power for good - Use it!