rundll32: Build with -mwindows.
[wine/multimedia.git] / programs / taskmgr / affinity.c
blob1b6d20e4295dfb6f76ec647a8f90ae30e6f14c10
1 /*
2 * ReactOS Task Manager
4 * affinity.c
6 * Copyright (C) 1999 - 2001 Brian Palmer <brianp@reactos.org>
7 * Copyright (C) 2008 Vladimir Pankratov
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #define WIN32_LEAN_AND_MEAN /* Exclude rarely-used stuff from Windows headers */
25 #include <windows.h>
26 #include <commctrl.h>
27 #include <stdlib.h>
28 #include <malloc.h>
29 #include <memory.h>
30 #include <winnt.h>
31 #include <stdio.h>
33 #include "wine/unicode.h"
34 #include "taskmgr.h"
35 #include "perfdata.h"
37 HANDLE hProcessAffinityHandle;
39 static const WCHAR wszUnable2Access[] = {'U','n','a','b','l','e',' ','t','o',' ',
40 'A','c','c','e','s','s',' ','o','r',' ',
41 'S','e','t',' ','P','r','o','c','e','s','s',' ',
42 'A','f','f','i','n','i','t','y',0};
44 static INT_PTR CALLBACK
45 AffinityDialogWndProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
47 DWORD_PTR dwProcessAffinityMask = 0;
48 DWORD_PTR dwSystemAffinityMask = 0;
49 WCHAR wstrErrorText[256];
51 switch (message) {
52 case WM_INITDIALOG:
55 * Get the current affinity mask for the process and
56 * the number of CPUs present in the system
58 if (!GetProcessAffinityMask(hProcessAffinityHandle, &dwProcessAffinityMask, &dwSystemAffinityMask)) {
59 GetLastErrorText(wstrErrorText, sizeof(wstrErrorText)/sizeof(WCHAR));
60 EndDialog(hDlg, 0);
61 MessageBoxW(hMainWnd, wstrErrorText, wszUnable2Access, MB_OK|MB_ICONSTOP);
65 * Enable a checkbox for each processor present in the system
67 if (dwSystemAffinityMask & 0x00000001)
68 EnableWindow(GetDlgItem(hDlg, IDC_CPU0), TRUE);
69 if (dwSystemAffinityMask & 0x00000002)
70 EnableWindow(GetDlgItem(hDlg, IDC_CPU1), TRUE);
71 if (dwSystemAffinityMask & 0x00000004)
72 EnableWindow(GetDlgItem(hDlg, IDC_CPU2), TRUE);
73 if (dwSystemAffinityMask & 0x00000008)
74 EnableWindow(GetDlgItem(hDlg, IDC_CPU3), TRUE);
75 if (dwSystemAffinityMask & 0x00000010)
76 EnableWindow(GetDlgItem(hDlg, IDC_CPU4), TRUE);
77 if (dwSystemAffinityMask & 0x00000020)
78 EnableWindow(GetDlgItem(hDlg, IDC_CPU5), TRUE);
79 if (dwSystemAffinityMask & 0x00000040)
80 EnableWindow(GetDlgItem(hDlg, IDC_CPU6), TRUE);
81 if (dwSystemAffinityMask & 0x00000080)
82 EnableWindow(GetDlgItem(hDlg, IDC_CPU7), TRUE);
83 if (dwSystemAffinityMask & 0x00000100)
84 EnableWindow(GetDlgItem(hDlg, IDC_CPU8), TRUE);
85 if (dwSystemAffinityMask & 0x00000200)
86 EnableWindow(GetDlgItem(hDlg, IDC_CPU9), TRUE);
87 if (dwSystemAffinityMask & 0x00000400)
88 EnableWindow(GetDlgItem(hDlg, IDC_CPU10), TRUE);
89 if (dwSystemAffinityMask & 0x00000800)
90 EnableWindow(GetDlgItem(hDlg, IDC_CPU11), TRUE);
91 if (dwSystemAffinityMask & 0x00001000)
92 EnableWindow(GetDlgItem(hDlg, IDC_CPU12), TRUE);
93 if (dwSystemAffinityMask & 0x00002000)
94 EnableWindow(GetDlgItem(hDlg, IDC_CPU13), TRUE);
95 if (dwSystemAffinityMask & 0x00004000)
96 EnableWindow(GetDlgItem(hDlg, IDC_CPU14), TRUE);
97 if (dwSystemAffinityMask & 0x00008000)
98 EnableWindow(GetDlgItem(hDlg, IDC_CPU15), TRUE);
99 if (dwSystemAffinityMask & 0x00010000)
100 EnableWindow(GetDlgItem(hDlg, IDC_CPU16), TRUE);
101 if (dwSystemAffinityMask & 0x00020000)
102 EnableWindow(GetDlgItem(hDlg, IDC_CPU17), TRUE);
103 if (dwSystemAffinityMask & 0x00040000)
104 EnableWindow(GetDlgItem(hDlg, IDC_CPU18), TRUE);
105 if (dwSystemAffinityMask & 0x00080000)
106 EnableWindow(GetDlgItem(hDlg, IDC_CPU19), TRUE);
107 if (dwSystemAffinityMask & 0x00100000)
108 EnableWindow(GetDlgItem(hDlg, IDC_CPU20), TRUE);
109 if (dwSystemAffinityMask & 0x00200000)
110 EnableWindow(GetDlgItem(hDlg, IDC_CPU21), TRUE);
111 if (dwSystemAffinityMask & 0x00400000)
112 EnableWindow(GetDlgItem(hDlg, IDC_CPU22), TRUE);
113 if (dwSystemAffinityMask & 0x00800000)
114 EnableWindow(GetDlgItem(hDlg, IDC_CPU23), TRUE);
115 if (dwSystemAffinityMask & 0x01000000)
116 EnableWindow(GetDlgItem(hDlg, IDC_CPU24), TRUE);
117 if (dwSystemAffinityMask & 0x02000000)
118 EnableWindow(GetDlgItem(hDlg, IDC_CPU25), TRUE);
119 if (dwSystemAffinityMask & 0x04000000)
120 EnableWindow(GetDlgItem(hDlg, IDC_CPU26), TRUE);
121 if (dwSystemAffinityMask & 0x08000000)
122 EnableWindow(GetDlgItem(hDlg, IDC_CPU27), TRUE);
123 if (dwSystemAffinityMask & 0x10000000)
124 EnableWindow(GetDlgItem(hDlg, IDC_CPU28), TRUE);
125 if (dwSystemAffinityMask & 0x20000000)
126 EnableWindow(GetDlgItem(hDlg, IDC_CPU29), TRUE);
127 if (dwSystemAffinityMask & 0x40000000)
128 EnableWindow(GetDlgItem(hDlg, IDC_CPU30), TRUE);
129 if (dwSystemAffinityMask & 0x80000000)
130 EnableWindow(GetDlgItem(hDlg, IDC_CPU31), TRUE);
134 * Check each checkbox that the current process
135 * has affinity with
137 if (dwProcessAffinityMask & 0x00000001)
138 SendMessageW(GetDlgItem(hDlg, IDC_CPU0), BM_SETCHECK, BST_CHECKED, 0);
139 if (dwProcessAffinityMask & 0x00000002)
140 SendMessageW(GetDlgItem(hDlg, IDC_CPU1), BM_SETCHECK, BST_CHECKED, 0);
141 if (dwProcessAffinityMask & 0x00000004)
142 SendMessageW(GetDlgItem(hDlg, IDC_CPU2), BM_SETCHECK, BST_CHECKED, 0);
143 if (dwProcessAffinityMask & 0x00000008)
144 SendMessageW(GetDlgItem(hDlg, IDC_CPU3), BM_SETCHECK, BST_CHECKED, 0);
145 if (dwProcessAffinityMask & 0x00000010)
146 SendMessageW(GetDlgItem(hDlg, IDC_CPU4), BM_SETCHECK, BST_CHECKED, 0);
147 if (dwProcessAffinityMask & 0x00000020)
148 SendMessageW(GetDlgItem(hDlg, IDC_CPU5), BM_SETCHECK, BST_CHECKED, 0);
149 if (dwProcessAffinityMask & 0x00000040)
150 SendMessageW(GetDlgItem(hDlg, IDC_CPU6), BM_SETCHECK, BST_CHECKED, 0);
151 if (dwProcessAffinityMask & 0x00000080)
152 SendMessageW(GetDlgItem(hDlg, IDC_CPU7), BM_SETCHECK, BST_CHECKED, 0);
153 if (dwProcessAffinityMask & 0x00000100)
154 SendMessageW(GetDlgItem(hDlg, IDC_CPU8), BM_SETCHECK, BST_CHECKED, 0);
155 if (dwProcessAffinityMask & 0x00000200)
156 SendMessageW(GetDlgItem(hDlg, IDC_CPU9), BM_SETCHECK, BST_CHECKED, 0);
157 if (dwProcessAffinityMask & 0x00000400)
158 SendMessageW(GetDlgItem(hDlg, IDC_CPU10), BM_SETCHECK, BST_CHECKED, 0);
159 if (dwProcessAffinityMask & 0x00000800)
160 SendMessageW(GetDlgItem(hDlg, IDC_CPU11), BM_SETCHECK, BST_CHECKED, 0);
161 if (dwProcessAffinityMask & 0x00001000)
162 SendMessageW(GetDlgItem(hDlg, IDC_CPU12), BM_SETCHECK, BST_CHECKED, 0);
163 if (dwProcessAffinityMask & 0x00002000)
164 SendMessageW(GetDlgItem(hDlg, IDC_CPU13), BM_SETCHECK, BST_CHECKED, 0);
165 if (dwProcessAffinityMask & 0x00004000)
166 SendMessageW(GetDlgItem(hDlg, IDC_CPU14), BM_SETCHECK, BST_CHECKED, 0);
167 if (dwProcessAffinityMask & 0x00008000)
168 SendMessageW(GetDlgItem(hDlg, IDC_CPU15), BM_SETCHECK, BST_CHECKED, 0);
169 if (dwProcessAffinityMask & 0x00010000)
170 SendMessageW(GetDlgItem(hDlg, IDC_CPU16), BM_SETCHECK, BST_CHECKED, 0);
171 if (dwProcessAffinityMask & 0x00020000)
172 SendMessageW(GetDlgItem(hDlg, IDC_CPU17), BM_SETCHECK, BST_CHECKED, 0);
173 if (dwProcessAffinityMask & 0x00040000)
174 SendMessageW(GetDlgItem(hDlg, IDC_CPU18), BM_SETCHECK, BST_CHECKED, 0);
175 if (dwProcessAffinityMask & 0x00080000)
176 SendMessageW(GetDlgItem(hDlg, IDC_CPU19), BM_SETCHECK, BST_CHECKED, 0);
177 if (dwProcessAffinityMask & 0x00100000)
178 SendMessageW(GetDlgItem(hDlg, IDC_CPU20), BM_SETCHECK, BST_CHECKED, 0);
179 if (dwProcessAffinityMask & 0x00200000)
180 SendMessageW(GetDlgItem(hDlg, IDC_CPU21), BM_SETCHECK, BST_CHECKED, 0);
181 if (dwProcessAffinityMask & 0x00400000)
182 SendMessageW(GetDlgItem(hDlg, IDC_CPU22), BM_SETCHECK, BST_CHECKED, 0);
183 if (dwProcessAffinityMask & 0x00800000)
184 SendMessageW(GetDlgItem(hDlg, IDC_CPU23), BM_SETCHECK, BST_CHECKED, 0);
185 if (dwProcessAffinityMask & 0x01000000)
186 SendMessageW(GetDlgItem(hDlg, IDC_CPU24), BM_SETCHECK, BST_CHECKED, 0);
187 if (dwProcessAffinityMask & 0x02000000)
188 SendMessageW(GetDlgItem(hDlg, IDC_CPU25), BM_SETCHECK, BST_CHECKED, 0);
189 if (dwProcessAffinityMask & 0x04000000)
190 SendMessageW(GetDlgItem(hDlg, IDC_CPU26), BM_SETCHECK, BST_CHECKED, 0);
191 if (dwProcessAffinityMask & 0x08000000)
192 SendMessageW(GetDlgItem(hDlg, IDC_CPU27), BM_SETCHECK, BST_CHECKED, 0);
193 if (dwProcessAffinityMask & 0x10000000)
194 SendMessageW(GetDlgItem(hDlg, IDC_CPU28), BM_SETCHECK, BST_CHECKED, 0);
195 if (dwProcessAffinityMask & 0x20000000)
196 SendMessageW(GetDlgItem(hDlg, IDC_CPU29), BM_SETCHECK, BST_CHECKED, 0);
197 if (dwProcessAffinityMask & 0x40000000)
198 SendMessageW(GetDlgItem(hDlg, IDC_CPU30), BM_SETCHECK, BST_CHECKED, 0);
199 if (dwProcessAffinityMask & 0x80000000)
200 SendMessageW(GetDlgItem(hDlg, IDC_CPU31), BM_SETCHECK, BST_CHECKED, 0);
202 return TRUE;
204 case WM_COMMAND:
207 * If the user has cancelled the dialog box
208 * then just close it
210 if (LOWORD(wParam) == IDCANCEL) {
211 EndDialog(hDlg, LOWORD(wParam));
212 return TRUE;
216 * The user has clicked OK -- so now we have
217 * to adjust the process affinity mask
219 if (LOWORD(wParam) == IDOK) {
221 * First we have to create a mask out of each
222 * checkbox that the user checked.
224 if (SendMessageW(GetDlgItem(hDlg, IDC_CPU0), BM_GETCHECK, 0, 0))
225 dwProcessAffinityMask |= 0x00000001;
226 if (SendMessageW(GetDlgItem(hDlg, IDC_CPU1), BM_GETCHECK, 0, 0))
227 dwProcessAffinityMask |= 0x00000002;
228 if (SendMessageW(GetDlgItem(hDlg, IDC_CPU2), BM_GETCHECK, 0, 0))
229 dwProcessAffinityMask |= 0x00000004;
230 if (SendMessageW(GetDlgItem(hDlg, IDC_CPU3), BM_GETCHECK, 0, 0))
231 dwProcessAffinityMask |= 0x00000008;
232 if (SendMessageW(GetDlgItem(hDlg, IDC_CPU4), BM_GETCHECK, 0, 0))
233 dwProcessAffinityMask |= 0x00000010;
234 if (SendMessageW(GetDlgItem(hDlg, IDC_CPU5), BM_GETCHECK, 0, 0))
235 dwProcessAffinityMask |= 0x00000020;
236 if (SendMessageW(GetDlgItem(hDlg, IDC_CPU6), BM_GETCHECK, 0, 0))
237 dwProcessAffinityMask |= 0x00000040;
238 if (SendMessageW(GetDlgItem(hDlg, IDC_CPU7), BM_GETCHECK, 0, 0))
239 dwProcessAffinityMask |= 0x00000080;
240 if (SendMessageW(GetDlgItem(hDlg, IDC_CPU8), BM_GETCHECK, 0, 0))
241 dwProcessAffinityMask |= 0x00000100;
242 if (SendMessageW(GetDlgItem(hDlg, IDC_CPU9), BM_GETCHECK, 0, 0))
243 dwProcessAffinityMask |= 0x00000200;
244 if (SendMessageW(GetDlgItem(hDlg, IDC_CPU10), BM_GETCHECK, 0, 0))
245 dwProcessAffinityMask |= 0x00000400;
246 if (SendMessageW(GetDlgItem(hDlg, IDC_CPU11), BM_GETCHECK, 0, 0))
247 dwProcessAffinityMask |= 0x00000800;
248 if (SendMessageW(GetDlgItem(hDlg, IDC_CPU12), BM_GETCHECK, 0, 0))
249 dwProcessAffinityMask |= 0x00001000;
250 if (SendMessageW(GetDlgItem(hDlg, IDC_CPU13), BM_GETCHECK, 0, 0))
251 dwProcessAffinityMask |= 0x00002000;
252 if (SendMessageW(GetDlgItem(hDlg, IDC_CPU14), BM_GETCHECK, 0, 0))
253 dwProcessAffinityMask |= 0x00004000;
254 if (SendMessageW(GetDlgItem(hDlg, IDC_CPU15), BM_GETCHECK, 0, 0))
255 dwProcessAffinityMask |= 0x00008000;
256 if (SendMessageW(GetDlgItem(hDlg, IDC_CPU16), BM_GETCHECK, 0, 0))
257 dwProcessAffinityMask |= 0x00010000;
258 if (SendMessageW(GetDlgItem(hDlg, IDC_CPU17), BM_GETCHECK, 0, 0))
259 dwProcessAffinityMask |= 0x00020000;
260 if (SendMessageW(GetDlgItem(hDlg, IDC_CPU18), BM_GETCHECK, 0, 0))
261 dwProcessAffinityMask |= 0x00040000;
262 if (SendMessageW(GetDlgItem(hDlg, IDC_CPU19), BM_GETCHECK, 0, 0))
263 dwProcessAffinityMask |= 0x00080000;
264 if (SendMessageW(GetDlgItem(hDlg, IDC_CPU20), BM_GETCHECK, 0, 0))
265 dwProcessAffinityMask |= 0x00100000;
266 if (SendMessageW(GetDlgItem(hDlg, IDC_CPU21), BM_GETCHECK, 0, 0))
267 dwProcessAffinityMask |= 0x00200000;
268 if (SendMessageW(GetDlgItem(hDlg, IDC_CPU22), BM_GETCHECK, 0, 0))
269 dwProcessAffinityMask |= 0x00400000;
270 if (SendMessageW(GetDlgItem(hDlg, IDC_CPU23), BM_GETCHECK, 0, 0))
271 dwProcessAffinityMask |= 0x00800000;
272 if (SendMessageW(GetDlgItem(hDlg, IDC_CPU24), BM_GETCHECK, 0, 0))
273 dwProcessAffinityMask |= 0x01000000;
274 if (SendMessageW(GetDlgItem(hDlg, IDC_CPU25), BM_GETCHECK, 0, 0))
275 dwProcessAffinityMask |= 0x02000000;
276 if (SendMessageW(GetDlgItem(hDlg, IDC_CPU26), BM_GETCHECK, 0, 0))
277 dwProcessAffinityMask |= 0x04000000;
278 if (SendMessageW(GetDlgItem(hDlg, IDC_CPU27), BM_GETCHECK, 0, 0))
279 dwProcessAffinityMask |= 0x08000000;
280 if (SendMessageW(GetDlgItem(hDlg, IDC_CPU28), BM_GETCHECK, 0, 0))
281 dwProcessAffinityMask |= 0x10000000;
282 if (SendMessageW(GetDlgItem(hDlg, IDC_CPU29), BM_GETCHECK, 0, 0))
283 dwProcessAffinityMask |= 0x20000000;
284 if (SendMessageW(GetDlgItem(hDlg, IDC_CPU30), BM_GETCHECK, 0, 0))
285 dwProcessAffinityMask |= 0x40000000;
286 if (SendMessageW(GetDlgItem(hDlg, IDC_CPU31), BM_GETCHECK, 0, 0))
287 dwProcessAffinityMask |= 0x80000000;
290 * Make sure they are giving the process affinity
291 * with at least one processor. I'd hate to see a
292 * process that is not in a wait state get deprived
293 * of it's cpu time.
295 if (!dwProcessAffinityMask) {
296 static const WCHAR wszErrorMsg[] = {'T','h','e',' ','p','r','o','c','e','s','s',' ',
297 'm','u','s','t',' ','h','a','v','e',' ',
298 'a','f','f','i','n','i','t','y',' ',
299 'w','i','t','h',' ','a','t',' ','l','e','a','s','t',' ',
300 'o','n','e',' ','p','r','o','c','e','s','s','o','r','.',0};
301 static const WCHAR wszErrorTitle[] = {'I','n','v','a','l','i','d',' ','O','p','t','i','o','n',0};
302 MessageBoxW(hDlg, wszErrorMsg, wszErrorTitle, MB_OK|MB_ICONSTOP);
303 return TRUE;
307 * Try to set the process affinity
309 if (!SetProcessAffinityMask(hProcessAffinityHandle, dwProcessAffinityMask)) {
310 GetLastErrorText(wstrErrorText, sizeof(wstrErrorText)/sizeof(WCHAR));
311 EndDialog(hDlg, LOWORD(wParam));
312 MessageBoxW(hMainWnd, wstrErrorText, wszUnable2Access, MB_OK|MB_ICONSTOP);
315 EndDialog(hDlg, LOWORD(wParam));
316 return TRUE;
319 break;
322 return 0;
325 void ProcessPage_OnSetAffinity(void)
327 LV_ITEMW lvitem;
328 ULONG Index;
329 DWORD dwProcessId;
330 WCHAR wstrErrorText[256];
332 for (Index=0; Index<(ULONG)ListView_GetItemCount(hProcessPageListCtrl); Index++) {
333 memset(&lvitem, 0, sizeof(LV_ITEMW));
334 lvitem.mask = LVIF_STATE;
335 lvitem.stateMask = LVIS_SELECTED;
336 lvitem.iItem = Index;
337 SendMessageW(hProcessPageListCtrl, LVM_GETITEMW, 0, (LPARAM) &lvitem);
338 if (lvitem.state & LVIS_SELECTED)
339 break;
341 dwProcessId = PerfDataGetProcessId(Index);
342 if ((ListView_GetSelectedCount(hProcessPageListCtrl) != 1) || (dwProcessId == 0))
343 return;
344 hProcessAffinityHandle = OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_SET_INFORMATION, FALSE, dwProcessId);
345 if (!hProcessAffinityHandle) {
346 GetLastErrorText(wstrErrorText, sizeof(wstrErrorText)/sizeof(WCHAR));
347 MessageBoxW(hMainWnd, wstrErrorText, wszUnable2Access, MB_OK|MB_ICONSTOP);
348 return;
350 DialogBoxW(hInst, MAKEINTRESOURCEW(IDD_AFFINITY_DIALOG), hMainWnd, AffinityDialogWndProc);
351 if (hProcessAffinityHandle) {
352 CloseHandle(hProcessAffinityHandle);
353 hProcessAffinityHandle = NULL;