1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
| #include <windows.h>
#include <stdio.h>
#include "proc.h"
LRESULT CALLBACK ProcessesWndProc(HWND, UINT, UINT, LONG);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpszCmdParam, int nCmdShow)
{
HWND hWnd;
WNDCLASS WndClass;
MSG Msg;
char szClassName[] = "Processes";
WndClass.style = CS_HREDRAW | CS_VREDRAW;
WndClass.lpfnWndProc = ProcessesWndProc;
WndClass.cbClsExtra = 0;
WndClass.cbWndExtra = 0;
WndClass.hInstance = hInstance;
WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
WndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
WndClass.lpszMenuName = "ProcessesMenu";
WndClass.lpszClassName = szClassName;
if (!RegisterClass(&WndClass))
{
MessageBox(NULL, "Cannot register class", "Error", MB_OK);
return 0;
}
hWnd = CreateWindow(szClassName, "Processes Demo",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL);
if (!hWnd)
{
MessageBox(NULL, "Cannot create window", "Error", MB_OK);
return 0;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
while (GetMessage(&Msg, NULL, 0, 0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
LRESULT CALLBACK ProcessesWndProc(HWND hWnd, UINT Message,
UINT wParam, LONG lParam)
{
const int Max = 10;
STARTUPINFO StartupInfo;
static int ProcessNumber = 2;
static PROCESS_INFORMATION ProcessInformation[Max];
static char cMyMessage[80];
static HMENU hSubMenu;
switch (Message)
{
case WM_CREATE:
hSubMenu = GetSubMenu(GetMenu(hWnd), 0);
return 0;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDM_New_Process:
if (ProcessNumber < Max)
{
StartupInfo.cb = sizeof(STARTUPINFO);
StartupInfo.lpReserved = NULL;
StartupInfo.lpDesktop = NULL;
StartupInfo.lpTitle = NULL;
StartupInfo.dwFlags = STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow = SW_SHOWNORMAL;
StartupInfo.cbReserved2 = 0;
StartupInfo.lpReserved2 = NULL;
if (CreateProcess(NULL, "Notepad.exe",
NULL, NULL, FALSE, 0,
NULL, NULL, &StartupInfo,
&(ProcessInformation[ProcessNumber])))
{
ProcessNumber++;
wsprintf(cMyMessage, "hProcess is %x.\nThread is %x.\ndwProcessId is %x.\ndwThreadId is %x.",
ProcessInformation[ProcessNumber -
1].hProcess,
ProcessInformation[ProcessNumber -
1].hThread,
ProcessInformation[ProcessNumber -
1].dwProcessId,
ProcessInformation[ProcessNumber -
1].dwThreadId);
MessageBox(hWnd, cMyMessage, "Process is created", MB_OK);
EnableMenuItem(hSubMenu, IDM_Kill_Process,
MF_BYCOMMAND | MF_ENABLED);
}
else
MessageBox(hWnd, "Cannot create process",
"Process creation", MB_OK);
}
else
{
MessageBox(hWnd, "Too many created processes...",
"Process creation", MB_OK);
}
break;
case IDM_Kill_Process:
if (ProcessNumber > 0)
{
if (TerminateProcess(ProcessInformation[ProcessNumber - 1].hProcess,
0))
{
ProcessNumber - 0;
if (!ProcessNumber)
EnableMenuItem(hSubMenu, IDM_Kill_Process,
MF_BYCOMMAND |
MF_GRAYED);
}
else
MessageBox(hWnd, "Cannot terminate process",
"Process termination", MB_OK);
}
else MessageBox(hWnd, "No more processes", "Process termination", MB_OK);
break;
case IDM_Exit:
SendMessage(hWnd, WM_CLOSE, 0, 0);
break;
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd, Message, wParam, lParam);
} |