Wide and 2.2 C language
2.2.2 broader character
Wide character in the C language is based on short-type data, this data type is defined in the header file WCHAR.H is from:
typedef unsigned short wchar_t ;
Therefore wchar_t wide character data type with an unsigned short integer unsigned short C language, are 16 bits wide.
E.g:
wchar_t c = 'A' ;
Saved on your computer is 0x0041, appears as 0x41 0x00
2.2.3 Wide character library functions
When using a wide character length calculating strlen:
#include <stdio.h> #include <string.h> int main() { wchar_t *p = L"Hello" ; printf( "%d\n", strlen(p) ) ; return 0 ; }
The results show that after 1 run is, obviously, it does not obtain the correct length, this is because the wide-character string "Hello" period value stored in the memory as follows:
48 00 65 00 6C 00 6C 00 6F 00 21 00
This is because when strlen find the first 0:00 of the string is considered that the string has ended, so the length was obtained as a 1, strlen to count this one is the 0x48 character represented 'H'.
We use wcslen wide character length calculating function. Same thing
Function name |
Function prototype |
Function Function |
return value |
wcscat |
wchar_t *wcscat(wchar_t *s1, const wchar_t *s2); |
The connection string s1 s2 referred to later |
referring to the first address of the string s1 |
wcschr |
wchar_t *wcschr(const wchar_t *s, wchar_t c); |
Locate the first occurrence of the character c in the string s |
If found, the address of the character is returned, otherwise NULL |
wcscmp |
int wcscmp(const wchar_t *s1, const wchar_t *s2); |
Let the string s1 s2 compared with strings |
s1 <s2, returns negative; s1 == s2, returns 0; s1> s2, returns a positive number |
wcscpy |
wchar_t *wcscpy(wchar_t *s1, const wchar_t *s2); |
Copy the string referred s2 cover in s1 |
First address string referred s1 |
wcslen |
size_t wcslen(const wchar_t *s); |
S requirements referred string length |
Returns the number of valid characters |
wcsstr |
wchar_t * wcsstr(const wchar_t *s1, const wchar_t *s2); |
Find the position of the string in the string s1 s2 first occurrence |
If found, the address of the location is returned, otherwise returns NULL |
2.2.4 maintain a source code file
Use the TEXT avoid using L "hello" directly TEXT ( "hello")
TEXT retrospective definition of the place has been found that the addition of L
#define TEXT(quote) __TEXT(quote) // r_winnt #define __TEXT(quote) L##quote // r_winnt
Therefore, use the TEXT or _TEXT can not thank the L
2.3 wide and Windows
Type 2.3.1 Windows header file
Windows program includes a header file WINDOWS.H. The document includes a number of other header files, including WINDEF.H, the file has a number of basic patterns used in Windows-defined, and it is itself also includes WINNT.H. WINNT.H handle basic Unicode support.
WINNT.H comprising front headers CTYPE.H table C, which is one of the many C header file, including the definition of wchar_t. WINNT.H defines a new data type, referred to as CHAR and WCHAR:
typedef char CHAR ; typedef wchar_t WCHAR ; // wc
When you need to define eight characters or 16 characters, it is recommended that you use in Windows programs and data types are CHAR WCHAR. WCHAR Comment defined behind Hungarian notation is recommended: a variable based WCHAR data patterns may be attached in front of the letter to indicate a wide character wc.
Further WINNT.H header file defines a pointer to a string used as 8 and six data patterns used as four 8 const pointer to a string data type. Here a selection of header files some practical explanation data type statement:
typedef CHAR * PCHAR, * LPCH, * PCH, * NPSTR, * LPSTR, * PSTR ;
typedef CONST CHAR * LPCCH, * PCCH, * LPCSTR, * PCSTR ;
N and L represents the prefix "near" and "long", referring to the 16-bit Windows in two sizes different indicators. Win32 no difference in the near and long index.
Similarly, WINNT.H defined as six 16-bit pointer to a string data type as const, and four 16-bit pointer to a string data type:
typedef WCHAR * PWCHAR, * LPWCH, * PWCH, * NWPSTR, * LPWSTR, * PWSTR ;
typedef CONST WCHAR * LPCWCH, * PCWCH, * LPCWSTR, * PCWSTR ;
At this point, we have the data type CHAR (an 8-bit char) and WCHAR (a 16-bit wchar_t), and point to CHAR and WCHAR indicators. Like with TCHAR.H, WINNT.H TCHAR is defined as the general character type. If the UNICODE definition identifier (not the bottom line), and the TCHAR indicators pointing to TCHAR are defined as indicators of WCHAR and directed WCHAR; if the UNICODE identifier is not defined, and the point of TCHAR TCHAR index to point to, and are defined as char char indicators:
#ifdef UNICODE typedef WCHAR TCHAR, * PTCHAR ; typedef LPWSTR LPTCH, PTCH, PTSTR, LPTSTR ; typedef LPCWSTR LPCTSTR ; #else typedef char TCHAR, * PTCHAR ; typedef LPSTR LPTCH, PTCH, PTSTR, LPTSTR ; typedef LPCSTR LPCTSTR ; #endif
If you have defined TCHAR data type in a header file, or other header file, then WINNT.H and WCHAR.H header file can be defined to prevent their repetition. However, whenever the use of other header files in the program, should be included before any other WINDOWS.H header file.
WINNT.H header file also defines a macro that added prior to the first L-quoted strings. If UNICODE is defined identifier, it is called a macro __TEXT defined as follows:
#define __TEXT(quote) L##quote
If not defined identifier UNICODE, like the definition of __TEXT macro:
#define __TEXT(quote) quote
Further, TEXT macro is thus defined:
#define TEXT(quote) __TEXT(quote)
This method TCHAR.H _TEXT macro defined in the same, but do not have to worry about the bottom line. I will use this macro TEXT version of the book.
These definitions allow you mix ASCII and Unicode strings in the same program, or may be written in a compiled program ASCII or Unicode. If you want to explicitly define the 8-bit character variables and strings, use CHAR, PCHAR (or other), as well as a quoted string. To explicitly use 16-bit character variables and strings, use WCHAR, PWCHAR, and L added to the front of the quotation mark. For 8-bit or 16-bit depending on the definition of a variable or an identifier UNICODE string to use TCHAR, PTCHAR and TEXT macro.
2.3.2 Windows function calls
MessageBoxA:
WINUSERAPI int WINAPI MessageBoxA (HWND hWnd, LPCSTR lpText, LPCSTR lpCaption, UINT uType) ;
Here is MessageBoxW:
WINUSERAPI int WINAPI MessageBoxW (HWND hWnd, LPCWSTR lpText, LPCWSTR lpCaption, UINT uType) ;
Their second and third arguments are a string constant pointer
MessageBox will be called according to the parameter type:
#ifdef UNICODE #define MessageBox MessageBoxW #else #define MessageBox MessageBoxA #endif // !UNICODE
2.3.3 Windows string functions
The following is a set of string functions defined by Windows, these functions are used to calculate the length of the string, copy the string, the connection string and the comparison string:
ILength = lstrlen (pString); pString = lstrcpy (pString1, pString2); pString = lstrcpyn (pString1, pString2, iCount); pString = lstrcat (pString1, pString2); ICOMP = lstrcmp (pString1, pString2); ICOMP = lstrcmpi (pString1, pString2);
These functions are the same as the corresponding C function link library functions. If the UNICODE identifier is defined, then the function will accept wide strings, or only accept conventional strings. LstrlenW wide string version of the function may be performed in Windows 98.
2.3.4 Use printf in Windows
You can not use the printf function in Windows programs, but the use of other functions:
#include <Windows.h> #include <stdio.h> #include <stdlib.h> int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { char szBuffer[100]; sprintf(szBuffer, "The Sum of %i and %i is %i\n", 5, 3, 5 + 3); MessageBox(NULL, szBuffer, TEXT("Hk_Mayfly"), 0x4L | 0x40L); return 0; }
The following table lists all the sprintf function Microsoft C run-time libraries and Windows support.
ASCII |
Wide character |
conventional |
|
The number of variable parameters |
|||
Standard Edition |
sprintf |
swprintf |
_stprintf |
The maximum length version |
_snprintf |
_snwprintf |
_sntprintf |
Windows version |
wsprintfA |
wsprintfW |
wsprintf |
Pointer parameter array |
|||
Standard Edition |
vsprintf |
vswprintf |
_vstprintf |
The maximum length version |
_vsnprintf |
_vsnwprintf |
_vsntprintf |
Windows version |
wvsprintfA |
wvsprintfW |
wvsprintf |
2.3.5 formatted message box
#include <windows.h> #include <tchar.h> #include <stdio.h> int CDECL MessageBoxPrintf (TCHAR * szCaption, TCHAR * szFormat, ...) { TCHAR szBuffer [1024] ; va_list pArgList ; // The va_start macro (defined in STDARG.H) is usually equivalent to: // pArgList = (char *) &szFormat + sizeof (szFormat) ; va_start (pArgList, szFormat) ; // The last argument to wvsprintf points to the arguments _vsntprintf ( szBuffer, sizeof (szBuffer) / sizeof (TCHAR), szFormat, pArgList) ; // The va_end macro just zeroes out pArgList for no good reason va_end (pArgList) ; return MessageBox (NULL, szBuffer, szCaption, 0) ; } int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { int cxScreen, cyScreen ; cxScreen = GetSystemMetrics (SM_CXSCREEN) ; cyScreen = GetSystemMetrics (SM_CYSCREEN) ; MessageBoxPrintf ( TEXT ("ScrnSize"), TEXT ("The screen is %i pixels wide by %i pixels high."), cxScreen, cyScreen) ; return 0 ; }