Windows Programming _ chap02_unicode _ Study Notes

Source: Internet
Author: User
Windows Programming _ chap02_unicode _ Study Notes

--By: neicole (2013.05.24)

01. Opening

 

Chapter 1 of "windows program design" briefly describes the historical origins of Unicode (books are good, not boring at all, and look more like stories ), and the basic knowledge and application of wide characters in the C language in Windows programming. The book is simple and easy to learn. However, in conclusion, I feel that this chapter is not as easy as I think. How to organize them into knowledge modules is the biggest problem. By summing up, I formed my own knowledge module and had my own knowledge framework. I personally think This is meaningful learning, thinking, and learning.

02. Unicode origin and summary

 

Early stage: [1824, Braille (6-digit code)]-> [1854, telegraph code]-> [1903, telex code (5-digit code)]

Development: [bcdic, 6-bit encoding]-> [ebcdic, 8-bit encoding]-> [ASCII, 7-bit encoding]-> [DBCS, character set length is not uniform, 8-or 16-bit]-> [Unicode, 16-bit encoding, can represent 65536 characters]

In summary, Unicode is a character encoding used on computers. It can convert and process texts across languages and platforms.

Note: The width character is not necessarily Unicode. Unicode is only an implementation of the width character encoding.

03. wchar in C

"Uppercase letters (long integer) followed by left quotation marks" indicate to the compiler that the string will be stored with wide characters.

// Wchar_t test. CPP # include <iostream> # include <wchar. h> int main () {// In wchar. in H, typedef unsigned short wchar_t. Therefore, the wchar_t data type is the same as that of the unsigned integer type, and both are 16-bit width. // Define wchar_t c = 'a'; wchar_t C2 = L 'a'; wchar_t * STR = l "Haha "; // Add L to indicate wide character storage // String Length Measurement // The length cannot be measured using strlen (...), you can use wcslen (...), function prototype: size_t _ cdecl wcslen (const wchar_t *); size_t strlen = wcslen (STR); wchar_t arr [] = l "Haha"; size_t arrstrlen = sizeof) /sizeof (wchar_t)-1; // another character is the terminator return 0 ;}

 

04. wchar in Windows Programming

 

04.01 in the tchar. h header file, the role of the _ Unicode identifier.

04.01.01 function name for String Length Measurement

# Ifdef _ Unicode

# DEFINE _ tcslen wcslen

# Else

# DEFINE _ tcslen strlen

# Endif

04.01.02 whether the characters are compiled with wide characters

# Ifdef _ Unicode

# DEFINE _ T (x) L # x

# Else

# DEFINE _ T (x) x

# Endif

04.01.03 whether the string is compiled with wide characters

# Ifdef _ Unicode

# DEFINE _ T (x) _ T (X)

# DEFINE _ text (x) _ T (X)

# Else

...

# Endif

04.02 wide characters in winnt. h

Ctype. h In 04.02.01winnt.h defines char and wchar

Typedef char

Typedef wchar_t wchar

 

Pointer type in 04.02.02 winnt. h

Typedef 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;

04.02.03 Unicode without underscores in winnt. h

# Ifdef Unicode

Typedef wchar tchar, * ptchar;

Typedef lpwstr lptch, ptch, ptstr, lptstr;

Typedef maid;

# Else

Typedef char tchar, * ptchar;

Typedef lpstr lptch, ptch, ptstr, lptstr;

Typedef maid;

# Endif

Text in winnt. h 04.02.04

# Ifdef Unicode

# DEFINE _ text (quote) L # quote

# Else

# DEFINE _ text (quote) quote

# Endif

# Define text (quote) _ text (quote)

04.03 wide character function in winuser. h

 

04.03.01 MessageBox function prototype

Winuserapi int winapi messageboxa (hwnd, lpcstr lptext, lpcstr lpcaption, unit utype );

Winuserapi int winapi messageboxw (hwnd, lpcswtr lptext, lpcwstr lpcaption, unit utype );

The key lies in the difference between the second parameter and the third parameter, whether they accept the width character.

 

