zhuang@linux:~/reading/programming-windows/02-an-introduction-to-unicode/$ less
An Introduction to Unicode
This post extracts some knowledge from Programming Windows Chapter 2 – An Introduction to Unicode.
Maintaining a Single Source
One answer is to use the TCHAR.H header file included with Microsoft Visual C++. This header file is not part of the ANSI C standard, so every function and macro definition defined therein is preceded by an underscore.
TCHAR.H provides a set of alternative names for the normal run-time library functions requiring string parameters (for example, _tprintf and _tcslen ). These are sometimes referred to as “generic” function names because they can refer to either the Unicode or non-Unicode versions of the functions.
If an identifier named _UNICODE is defined and the TCHAR.H header file is included in your program, _tcslen is defined to be wcslen:
#define _tcslen wcslen
If UNICODE isn’t defined, _tcslen is defined to be strlen:
#define _tcslen strlen
If the _UNICODE identifier is defined, a macro called __T is defined like this:
#define __T(x) L##x
This is fairly obscure syntax, but it’s in the ANSI C standard for the C preprocessor. That pair of number signs is called a “token paste,” and it causes the letter L to be appended to the macro parameter. Thus, if the macro parameter is “Hello!”, then L##x is L"Hello!".
If the _UNICODE identifier is not defined, the __T macro is simply defined in the following way:
#define __T(x) x
Regardless, two other macros are defined to be the same as __T:
#define _T(x) __T(x)
#define _TEXT(x) __T(x)UNICODE vs _UNICODE
In Microsoft Visual Studio, _UNICODE is a preprocessor macro that is typically used with UNICODE to control whether to use the wide character (UTF-16) version of the Windows API.
| macro | functions |
|---|---|
UNICODE | control Windows API(e.x. CreateFile → CreateFileW) |
_UNICODE | control C running time library(e.x. _tprintf) |
In Visual Studio: Enabling the Unicode character set will automatically add the UNICODE;_UNICODE macro.
zhuang@linux:~/reading/programming-windows/02-an-introduction-to-unicode/$ comments