Monday, December 31, 2012

Windows Programming

Windows Programming
chapter1 Getting Started
In the early days, the great bulk of Windows was implemented in just three dynamic-link libraries. These represented the three main subsystems of Windows, which were referred to as Kernel, User, and GDI.
Kernel handles all the stuff that an operating system kernel traditionally handles—memory management, file I/O, and tasking. User refers to the user interface, and implements all the windowing logic. GDI is the Graphics Device Interface, which allows a program to display text and graphics on the screen and printer.
Windows is a complex system; putting a programming layer on top of the API doesn't eliminate the complexity—it merely hides it. Sooner or later that complexity is going to jump out and bite you in the leg. Knowing the API gives you a better chance at recovery. No approach can be more powerful or versatile than using this API directly.
chapter2 An Introduction to Unicode
typedef unsigned short wchar_t ;
wchar_t * p = L"Hello!" ;
Notice the capital L (for long) immediately preceding the first quotation mark. This indicates to the compiler that the string is to be stored with wide characters—that is, with every character occupying 2 bytes.
char * p = "Hello!" ;
The character string is stored in static memory and uses 7 bytes of storage—the 6 bytes of the string in addition to a terminating 0.
we can call
iLength = strlen (pc) ;
The variable iLength will be set equal to 6, the number of characters in the string.
wchar_t * pw = L"Hello!" ;
And now we call strlen again:
iLength = strlen (pw) ;
This little exercise clearly illustrates the differences between the C language itself and the run-time library functions. The compiler interprets the string L"Hello!" as a collection of 16-bit short integers and stores them in the wchar_t array. The compiler also handles any array indexing and the sizeof operator, so these work properly. But run-time library functions such as strlen are added during link time. These functions expect strings that comprise single-byte characters. When they are confronted with wide-character strings, they don't perform as we'd like.
The wide-character version of the strlen function is called wcslen ("wide-character string length")
iLength = wcslen (pw) ;
The function returns 6, the number of characters in the string. Keep in mind that the character length of a string does not change when you move to wide characters—only the byte length changes.
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
And so on. TCHAR.H also solves the problem of the two character data types with a new data type named TCHAR. If the _UNICODE identifier is defined, TCHAR is wchar_t:
typedef wchar_t TCHAR ;
Otherwise, TCHAR is simply a char:
typedef char TCHAR ;
宏定义只是简单的字符串代换(原地扩展),而typedef则不是原地扩展,它的新名字具有一定的封装性,以致于新命名的标识符具有更易定义变
量的功能。请看上面第一大点代码的第三行:
typedef    (int*)      pINT;
以及下面这行:
#define    pINT2    int*
效果相同?实则不同!实践中见差别:pINT a,b;的效果同int *a; int *b;表示定义了两个整型指针变量。而pINT2 a,b;的效果同int *a, b;
表示定义了一个整型指针变量a和整型变量b。
注意:两者还有一个行尾;号的区别哦!
The WINNT.H header file goes on to define six data types you can use as pointers to 8-bit character strings and four data types you can use as pointers to const 8-bit character strings. I've condensed the actual header file statements a bit to show the data types here:
typedef CHAR * PCHAR, * LPCH, * PCH, * NPSTR, * LPSTR, * PSTR ;
typedef CONST CHAR * LPCCH, * PCCH, * LPCSTR, * PCSTR ;
The N and L prefixes stand for "near" and "long" and refer to the two different sizes of pointers in 16-bit Windows. There is no differentiation between near and long pointers in Win32.

chapter3 Windows and Messages

No comments:

Post a Comment