SDK programming notes - UNICODE
[Simple foundation but important]
Chapter 2, PROGRAMMING Windows, a very detailed explanation of all aspects of Unicode. We need to know that it changes to C language and Windows programming.
· C language related
There is also a wide character inner WCHAR_T in CHAR and C language. Wchar_t is defined as: typedef unsigned short wchar_t; obviously it is 16 bits. Wchar_t types of normal strings should be written like this: l "hello". Therefore, it can be defined such a wide character pointer WCHAR_T * PWC = L "Hello"; There are two versions for string functions and C language. If there is WCSLEN, WPRINTF is a wide character version of Printf. Of course, these two versions of functions use their own type of parameters because of how can CHAR and UNSIGNED SHORT? The compiler will not let you go. The following table is comparison to Char, Wchar_t related content:
(Test Environment: VC 6.0) CHAR type WCHAR_T type type size (32-bit system) 8-bit 16-bit constant character indicator 'a'l'a' or 'a' constant string representation 'Hello'L'Hello 'Some use methods char c =' a '; (C: 0x41) WCHAR_T WC =' a '; (WC value: 0x0041) char * p =' Hello '; wchar_t * pw = l "Hello"; Sizeof Sizeof ("Hello") = 6 SizeOf (L "Hello") = 12 Calculation length strlen ("Hello") = 5WCSLEN (L "Hello) = 5
Since there are two versions of the function, what should I do if we want to support them in the program? If you use VC , you only have some macros that are compatible with two character sets as long as you include Tchar.h header files in the program. E.g:
#ifdef _UNICODE typedef wchar_t _TCHAR; typedef wchar_t TCHAR; #define __T (x) L ## x #define _tprintf wprintf #define _tcslen wcslen ...... # else typedef char _TCHAR; typedef char TCHAR; #define __T (x ) x #define _tprintf printf #define _tcslen strlen ... # Endif # define _t (x) __t (x) #define _text (x) __t (x)
We only need to use tchar, _tpirntf, _tcslen, _text, etc., you can take into account two character sets.
· Windows related
Windows2000 / NT fully supports Unicode, and Windows98 supports very little to Unicode. What we care about writing procedures, which can be compiled into unicode, but also compile to support Unicode. Some macros are defined in the header file of the Windows SDK to complete this task.
typedef char CHAR; typedef wchar_t WCHAR; // wctypedef CHAR * PCHAR, * LPCH, * PCH, * NPSTR, * LPSTR, * PSTR; typedef CONST CHAR * LPCCH, * PCCH, * LPCSTR, * PCSTR; typedef WCHAR * PWCHAR, * LPWCH, * PWCH, * NWPSTR, * LPWSTR, * PWSTR; TYPEDEF Const Wchar * LPCWCH, * PCWCH, * LPCWSTR, * PCWSTR; and standard C, Windows C also uses Tchar as a compatible type.
#ifdef UNICODE typedef WCHAR TCHAR, * PTCHAR; typedef LPWSTR LPTCH, PTCH, PTSTR, LPTSTR; typedef LPCWSTR LPCTSTR; #define __TEXT (quote) L ## quote #else typedef char TCHAR, * PTCHAR; typedef LPSTR LPTCH, PTCH, PTSTR , Lptstr; typedef lpcstr lpctstr; #define __text (quote) quote # Endif
There is also a Text macro, equivalent to __text macro: #define text (quote) __text (quote).
When you write a Win32 SDK program, you often use the following code when you register the window class:
IF (! RegisterClass (& Wndclass)) {MessageBox (Null, Text ("this Program Requires WinodWS 2000 / NT"), SZAPPNAME, MB_ICONERROR); RETURN 0;}
Thus, when running the Unicode using the Unicode is running under Windows 98, the program can give a prompt and exit. Why can this code run under Win98? Because MessageBox is replaced with MessageBoxw under the definition of Unicode, MessageBoxW is one of the few wide character Windows functions supported by Win98. In summary, use a macro like Tchar, PTSTR, LPCTSTR, and Text when writing a Windows SDK program.
THE END