【读书笔记】"Programming Windows" (chapter 1)

 

Programming Windows
chapter 1 – Getting started
A History of Windows
Operating system
Distribution period
Features
MS-DOS
before 1981
· intel 8086 or 8088 running in "real mode"
·access 1 megabyte (MB) of memory
·single task
·minimal operating system
·command-line interface to commands
·applications accessed the hardware of the PC directly
Macintosh
1984
·first desktop Graphic environments
Microsoft Windows 1.0
1985
· intel 8086 or 8088 running in "real mode"
·access 1 megabyte (MB) of memory
·single task
·Graphic environments
·provide drivers for video displays and printers
Windows 2.0
1987
· intel 8086 or 8088 running in "real mode"
·access 1 megabyte (MB) of memory
·single task
·enhancements to the keyboard and mouse interface, particularly for menus and dialog boxes.
IBM. OS/2
1987
·graphical Presentation Manager
·protected-mode
Windows/386
shortly after 1987
·virtual 86" mode of the Intel 386 microprocessor to window and multitask many DOS programs that directly accessed hardware
Windows 3.0
1990
·support of the 16-bit protected-mode operation of Intel's 286, 386, and 486 microprocessors
·access to up to 16 megabytes of memory
Windows 3.1
1992
·TrueType font technology
·multimedia (sound and music)
·Object Linking and Embedding (OLE)
·standardized common dialog boxes.
· ran only in protected mode and required a 286 or 386 processor with at least 1 MB of memory
Windows NT
1993
·support the 32-bit mode of the Intel 386, 486, and Pentium microprocessors.
· access to a 32-bit flat address space and use a 32-bit instruction set.
·designed to be portable to non-Intel processors, and it runs on several RISC-based workstations.
Windows 95
1995
·supported the 32-bit programming mode of the Intel 386 and later microprocessors.
· requiring fewer hardware resources.
Windows 98
1998
·performance improvements, better hardware support
·Internet Explorer prepared
Windows2000
2000
·support Multiprocessor
·NT kernel
·high security
Windows Xp
 
·support Multiprocessor
·NT kernel
·high security
Windows Vista
 
·support Multiprocessor
·NT kernel
·high security
 
Aspects of Windows
Windows 98 / WinNT
Common features
·preemptive multitasking and multithreading graphical operating systems
·graphical user interface (GUI)
·WYSIWYG (what you see is what you get) Supported
·full-fledged 32-bit operating systems with flat memory space
·dynamic-link libraries RunningTime Support
 
Dynamic Linking
Provide for windows common function call, and with .DLL or .EXE as its extension.
Windows Programming Options
Programming with C and use native Windows APIs to learn how to program windows application.
APIs and Memory Models
API (Application Program Interface)
Memory Mode
Memory Mode
Features
16-bits segmented memory mode
·Versions 1.0 through 3.1 of Windows
·16-bit Intel 8086, 8088, and 286 microprocessors
·also supported by 386CPU
·microprocessor register size 16 bits
·C int data type was also 16 bits wide
·access memory with to components(16-bit segment pointer and a 16-bit offset pointer)Using l ong, or far, pointers (which involved both a segment address and an offset address) and short, or near, pointers (which involved an offset address with an assumed segment address).
32-bits flat memory model
·Windows NT and Windows 95
·Intel 386, 486, and Pentium processors with 32bits mode
·microprocessor register size 32 bits
·32-bit versions of Windows use simple 32-bit pointer values that address a flat linear address space
 
 
 
Language Options
Programming Windows approach
C and the native APIs
best performance, the most power, and the greatest versatility
Visual Basic or Borland Delphi(RAD)
Rapid and simple
Microsoft Visual C++ with  (MFC)
MFC encapsulates many of the messier aspects of Windows programming in a collection of C++ classes
JAVA
Internet and portable
 
 
 
Your First Windows Program
A Character-Mode Model (original c program)
#include <stdio.h>
int main (void)
{
     printf ("hello, world/n") ;
     return 0 ;
}
The Windows Equivalent
#include <windows.h>
int WINAPI WinMain (HINSTANCE hInstance, 
HINSTANCE hPrevInstance,
                   PSTR szCmdLine, 
int iCmdShow)
{
     MessageBox (NULL, TEXT ("Hello, Windows 98!"), TEXT ("HelloMsg"), 0) ;
     return 0 ;
}
 
 
The Header Files
WINDOWS.H
a master include file that includes other Windows header files, some of which also include other header files. The most important and most basic of these header files are:
WINDEF.H
Basic type definitions.
WINNT.H
Type definitions for Unicode support
WINBASE.H
Kernel functions.
WINUSER.H
User interface functions.
WINGDI.H
Graphics device interface functions.
 
Program Entry Point
the entry of c program is main() , and the entry of windows program is WinMain(), it is declare as:
int WINAPI WinMain (HINSTANCE hInstance,
                        HINSTANCE hPrevInstance,
                        PSTR szCmdLine,
                        int iCmdShow) 
this function is declare in file “WINBASE.H”
 
