added patch submitted by Guido de Jong
[reactos.git] / rosapps / taskmgr / affinity.cpp
1 /*
2 * ReactOS Task Manager
3 *
4 * affinity.cpp
5 *
6 * Copyright (C) 1999 - 2001 Brian Palmer <brianp@reactos.org>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #ifdef _MSC_VER
24 #include "stdafx.h"
25 #else
26 #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
27 #include <windows.h>
28 #include <commctrl.h>
29 #include <stdlib.h>
30 #include <malloc.h>
31 #include <memory.h>
32 #include <tchar.h>
33 #include <process.h>
34 #include <stdio.h>
35 #endif
36
37 #include "taskmgr.h"
38 #include "ProcessPage.h"
39 #include "affinity.h"
40 #include "perfdata.h"
41
42 HANDLE hProcessAffinityHandle;
43
44 LRESULT CALLBACK AffinityDialogWndProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
45
46 void ProcessPage_OnSetAffinity(void)
47 {
48 LV_ITEM lvitem;
49 ULONG Index;
50 DWORD dwProcessId;
51 TCHAR strErrorText[260];
52
53 for (Index=0; Index<(ULONG)ListView_GetItemCount(hProcessPageListCtrl); Index++)
54 {
55 memset(&lvitem, 0, sizeof(LV_ITEM));
56
57 lvitem.mask = LVIF_STATE;
58 lvitem.stateMask = LVIS_SELECTED;
59 lvitem.iItem = Index;
60
61 ListView_GetItem(hProcessPageListCtrl, &lvitem);
62
63 if (lvitem.state & LVIS_SELECTED)
64 break;
65 }
66
67 dwProcessId = PerfDataGetProcessId(Index);
68
69 if ((ListView_GetSelectedCount(hProcessPageListCtrl) != 1) || (dwProcessId == 0))
70 return;
71
72 hProcessAffinityHandle = OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_SET_INFORMATION, FALSE, dwProcessId);
73
74 if (!hProcessAffinityHandle)
75 {
76 GetLastErrorText(strErrorText, 260);
77 MessageBox(hMainWnd, strErrorText, "Unable to Access or Set Process Affinity", MB_OK|MB_ICONSTOP);
78 return;
79 }
80
81 DialogBox(hInst, MAKEINTRESOURCE(IDD_AFFINITY_DIALOG), hMainWnd, (DLGPROC)AffinityDialogWndProc);
82
83 if (hProcessAffinityHandle)
84 {
85 CloseHandle(hProcessAffinityHandle);
86 hProcessAffinityHandle = NULL;
87 }
88 }
89
90 LRESULT CALLBACK AffinityDialogWndProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
91 {
92 DWORD dwProcessAffinityMask = 0;
93 DWORD dwSystemAffinityMask = 0;
94 TCHAR strErrorText[260];
95
96 switch (message)
97 {
98 case WM_INITDIALOG:
99
100 //
101 // Get the current affinity mask for the process and
102 // the number of CPUs present in the system
103 //
104 if (!GetProcessAffinityMask(hProcessAffinityHandle, &dwProcessAffinityMask, &dwSystemAffinityMask))
105 {
106 GetLastErrorText(strErrorText, 260);
107 EndDialog(hDlg, 0);
108 MessageBox(hMainWnd, strErrorText, "Unable to Access or Set Process Affinity", MB_OK|MB_ICONSTOP);
109 }
110
111 //
112 // Enable a checkbox for each processor present in the system
113 //
114 if (dwSystemAffinityMask & 0x00000001)
115 EnableWindow(GetDlgItem(hDlg, IDC_CPU0), TRUE);
116 if (dwSystemAffinityMask & 0x00000002)
117 EnableWindow(GetDlgItem(hDlg, IDC_CPU1), TRUE);
118 if (dwSystemAffinityMask & 0x00000004)
119 EnableWindow(GetDlgItem(hDlg, IDC_CPU2), TRUE);
120 if (dwSystemAffinityMask & 0x00000008)
121 EnableWindow(GetDlgItem(hDlg, IDC_CPU3), TRUE);
122 if (dwSystemAffinityMask & 0x00000010)
123 EnableWindow(GetDlgItem(hDlg, IDC_CPU4), TRUE);
124 if (dwSystemAffinityMask & 0x00000020)
125 EnableWindow(GetDlgItem(hDlg, IDC_CPU5), TRUE);
126 if (dwSystemAffinityMask & 0x00000040)
127 EnableWindow(GetDlgItem(hDlg, IDC_CPU6), TRUE);
128 if (dwSystemAffinityMask & 0x00000080)
129 EnableWindow(GetDlgItem(hDlg, IDC_CPU7), TRUE);
130 if (dwSystemAffinityMask & 0x00000100)
131 EnableWindow(GetDlgItem(hDlg, IDC_CPU8), TRUE);
132 if (dwSystemAffinityMask & 0x00000200)
133 EnableWindow(GetDlgItem(hDlg, IDC_CPU9), TRUE);
134 if (dwSystemAffinityMask & 0x00000400)
135 EnableWindow(GetDlgItem(hDlg, IDC_CPU10), TRUE);
136 if (dwSystemAffinityMask & 0x00000800)
137 EnableWindow(GetDlgItem(hDlg, IDC_CPU11), TRUE);
138 if (dwSystemAffinityMask & 0x00001000)
139 EnableWindow(GetDlgItem(hDlg, IDC_CPU12), TRUE);
140 if (dwSystemAffinityMask & 0x00002000)
141 EnableWindow(GetDlgItem(hDlg, IDC_CPU13), TRUE);
142 if (dwSystemAffinityMask & 0x00004000)
143 EnableWindow(GetDlgItem(hDlg, IDC_CPU14), TRUE);
144 if (dwSystemAffinityMask & 0x00008000)
145 EnableWindow(GetDlgItem(hDlg, IDC_CPU15), TRUE);
146 if (dwSystemAffinityMask & 0x00010000)
147 EnableWindow(GetDlgItem(hDlg, IDC_CPU16), TRUE);
148 if (dwSystemAffinityMask & 0x00020000)
149 EnableWindow(GetDlgItem(hDlg, IDC_CPU17), TRUE);
150 if (dwSystemAffinityMask & 0x00040000)
151 EnableWindow(GetDlgItem(hDlg, IDC_CPU18), TRUE);
152 if (dwSystemAffinityMask & 0x00080000)
153 EnableWindow(GetDlgItem(hDlg, IDC_CPU19), TRUE);
154 if (dwSystemAffinityMask & 0x00100000)
155 EnableWindow(GetDlgItem(hDlg, IDC_CPU20), TRUE);
156 if (dwSystemAffinityMask & 0x00200000)
157 EnableWindow(GetDlgItem(hDlg, IDC_CPU21), TRUE);
158 if (dwSystemAffinityMask & 0x00400000)
159 EnableWindow(GetDlgItem(hDlg, IDC_CPU22), TRUE);
160 if (dwSystemAffinityMask & 0x00800000)
161 EnableWindow(GetDlgItem(hDlg, IDC_CPU23), TRUE);
162 if (dwSystemAffinityMask & 0x01000000)
163 EnableWindow(GetDlgItem(hDlg, IDC_CPU24), TRUE);
164 if (dwSystemAffinityMask & 0x02000000)
165 EnableWindow(GetDlgItem(hDlg, IDC_CPU25), TRUE);
166 if (dwSystemAffinityMask & 0x04000000)
167 EnableWindow(GetDlgItem(hDlg, IDC_CPU26), TRUE);
168 if (dwSystemAffinityMask & 0x08000000)
169 EnableWindow(GetDlgItem(hDlg, IDC_CPU27), TRUE);
170 if (dwSystemAffinityMask & 0x10000000)
171 EnableWindow(GetDlgItem(hDlg, IDC_CPU28), TRUE);
172 if (dwSystemAffinityMask & 0x20000000)
173 EnableWindow(GetDlgItem(hDlg, IDC_CPU29), TRUE);
174 if (dwSystemAffinityMask & 0x40000000)
175 EnableWindow(GetDlgItem(hDlg, IDC_CPU30), TRUE);
176 if (dwSystemAffinityMask & 0x80000000)
177 EnableWindow(GetDlgItem(hDlg, IDC_CPU31), TRUE);
178
179
180 //
181 // Check each checkbox that the current process
182 // has affinity with
183 //
184 if (dwProcessAffinityMask & 0x00000001)
185 SendMessage(GetDlgItem(hDlg, IDC_CPU0), BM_SETCHECK, BST_CHECKED, 0);
186 if (dwProcessAffinityMask & 0x00000002)
187 SendMessage(GetDlgItem(hDlg, IDC_CPU1), BM_SETCHECK, BST_CHECKED, 0);
188 if (dwProcessAffinityMask & 0x00000004)
189 SendMessage(GetDlgItem(hDlg, IDC_CPU2), BM_SETCHECK, BST_CHECKED, 0);
190 if (dwProcessAffinityMask & 0x00000008)
191 SendMessage(GetDlgItem(hDlg, IDC_CPU3), BM_SETCHECK, BST_CHECKED, 0);
192 if (dwProcessAffinityMask & 0x00000010)
193 SendMessage(GetDlgItem(hDlg, IDC_CPU4), BM_SETCHECK, BST_CHECKED, 0);
194 if (dwProcessAffinityMask & 0x00000020)
195 SendMessage(GetDlgItem(hDlg, IDC_CPU5), BM_SETCHECK, BST_CHECKED, 0);
196 if (dwProcessAffinityMask & 0x00000040)
197 SendMessage(GetDlgItem(hDlg, IDC_CPU6), BM_SETCHECK, BST_CHECKED, 0);
198 if (dwProcessAffinityMask & 0x00000080)
199 SendMessage(GetDlgItem(hDlg, IDC_CPU7), BM_SETCHECK, BST_CHECKED, 0);
200 if (dwProcessAffinityMask & 0x00000100)
201 SendMessage(GetDlgItem(hDlg, IDC_CPU8), BM_SETCHECK, BST_CHECKED, 0);
202 if (dwProcessAffinityMask & 0x00000200)
203 SendMessage(GetDlgItem(hDlg, IDC_CPU9), BM_SETCHECK, BST_CHECKED, 0);
204 if (dwProcessAffinityMask & 0x00000400)
205 SendMessage(GetDlgItem(hDlg, IDC_CPU10), BM_SETCHECK, BST_CHECKED, 0);
206 if (dwProcessAffinityMask & 0x00000800)
207 SendMessage(GetDlgItem(hDlg, IDC_CPU11), BM_SETCHECK, BST_CHECKED, 0);
208 if (dwProcessAffinityMask & 0x00001000)
209 SendMessage(GetDlgItem(hDlg, IDC_CPU12), BM_SETCHECK, BST_CHECKED, 0);
210 if (dwProcessAffinityMask & 0x00002000)
211 SendMessage(GetDlgItem(hDlg, IDC_CPU13), BM_SETCHECK, BST_CHECKED, 0);
212 if (dwProcessAffinityMask & 0x00004000)
213 SendMessage(GetDlgItem(hDlg, IDC_CPU14), BM_SETCHECK, BST_CHECKED, 0);
214 if (dwProcessAffinityMask & 0x00008000)
215 SendMessage(GetDlgItem(hDlg, IDC_CPU15), BM_SETCHECK, BST_CHECKED, 0);
216 if (dwProcessAffinityMask & 0x00010000)
217 SendMessage(GetDlgItem(hDlg, IDC_CPU16), BM_SETCHECK, BST_CHECKED, 0);
218 if (dwProcessAffinityMask & 0x00020000)
219 SendMessage(GetDlgItem(hDlg, IDC_CPU17), BM_SETCHECK, BST_CHECKED, 0);
220 if (dwProcessAffinityMask & 0x00040000)
221 SendMessage(GetDlgItem(hDlg, IDC_CPU18), BM_SETCHECK, BST_CHECKED, 0);
222 if (dwProcessAffinityMask & 0x00080000)
223 SendMessage(GetDlgItem(hDlg, IDC_CPU19), BM_SETCHECK, BST_CHECKED, 0);
224 if (dwProcessAffinityMask & 0x00100000)
225 SendMessage(GetDlgItem(hDlg, IDC_CPU20), BM_SETCHECK, BST_CHECKED, 0);
226 if (dwProcessAffinityMask & 0x00200000)
227 SendMessage(GetDlgItem(hDlg, IDC_CPU21), BM_SETCHECK, BST_CHECKED, 0);
228 if (dwProcessAffinityMask & 0x00400000)
229 SendMessage(GetDlgItem(hDlg, IDC_CPU22), BM_SETCHECK, BST_CHECKED, 0);
230 if (dwProcessAffinityMask & 0x00800000)
231 SendMessage(GetDlgItem(hDlg, IDC_CPU23), BM_SETCHECK, BST_CHECKED, 0);
232 if (dwProcessAffinityMask & 0x01000000)
233 SendMessage(GetDlgItem(hDlg, IDC_CPU24), BM_SETCHECK, BST_CHECKED, 0);
234 if (dwProcessAffinityMask & 0x02000000)
235 SendMessage(GetDlgItem(hDlg, IDC_CPU25), BM_SETCHECK, BST_CHECKED, 0);
236 if (dwProcessAffinityMask & 0x04000000)
237 SendMessage(GetDlgItem(hDlg, IDC_CPU26), BM_SETCHECK, BST_CHECKED, 0);
238 if (dwProcessAffinityMask & 0x08000000)
239 SendMessage(GetDlgItem(hDlg, IDC_CPU27), BM_SETCHECK, BST_CHECKED, 0);
240 if (dwProcessAffinityMask & 0x10000000)
241 SendMessage(GetDlgItem(hDlg, IDC_CPU28), BM_SETCHECK, BST_CHECKED, 0);
242 if (dwProcessAffinityMask & 0x20000000)
243 SendMessage(GetDlgItem(hDlg, IDC_CPU29), BM_SETCHECK, BST_CHECKED, 0);
244 if (dwProcessAffinityMask & 0x40000000)
245 SendMessage(GetDlgItem(hDlg, IDC_CPU30), BM_SETCHECK, BST_CHECKED, 0);
246 if (dwProcessAffinityMask & 0x80000000)
247 SendMessage(GetDlgItem(hDlg, IDC_CPU31), BM_SETCHECK, BST_CHECKED, 0);
248
249 return TRUE;
250
251 case WM_COMMAND:
252
253 //
254 // If the user has cancelled the dialog box
255 // then just close it
256 //
257 if (LOWORD(wParam) == IDCANCEL)
258 {
259 EndDialog(hDlg, LOWORD(wParam));
260 return TRUE;
261 }
262
263 //
264 // The user has clicked OK -- so now we have
265 // to adjust the process affinity mask
266 //
267 if (LOWORD(wParam) == IDOK)
268 {
269 //
270 // First we have to create a mask out of each
271 // checkbox that the user checked.
272 //
273 if (SendMessage(GetDlgItem(hDlg, IDC_CPU0), BM_GETCHECK, 0, 0))
274 dwProcessAffinityMask |= 0x00000001;
275 if (SendMessage(GetDlgItem(hDlg, IDC_CPU1), BM_GETCHECK, 0, 0))
276 dwProcessAffinityMask |= 0x00000002;
277 if (SendMessage(GetDlgItem(hDlg, IDC_CPU2), BM_GETCHECK, 0, 0))
278 dwProcessAffinityMask |= 0x00000004;
279 if (SendMessage(GetDlgItem(hDlg, IDC_CPU3), BM_GETCHECK, 0, 0))
280 dwProcessAffinityMask |= 0x00000008;
281 if (SendMessage(GetDlgItem(hDlg, IDC_CPU4), BM_GETCHECK, 0, 0))
282 dwProcessAffinityMask |= 0x00000010;
283 if (SendMessage(GetDlgItem(hDlg, IDC_CPU5), BM_GETCHECK, 0, 0))
284 dwProcessAffinityMask |= 0x00000020;
285 if (SendMessage(GetDlgItem(hDlg, IDC_CPU6), BM_GETCHECK, 0, 0))
286 dwProcessAffinityMask |= 0x00000040;
287 if (SendMessage(GetDlgItem(hDlg, IDC_CPU7), BM_GETCHECK, 0, 0))
288 dwProcessAffinityMask |= 0x00000080;
289 if (SendMessage(GetDlgItem(hDlg, IDC_CPU8), BM_GETCHECK, 0, 0))
290 dwProcessAffinityMask |= 0x00000100;
291 if (SendMessage(GetDlgItem(hDlg, IDC_CPU9), BM_GETCHECK, 0, 0))
292 dwProcessAffinityMask |= 0x00000200;
293 if (SendMessage(GetDlgItem(hDlg, IDC_CPU10), BM_GETCHECK, 0, 0))
294 dwProcessAffinityMask |= 0x00000400;
295 if (SendMessage(GetDlgItem(hDlg, IDC_CPU11), BM_GETCHECK, 0, 0))
296 dwProcessAffinityMask |= 0x00000800;
297 if (SendMessage(GetDlgItem(hDlg, IDC_CPU12), BM_GETCHECK, 0, 0))
298 dwProcessAffinityMask |= 0x00001000;
299 if (SendMessage(GetDlgItem(hDlg, IDC_CPU13), BM_GETCHECK, 0, 0))
300 dwProcessAffinityMask |= 0x00002000;
301 if (SendMessage(GetDlgItem(hDlg, IDC_CPU14), BM_GETCHECK, 0, 0))
302 dwProcessAffinityMask |= 0x00004000;
303 if (SendMessage(GetDlgItem(hDlg, IDC_CPU15), BM_GETCHECK, 0, 0))
304 dwProcessAffinityMask |= 0x00008000;
305 if (SendMessage(GetDlgItem(hDlg, IDC_CPU16), BM_GETCHECK, 0, 0))
306 dwProcessAffinityMask |= 0x00010000;
307 if (SendMessage(GetDlgItem(hDlg, IDC_CPU17), BM_GETCHECK, 0, 0))
308 dwProcessAffinityMask |= 0x00020000;
309 if (SendMessage(GetDlgItem(hDlg, IDC_CPU18), BM_GETCHECK, 0, 0))
310 dwProcessAffinityMask |= 0x00040000;
311 if (SendMessage(GetDlgItem(hDlg, IDC_CPU19), BM_GETCHECK, 0, 0))
312 dwProcessAffinityMask |= 0x00080000;
313 if (SendMessage(GetDlgItem(hDlg, IDC_CPU20), BM_GETCHECK, 0, 0))
314 dwProcessAffinityMask |= 0x00100000;
315 if (SendMessage(GetDlgItem(hDlg, IDC_CPU21), BM_GETCHECK, 0, 0))
316 dwProcessAffinityMask |= 0x00200000;
317 if (SendMessage(GetDlgItem(hDlg, IDC_CPU22), BM_GETCHECK, 0, 0))
318 dwProcessAffinityMask |= 0x00400000;
319 if (SendMessage(GetDlgItem(hDlg, IDC_CPU23), BM_GETCHECK, 0, 0))
320 dwProcessAffinityMask |= 0x00800000;
321 if (SendMessage(GetDlgItem(hDlg, IDC_CPU24), BM_GETCHECK, 0, 0))
322 dwProcessAffinityMask |= 0x01000000;
323 if (SendMessage(GetDlgItem(hDlg, IDC_CPU25), BM_GETCHECK, 0, 0))
324 dwProcessAffinityMask |= 0x02000000;
325 if (SendMessage(GetDlgItem(hDlg, IDC_CPU26), BM_GETCHECK, 0, 0))
326 dwProcessAffinityMask |= 0x04000000;
327 if (SendMessage(GetDlgItem(hDlg, IDC_CPU27), BM_GETCHECK, 0, 0))
328 dwProcessAffinityMask |= 0x08000000;
329 if (SendMessage(GetDlgItem(hDlg, IDC_CPU28), BM_GETCHECK, 0, 0))
330 dwProcessAffinityMask |= 0x10000000;
331 if (SendMessage(GetDlgItem(hDlg, IDC_CPU29), BM_GETCHECK, 0, 0))
332 dwProcessAffinityMask |= 0x20000000;
333 if (SendMessage(GetDlgItem(hDlg, IDC_CPU30), BM_GETCHECK, 0, 0))
334 dwProcessAffinityMask |= 0x40000000;
335 if (SendMessage(GetDlgItem(hDlg, IDC_CPU31), BM_GETCHECK, 0, 0))
336 dwProcessAffinityMask |= 0x80000000;
337
338 //
339 // Make sure they are giving the process affinity
340 // with at least one processor. I'd hate to see a
341 // process that is not in a wait state get deprived
342 // of it's cpu time.
343 //
344 if (!dwProcessAffinityMask)
345 {
346 MessageBox(hDlg, "The process must have affinity with at least one processor.", "Invalid Option", MB_OK|MB_ICONSTOP);
347 return TRUE;
348 }
349
350 //
351 // Try to set the process affinity
352 //
353 if (!SetProcessAffinityMask(hProcessAffinityHandle, dwProcessAffinityMask))
354 {
355 GetLastErrorText(strErrorText, 260);
356 EndDialog(hDlg, LOWORD(wParam));
357 MessageBox(hMainWnd, strErrorText, "Unable to Access or Set Process Affinity", MB_OK|MB_ICONSTOP);
358 }
359
360 EndDialog(hDlg, LOWORD(wParam));
361 return TRUE;
362 }
363
364 break;
365 }
366
367 return 0;
368 }