04.03.02 choice of MessageBox

# Ifdef Unicode

# Define MessageBox messageboxw

# Else

# Define MessageBox messageboxa

# Endif

05. Windows program design uses a wide character instance

 

05.01 results

05.01 source code

 

/*-----------------------------------------------------   SCRNSIZE.C -- Displays screen size in a message box                 (c) Charles Petzold, 1998  -----------------------------------------------------*/#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 ;}

05.02 Interpretation

It is not difficult to understand this program. You must understand what cdecl is and what va_arg, va_start, and va_end are. The main function calls the library function to obtain the display width and height, then calls the custom function to convert the width and height to tcharstr, and then creates a tchar string, call the MessageBox function to display the result.

05.02.01 cdecl (from msdn)

Learn about _ cdecl to understand that cdecl __cdecl is dedicated to Microsoft. This is the default call Convention for Calling C and C ++ programs. The caller can execute the vararg function to clear the stack.

1. the parameter is called from right to left in the list.

2. Stack maintenance responsibilities, call the function from the stack parameters.

3. Name modifier Conventions: name of the prefix of the underscore (_) character. However, the _ cdecl function of the C link is used for export.

4. case-insensitive conversion conventions. case-insensitive conversion is not performed.

Example:

// Example of the _ cdecl keyword on Function

Int _ cdecl System (const char *);

// Example of the _ cdecl keyword on function pointer

Typedefbool (_ cdecl * funcname_ptr) (void * arg1, const char * arg2, DWORD flags ,...);

05.02.02 va_arg, va_start, and va_end (from mdsn)

Instance:

 

// Testarg. CPP # include <stdio. h> # include <stdarg. h> # include <windows. h> void testarg (int I ,...) {va_list argptr; va_start (argptr, I); While (I --) {char * s = va_arg (argptr, char *); // read the printf ("% s \ n", S) parameter; // output parameter} va_end (argptr);} int main () {testarg (3, "ha ", "Haha", "HAHAHA"); // The result is six ha systems ("pause"); Return 0 ;}

Msdn description:

When the va_arg, va_end, and va_start macros provide parameters for accessing the function, the function uses a variable number of parameters that can be transplanted.

Va_start sets the optional parameters in the first parameter list of arg_ptr to be passed to the function. This parameter must be of the va_list type. This parameter prev_param is the first optional parameter. It is the name of the required parameter in the parameter list. If prev_param and register storage class, the macro behavior is not declared.

Before va_start, va_arg must be used for the first time.

Va_arg searches for the type position of a value from arg_ptr and incremental arg_ptr to the list of the next parameter, and uses the size type to determine the start position of the next parameter. Va_arg can be used to retrieve the time of any number of functions in the parameter list.

After all parameters are retrieved, the va_end pointer is reset to null.

 

Principle:

Typedefchar * va_list;

# Define_intsizeof (n )\

(Sizeof (n) + sizeof (INT)-1 )&~ (Sizeof (INT)-1 ))

# Defineva_start (AP, V) (AP = (va_list) & V + _ intsizeof (v ))

# Defineva_arg (AP, T )\

(* (T *) (AP + = _ intsizeof (t)-_ intsizeof (t )))

# Defineva_end (AP) (AP = (va_list) 0)

Definition _ intsizeof (n) is mainly used for some systems that require memory alignment. C functions are pushed from right to left into the stack. Figure (1) shows the distribution position of function parameters in the stack. we can see that va_list is defined as char *, and some platforms or operating systems are defined as void *. let's look at the definition of va_start, which is defined as & V + _ intsizeof (V), while & V is a fixed parameter in the stack address. So after we run va_start (AP, V, AP points to the address of the first variable parameter in the stack.

 

06. Terminator

 

In fact, the main result of this chapter is that Unicode can make programs more internationalized. If you want to use wide characters, the include header file tchar or windows can be used. After Unicode is defined, use the T Macro when using strings. In addition, you can use Windows cdecl If You Want To transmit parameters with an indefinite number, mainly because they are continuously stored and can be read in order.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.