l          WINAPI
The WINAPI identifier is defined in “WINDEF.H”
#define WINAPI __stdcall
 
 
What is __stdcall?
#define WINAPI __stdcall
This statement specifies a calling convention that involves how machine code is generated to place function call arguments on the stack. Most Windows function calls are declared as WINAPI which is defined as __stdcall by #define WINAPI __ stdcall
 
__stdcall ; __cdecl; __fastcal; thiscall; naked call
1 、__stdcall调用约定相当于16位动态库中经常使用的PASCAL调用约定。在32位的VC++5.0中PASCAL调用约定不再被支持(实际上它已被定义为__ stdcall。除了__ pascal外,__ fortran和__ syscall也不被支持),取而代之的是__ stdcall调用约定。两者实质上是一致的,即函数的参数自右向左通过 栈传递,被调用的函数在返回前清理传送参数的内存栈,但不同的是函数名的修饰部分(关于函数名的修饰部分在后面将详细说明)。_ stdcall是Pascal程序的缺省调用方式,通常用于Win32  Api中,函数采用从右到左的压 栈方式,自己在退出时清空堆栈。VC将函数编译后会在函数名前面加上下划线前缀,在函数名后加上"@"和参数的字节数。
2、__ cdecl  C调用约定(即用__ cdecl关键字说明)按从右至左的顺序压参数入 栈,由调用者把参数弹出栈。对于传送参数的内存栈是由调用者来维护的(正因为如此,实现可变参数的函数只能使用该调用约定)。另外,在函数名修饰约定方面也有所不同。_ cdecl是C和C++程序的缺省调用方式。每一个调用它的函数都包含清空堆栈的代码,所以产生的可执行文件大小会比调用_ stdcall函数的大。函数采用从右到左的压 栈方式。VC将函数编译后会在函数名前面加上下划线前缀。是MFC缺省调用约定。
3、 __fastcall调用约定是"人"如其名,它的主要特点就是快,因为它是通过寄存器来传送参数的(实际上,它用ECX和EDX传送前两个双字(DWORD)或更小的参数,剩下的参数仍旧自右向左压 栈传送,被调用的函数在返回前清理传送参数的内存栈),在函数名修饰约定方面,它和前两者均不同。_ fastcall方式的函数采用寄存器传递参数,VC将函数编译后会在函数名前面加上"@"前缀,在函数名后加上"@"和参数的字节数。
 
4、 thiscall仅仅应用于"C++"成员函数。this指针存放于CX寄存器,参数从右到左压。thiscall不是关键词,因此不能被程序员指定。
 
5、 naked call采用1-4的调用约定时,如果必要的话,进入函数时编译器会产生代码来保存ESI,EDI,EBX,EBP寄存器,退出函数时则产生代码恢复这些寄存器的内容。naked call不产生这样的代码。naked call不是类型修饰符,故必须和_ declspec共同使用。
 
关键字 __ stdcall、__ cdecl和__ fastcall可以直接加在要输出的函数前,也可以在编译环境的Setting.../C/C++ /Code Generation项选择。当加在输出函数前的关键字与编译环境中的选择不同时,直接加在输出函数前的关键字有效。它们对应的命令行参数分别为/ Gz、/ Gd和/ Gr。缺省状态为/ Gd,即__ cdecl。要完全模仿PASCAL调用约定首先必须使用__ stdcall调用约定,至于函数名修饰约定,可以通过其它方法模仿。还有一个值得一提的是WINAPI宏, Windows.h支持该宏,它可以将出函数翻译成适当的调用约定,在WIN32中,它被定义为__ stdcall。使用WINAPI宏可以创建自己的APIs。
 
 
WinMain ( )parameters
Meanings
HINSTANCE hInstance
the handle uniquely identifies the program of its instance
HINSTANCE hPrevInstance
In early versions of Windows, when you ran the same program concurrently more than once, you created multiple instances of that program. All instances of the same application shared code and read-only memory (usually resources such as menu and dialog box templates). A program could determine if other instances of itself were running by checking the hPrevInstance parameter. It could then skip certain chores and move some data from the previous instance into its own data area.
In the 32-bit versions of Windows, this concept has been abandoned. The second parameter to WinMain is always NULL (defined as 0).
LPSTR szCmdLine
the command line used to run the program
int iCmdShow
indicates how the program should be initially displayed
 
MessageBox
The MessageBox function creates, displays, and operates a message box. The message box contains an application-defined message and title, plus any combination of predefined icons and push buttons.
int MessageBox(
  HWND hWnd,          // handle of owner window
  LPCTSTR lpText,     // address of text in message box
  LPCTSTR lpCaption// address of title of message box
  UINT uType          // style of message box
);
Parameters
hWnd
Identifies the owner window of the message box to be created. If this parameter is NULL, the message box has no owner window.
lpText
Pointer to a null-terminated string containing the message to be displayed.
lpCaption
Pointer to a null-terminated string used for the dialog box title. If this parameter is NULL, the default title Error is used.
uType
Specifies a set of bit flags that determine the contents and behavior of the dialog box. This parameter can be a combination of flags from the following groups of flags.
 
 
 
 

你可能感兴趣的:(【读书笔记】"Programming Windows" (chapter 1))