:
17214
DÙNG CHO SV NGÀNH :
- 2010
i
2
CNTT
: 3
60
30
30
0
0
0
,
-
-
,
, ,
, , .
TS
LT
TH/Xemina
BT
KT
.
3
0
0
0
1.1.
Windows
1.1.1.
1.1.2.
Windows
1.1.3.
1.2.
32 64
1.2.1. Win32 API
1.2.2. Win64 API
1.3.
2005
1
1
1
.
4
4
0
2.1.
2.1.1.
2.1.2.
Windows
2.1.3.
2.2.
2.2.1.
2
1
2
1
ii
TS
LT
TH/Xemina
BT
KT
2.2.2. Copy file
2.2.3.
2.3.
2.3.1.
2.3.2.
1
1
.
4
6
0
1
3.1.
3.1.1. ,
3.1.2.
3.2.
3.2.1.
3.2.2.
3.2.3.
3.3.
3.3.1.
3.3.2.
3.3.3.
1
1
2
1
2
3
1
.
4
6
0
1
4.1.
4.2.
4.2.1.
4.2.2.
4.2.3.
Windows
4.2.4. :
4.3. (thread) trên Windows
4.3.1.
4.3.2.
/
4.3.3.
4.3.4.
4.4.
4.4.1.
4.4.2.
2
1
1
2
2
2
1
.
4
6
0
1
5.1.
5.2.
5.2.1. ()
5.2.2. ()
5.2.3.
5.3. :
1
1
2
2
iii
TS
LT
TH/Xemina
BT
KT
5.4.
5.4.1.
5.4.2. :
Windows
2
2
1
.
4
4
0
0
6.1.
6.2.
6.3.
6.4.
6.4.1.
6.4.2.
6.5. Windows Sockets 2.0
0,5
0,5
0,5
2
0,5
1
0,5
0,5
2
.
4
4
0
0
7.1.
7.2.
7.3.
7.3.1.
7.3.2.
1
1
2
0,5
1
2,5
:
-
.
.
.
- Charles Petzold. Programming Windows, fifth edition. Microsoft Press. 1998.
- Johnson M. Hart. Windows System Programming Third Edition. Addison Wesley
Professional. 2004.
sinh viên:
-
-
i
1
1: 2
2
2
2
3
3
2.1 Win32 API 3
2.2 Win64 API 4
4
4
3.2
5
3.3 14
3.4 17
20
2:
21
1.
21
2. 21
2.1 Serialization 21
2.2 24
3. 40
40
3:
L REGISTRY 41
1. 41
1.1 , 41
1.2 42
2. 43
2.1
43
2.2
43
2.3
44
3.
44
3.1
44
3.2
44
ii
3.3
44
44
4: 45
1. 45
2. 46
2.1. 46
2.2. 47
2.3. 47
2.4. :
47
3. (thread) trên Windows 49
3.1. 49
3.2.
/
49
3.3.
49
3.4. 50
4.
50
4.1. 50
4.2.
52
55
5: WINDOWS 56
1.
56
2. 56
2.1 () 56
2.2 () 56
2.3
56
3. : 57
4. 60
4.1 60
4.2 :
60
64
6:
65
1. 65
2. 65
3. 66
4. 66
iii
4.1 66
4.2 72
5. Windows Sockets 2.0 74
74
7:
75
75
75
76
7.4.
83
7.5. 84
85
O 86
87
1
H iu hành Windows ca Microsoft là h iu hành c cài t nhiu nht trên các
máy PC hin nay. S ph bin ca Windows và nn tng phn cng ca Intel dn ti s cn
thit phi có nhng hiu bit sâu v chúng, c bit i vi nhng lp trình viên. Mc ích
ca hc phn này là cung cp cho hc viên mt cái nhìn tng quan, t c bn ti chi tit v
các khía cnh ca lp trình trên h iu hành Windows, t các chi tit trong cu trúc ca mt
chng trình ti các khái nim cp cao v tin trình, lung, x lý song song, th vin DLL,
lp trình Socket, can thip vào c s d liu Registry
Lp trình C trên Windows,
, ,
.
các
thao tác vi h thng file, th mc, h thng CSDL Registry, qun lý tin trình và lung, lp
trình qun lý dch v, lp trình socket, th vin liên kt ng DLL
các em sinh viên,
, , các em sinh viên ,
.
.
6 10
2
API,
Borland
-
1.2 Các phiên
Professional Edition và Windows XP Home Edition là hai phiên
-Fi, DVD, dot NET
Windows 1.01
Windows 2.03
3
Windows 2.11
Windows 3.0
Windows 3.1x
Windows For Workgroups 3.1
Windows NT 3.1
Windows For Workgroups 3.11
Windows 3.2 (released in Simplified Chinese only)
Windows NT 3.5
Windows NT 3.51
Windows 95
Windows NT 4.0
Windows 98
Windows 98 SE
Windows 2000
Windows Me
Windows XP
Windows XP 64-bit Edition 2003
Windows Server 2003
Windows XP Professional x64 Edition
Windows Fundamentals for Legacy PCs
Windows Vista
Windows Home Server
Windows Server 2008
Windows 7
i Windows
-
2.
2.1 Win32 API
ra cùng
-
4
2.2 Win64 API
-
3.1
ng trình c
ng
ng trình
sau là test.
#include <windows.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
MessageBox(NULL, "Goodbye, cruel world!", "Note", MB_OK);
return 0;
}
ch b
ng trình không
ng n.
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
ng trình trên môi tr
hàm main trên môi trch
HINSTANCE hInstance
5
HINSTANCE hPrevInstance
Luôn là
LPSTR lpCmdLine
int nCmdShow
này sau.
WINAPI
3.2
:
1
indows
?.
.
,
.
6
:
#include <windows.h>
// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam,
LPARAM lParam)
{
switch(msg)
{
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
//Step 1: Registering the Window Class
wc.cbSize = sizeof(WNDCLASSEX);
7
wc.style = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground
/*wc.hbrBackground = GetStockObject(WHITE_BRUSH
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
// Step 2: Creating the Window
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
"The title of my window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
NULL, NULL, hInstance, NULL);
if(hwnd == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
8
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
// Step 3: The Message Loop
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
1
.
.
1:
(Registering)
(Window).
(Window Class)
,
(
), , . Theo
.
(window) (
)
.
C++.
const char g_szClassName[] = "myWindowClass";
,
.
9
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
WinMain()
.
().
:
cbSize:
style: Class Style (CS_*), (WS_*).
0.
lpfnWndProc:
.
cbClsExtra:
.
0.
hInstance: (instance) (
WinMain()).
hIcon:
(
32x32)
+TAB.
10
hCursor:
.
hbrBackground:
.
lpszMenuName:
.
lpszClassName:
hIconSm:
(16x16)
.
,
.
.
(
)
,
.
,
.
,
.
.
()
công hay không,
.
2:
,
.
() (
),
:
HWND hwnd;
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
"The title of my window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
NULL, NULL, hInstance, NULL);
(WS_EX_CLIENTEDGE) (extended), trong
. 0
.
.
(g_szClassName),
.
. Sau
(
)
.
11
WS_OVERLAPPEDWINDOW
Window Style.
. .
(CW_USEDEFAULT, CW_USEDEFAULT, 320, 240)
.
_
. 0
.
0 .
,
.
(NULL, NULL, g_hInst, NULL)
,
,
.
.
,
.
,
.
.
(). D (
)
.
0 (zero).
((void*)0)
.
,
. 0
.
1
. CreateWindow()
,
.
,
,
.
if(hwnd == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
, (
)
12
()
.
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
, _SHOWNORMAL
.
()
.
3:
,
.
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
()
.
, ,
,
,
.
GetMessage()
.
() .
,
(
).
TranslateMessage()
_
_KEYDOWN.
()
(
).
,
,
.
,
.
4: .
.
.
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam,
LPARAM lParam)
13
{
switch(msg)
{
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
,
, .
(WndProc()).
.
_
.
, .
WM_
+F4.
,
,
n
,
,
.
()
WM_
,
,
.
, ()
.
_
.
GetMessage() ,
,
,
_
().
.
5: khôn
5
, 5
.
14
3.3
: window_click.
,
()
,
,
,
, vân vân. .
.
_
công,
(code) .
.
,
(),
:
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam,
LPARAM lParam)
{
switch(msg)
{
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
:
WM_LBUTTONDOWN (
_RBUTTONDOWN, WM_
):
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam,
LPARAM lParam)
{
switch(msg)
{
case WM_LBUTTONDOWN: // <-
15
// <- we just added this stuff
break; // <-
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
(_
_QUIT)
.
(
)
ta.
.
:
:
.
.
GetModuleFileName(hInstance, szFileName, MAX_PATH);
MessageBox(hwnd, szFileName, "This program is:", MB_OK |
MB_ICONINFORMATION);
() :
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam,
LPARAM lParam)
{
switch(msg)
{
case WM_LBUTTONDOWN:
// BEGIN NEW CODE
{
char szFileName[MAX_PATH];
HINSTANCE hInstance = GetModuleHandle(NULL);
16
GetModuleFileName(hInstance, szFileName, MAX_PATH);
MessageBox(hwnd, szFileName, "This program is:", MB_OK |
MB_ICONINFORMATION);
}
// END NEW CODE
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
{ }
.
,
.
GetModuleFileName(),
,
?
(),
,
. :
HINSTANCE hInstance = GetModuleHandle(NULL);
()
(
)
,
LPTRSTR (
) *
:
char szFileName[MAX_PATH];
MAX_
.
.
17
3.4
.
,
.
?
(message) ().
(
) :
#define WM_INITDIALOG 0x0110
#define WM_COMMAND 0x0111
#define WM_LBUTTONDOWN 0x0201
vân vân.
.
( )
,
.
.
,
,
,
(
)
.
,
, .
,
. Nguyên
16
32 bit,
32 32 bit.
,
.
_
trên, .
_
,
, HIWORD(wParam) (
)
LOWORD(wParam) .
(window handle)
.
HIWORD() 2 byte cao
(High Word) 4 byte = 32 bit (0xFFFF0000) (0x0000FFFF)
.
32
16
(Double
WORD) 32 bit.
()
().
PostMessage()
(put)
.
()
. m SendMessage()
.
WM_
PostMessage(hwnd, WM_CLOSE, 0, 0);
.
18
0
.
_CLOSE.
(Dialog).
,
.
() ()
() (
trên).
. SendDlgItemMessage()
()
.
?
_
.
?
qua?
,
,
.
,
.
( Message Loop)
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
1. (),
thông
.
().
2.
(
) ()
,
. 0
_
.
3. ( )
TranslateMessage(), , d
.
.
4.
(). H