cfgmgr32: Forward CM_Locate_DevNodeA/W to setupapi.
[wine.git] / programs / taskmgr / affinity.c
blob31a4f64a6ebf949aecc0d23e7755bea9995ed1a1
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 <memory.h>
29 #include <winnt.h>
30 #include <stdio.h>
32 #include "wine/unicode.h"
33 #include "taskmgr.h"
34 #include "perfdata.h"
36 HANDLE hProcessAffinityHandle;
38 static const WCHAR wszUnable2Access[] = {'U','n','a','b','l','e',' ','t','o',' ',
39 'A','c','c','e','s','s',' ','o','r',' ',
40 'S','e','t',' ','P','r','o','c','e','s','s',' ',
41 'A','f','f','i','n','i','t','y',0};
43 static INT_PTR CALLBACK
44 AffinityDialogWndProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
46 DWORD_PTR dwProcessAffinityMask = 0;
47 DWORD_PTR dwSystemAffinityMask = 0;
48 WCHAR wstrErrorText[256];
50 switch (message) {
51 case WM_INITDIALOG:
54 * Get the current affinity mask for the process and
55 * the number of CPUs present in the system
57 if (!GetProcessAffinityMask(hProcessAffinityHandle, &dwProcessAffinityMask, &dwSystemAffinityMask)) {
58 GetLastErrorText(wstrErrorText, sizeof(wstrErrorText)/sizeof(WCHAR));
59 EndDialog(hDlg, 0);
60 MessageBoxW(hMainWnd, wstrErrorText, wszUnable2Access, MB_OK|MB_ICONSTOP);
64 * Enable a checkbox for each processor present in the system
66 if (dwSystemAffinityMask & 0x00000001)
67 EnableWindow(GetDlgItem(hDlg, IDC_CPU0), TRUE);
68 if (dwSystemAffinityMask & 0x00000002)
69 EnableWindow(GetDlgItem(hDlg, IDC_CPU1), TRUE);
70 if (dwSystemAffinityMask & 0x00000004)
71 EnableWindow(GetDlgItem(hDlg, IDC_CPU2), TRUE);
72 if (dwSystemAffinityMask & 0x00000008)
73 EnableWindow(GetDlgItem(hDlg, IDC_CPU3), TRUE);
74 if (dwSystemAffinityMask & 0x00000010)
75 EnableWindow(GetDlgItem(hDlg, IDC_CPU4), TRUE);
76 if (dwSystemAffinityMask & 0x00000020)
77 EnableWindow(GetDlgItem(hDlg, IDC_CPU5), TRUE);
78 if (dwSystemAffinityMask & 0x00000040)
79 EnableWindow(GetDlgItem(hDlg, IDC_CPU6), TRUE);
80 if (dwSystemAffinityMask & 0x00000080)
81 EnableWindow(GetDlgItem(hDlg, IDC_CPU7), TRUE);
82 if (dwSystemAffinityMask & 0x00000100)
83 EnableWindow(GetDlgItem(hDlg, IDC_CPU8), TRUE);
84 if (dwSystemAffinityMask & 0x00000200)
85 EnableWindow(GetDlgItem(hDlg, IDC_CPU9), TRUE);
86 if (dwSystemAffinityMask & 0x00000400)
87 EnableWindow(GetDlgItem(hDlg, IDC_CPU10), TRUE);
88 if (dwSystemAffinityMask & 0x00000800)
89 EnableWindow(GetDlgItem(hDlg, IDC_CPU11), TRUE);
90 if (dwSystemAffinityMask & 0x00001000)
91 EnableWindow(GetDlgItem(hDlg, IDC_CPU12), TRUE);
92 if (dwSystemAffinityMask & 0x00002000)
93 EnableWindow(GetDlgItem(hDlg, IDC_CPU13), TRUE);
94 if (dwSystemAffinityMask & 0x00004000)
95 EnableWindow(GetDlgItem(hDlg, IDC_CPU14), TRUE);
96 if (dwSystemAffinityMask & 0x00008000)
97 EnableWindow(GetDlgItem(hDlg, IDC_CPU15), TRUE);
98 if (dwSystemAffinityMask & 0x00010000)
99 EnableWindow(GetDlgItem(hDlg, IDC_CPU16), TRUE);
100 if (dwSystemAffinityMask & 0x00020000)
101 EnableWindow(GetDlgItem(hDlg, IDC_CPU17), TRUE);
102 if (dwSystemAffinityMask & 0x00040000)
103 EnableWindow(GetDlgItem(hDlg, IDC_CPU18), TRUE);
104 if (dwSystemAffinityMask & 0x00080000)
105 EnableWindow(GetDlgItem(hDlg, IDC_CPU19), TRUE);
106 if (dwSystemAffinityMask & 0x00100000)
107 EnableWindow(GetDlgItem(hDlg, IDC_CPU20), TRUE);
108 if (dwSystemAffinityMask & 0x00200000)
109 EnableWindow(GetDlgItem(hDlg, IDC_CPU21), TRUE);
110 if (dwSystemAffinityMask & 0x00400000)
111 EnableWindow(GetDlgItem(hDlg, IDC_CPU22), TRUE);
112 if (dwSystemAffinityMask & 0x00800000)
113 EnableWindow(GetDlgItem(hDlg, IDC_CPU23), TRUE);
114 if (dwSystemAffinityMask & 0x01000000)
115 EnableWindow(GetDlgItem(hDlg, IDC_CPU24), TRUE);
116 if (dwSystemAffinityMask & 0x02000000)
117 EnableWindow(GetDlgItem(hDlg, IDC_CPU25), TRUE);
118 if (dwSystemAffinityMask & 0x04000000)
119 EnableWindow(GetDlgItem(hDlg, IDC_CPU26), TRUE);
120 if (dwSystemAffinityMask & 0x08000000)
121 EnableWindow(GetDlgItem(hDlg, IDC_CPU27), TRUE);
122 if (dwSystemAffinityMask & 0x10000000)
123 EnableWindow(GetDlgItem(hDlg, IDC_CPU28), TRUE);
124 if (dwSystemAffinityMask & 0x20000000)
125 EnableWindow(GetDlgItem(hDlg, IDC_CPU29), TRUE);
126 if (dwSystemAffinityMask & 0x40000000)
127 EnableWindow(GetDlgItem(hDlg, IDC_CPU30), TRUE);
128 if (dwSystemAffinityMask & 0x80000000)
129 EnableWindow(GetDlgItem(hDlg, IDC_CPU31), TRUE);
133 * Check each checkbox that the current process
134 * has affinity with
136 if (dwProcessAffinityMask & 0x00000001)
137 SendMessageW(GetDlgItem(hDlg, IDC_CPU0), BM_SETCHECK, BST_CHECKED, 0);
138 if (dwProcessAffinityMask & 0x00000002)
139 SendMessageW(GetDlgItem(hDlg, IDC_CPU1), BM_SETCHECK, BST_CHECKED, 0);
140 if (dwProcessAffinityMask & 0x00000004)
141 SendMessageW(GetDlgItem(hDlg, IDC_CPU2), BM_SETCHECK, BST_CHECKED, 0);
142 if (dwProcessAffinityMask & 0x00000008)
143 SendMessageW(GetDlgItem(hDlg, IDC_CPU3), BM_SETCHECK, BST_CHECKED, 0);
144 if (dwProcessAffinityMask & 0x00000010)
145 SendMessageW(GetDlgItem(hDlg, IDC_CPU4), BM_SETCHECK, BST_CHECKED, 0);
146 if (dwProcessAffinityMask & 0x00000020)
147 SendMessageW(GetDlgItem(hDlg, IDC_CPU5), BM_SETCHECK, BST_CHECKED, 0);
148 if (dwProcessAffinityMask & 0x00000040)
149 SendMessageW(GetDlgItem(hDlg, IDC_CPU6), BM_SETCHECK, BST_CHECKED, 0);
150 if (dwProcessAffinityMask & 0x00000080)
151 SendMessageW(GetDlgItem(hDlg, IDC_CPU7), BM_SETCHECK, BST_CHECKED, 0);
152 if (dwProcessAffinityMask & 0x00000100)
153 SendMessageW(GetDlgItem(hDlg, IDC_CPU8), BM_SETCHECK, BST_CHECKED, 0);
154 if (dwProcessAffinityMask & 0x00000200)
155 SendMessageW(GetDlgItem(hDlg, IDC_CPU9), BM_SETCHECK, BST_CHECKED, 0);
156 if (dwProcessAffinityMask & 0x00000400)
157 SendMessageW(GetDlgItem(hDlg, IDC_CPU10), BM_SETCHECK, BST_CHECKED, 0);
158 if (dwProcessAffinityMask & 0x00000800)
159 SendMessageW(GetDlgItem(hDlg, IDC_CPU11), BM_SETCHECK, BST_CHECKED, 0);
160 if (dwProcessAffinityMask & 0x00001000)
161 SendMessageW(GetDlgItem(hDlg, IDC_CPU12), BM_SETCHECK, BST_CHECKED, 0);
162 if (dwProcessAffinityMask & 0x00002000)
163 SendMessageW(GetDlgItem(hDlg, IDC_CPU13), BM_SETCHECK, BST_CHECKED, 0);
164 if (dwProcessAffinityMask & 0x00004000)
165 SendMessageW(GetDlgItem(hDlg, IDC_CPU14), BM_SETCHECK, BST_CHECKED, 0);
166 if (dwProcessAffinityMask & 0x00008000)
167 SendMessageW(GetDlgItem(hDlg, IDC_CPU15), BM_SETCHECK, BST_CHECKED, 0);
168 if (dwProcessAffinityMask & 0x00010000)
169 SendMessageW(GetDlgItem(hDlg, IDC_CPU16), BM_SETCHECK, BST_CHECKED, 0);
170 if (dwProcessAffinityMask & 0x00020000)
171 SendMessageW(GetDlgItem(hDlg, IDC_CPU17), BM_SETCHECK, BST_CHECKED, 0);
172 if (dwProcessAffinityMask & 0x00040000)
173 SendMessageW(GetDlgItem(hDlg, IDC_CPU18), BM_SETCHECK, BST_CHECKED, 0);
174 if (dwProcessAffinityMask & 0x00080000)
175 SendMessageW(GetDlgItem(hDlg, IDC_CPU19), BM_SETCHECK, BST_CHECKED, 0);
176 if (dwProcessAffinityMask & 0x00100000)
177 SendMessageW(GetDlgItem(hDlg, IDC_CPU20), BM_SETCHECK, BST_CHECKED, 0);
178 if (dwProcessAffinityMask & 0x00200000)
179 SendMessageW(GetDlgItem(hDlg, IDC_CPU21), BM_SETCHECK, BST_CHECKED, 0);
180 if (dwProcessAffinityMask & 0x00400000)
181 SendMessageW(GetDlgItem(hDlg, IDC_CPU22), BM_SETCHECK, BST_CHECKED, 0);
182 if (dwProcessAffinityMask & 0x00800000)
183 SendMessageW(GetDlgItem(hDlg, IDC_CPU23), BM_SETCHECK, BST_CHECKED, 0);
184 if (dwProcessAffinityMask & 0x01000000)
185 SendMessageW(GetDlgItem(hDlg, IDC_CPU24), BM_SETCHECK, BST_CHECKED, 0);
186 if (dwProcessAffinityMask & 0x02000000)
187 SendMessageW(GetDlgItem(hDlg, IDC_CPU25), BM_SETCHECK, BST_CHECKED, 0);
188 if (dwProcessAffinityMask & 0x04000000)
189 SendMessageW(GetDlgItem(hDlg, IDC_CPU26), BM_SETCHECK, BST_CHECKED, 0);
190 if (dwProcessAffinityMask & 0x08000000)
191 SendMessageW(GetDlgItem(hDlg, IDC_CPU27), BM_SETCHECK, BST_CHECKED, 0);
192 if (dwProcessAffinityMask & 0x10000000)
193 SendMessageW(GetDlgItem(hDlg, IDC_CPU28), BM_SETCHECK, BST_CHECKED, 0);
194 if (dwProcessAffinityMask & 0x20000000)
195 SendMessageW(GetDlgItem(hDlg, IDC_CPU29), BM_SETCHECK, BST_CHECKED, 0);
196 if (dwProcessAffinityMask & 0x40000000)
197 SendMessageW(GetDlgItem(hDlg, IDC_CPU30), BM_SETCHECK, BST_CHECKED, 0);
198 if (dwProcessAffinityMask & 0x80000000)
199 SendMessageW(GetDlgItem(hDlg, IDC_CPU31), BM_SETCHECK, BST_CHECKED, 0);
201 return TRUE;
203 case WM_COMMAND:
206 * If the user has cancelled the dialog box
207 * then just close it
209 if (LOWORD(wParam) == IDCANCEL) {
210 EndDialog(hDlg, LOWORD(wParam));
211 return TRUE;
215 * The user has clicked OK -- so now we have
216 * to adjust the process affinity mask
218 if (LOWORD(wParam) == IDOK) {
220 * First we have to create a mask out of each
221 * checkbox that the user checked.
223 if (SendMessageW(GetDlgItem(hDlg, IDC_CPU0), BM_GETCHECK, 0, 0))
224 dwProcessAffinityMask |= 0x00000001;
225 if (SendMessageW(GetDlgItem(hDlg, IDC_CPU1), BM_GETCHECK, 0, 0))
226 dwProcessAffinityMask |= 0x00000002;
227 if (SendMessageW(GetDlgItem(hDlg, IDC_CPU2), BM_GETCHECK, 0, 0))
228 dwProcessAffinityMask |= 0x00000004;
229 if (SendMessageW(GetDlgItem(hDlg, IDC_CPU3), BM_GETCHECK, 0, 0))
230 dwProcessAffinityMask |= 0x00000008;
231 if (SendMessageW(GetDlgItem(hDlg, IDC_CPU4), BM_GETCHECK, 0, 0))
232 dwProcessAffinityMask |= 0x00000010;
233 if (SendMessageW(GetDlgItem(hDlg, IDC_CPU5), BM_GETCHECK, 0, 0))
234 dwProcessAffinityMask |= 0x00000020;
235 if (SendMessageW(GetDlgItem(hDlg, IDC_CPU6), BM_GETCHECK, 0, 0))
236 dwProcessAffinityMask |= 0x00000040;
237 if (SendMessageW(GetDlgItem(hDlg, IDC_CPU7), BM_GETCHECK, 0, 0))
238 dwProcessAffinityMask |= 0x00000080;
239 if (SendMessageW(GetDlgItem(hDlg, IDC_CPU8), BM_GETCHECK, 0, 0))
240 dwProcessAffinityMask |= 0x00000100;
241 if (SendMessageW(GetDlgItem(hDlg, IDC_CPU9), BM_GETCHECK, 0, 0))
242 dwProcessAffinityMask |= 0x00000200;
243 if (SendMessageW(GetDlgItem(hDlg, IDC_CPU10), BM_GETCHECK, 0, 0))
244 dwProcessAffinityMask |= 0x00000400;
245 if (SendMessageW(GetDlgItem(hDlg, IDC_CPU11), BM_GETCHECK, 0, 0))
246 dwProcessAffinityMask |= 0x00000800;
247 if (SendMessageW(GetDlgItem(hDlg, IDC_CPU12), BM_GETCHECK, 0, 0))
248 dwProcessAffinityMask |= 0x00001000;
249 if (SendMessageW(GetDlgItem(hDlg, IDC_CPU13), BM_GETCHECK, 0, 0))
250 dwProcessAffinityMask |= 0x00002000;
251 if (SendMessageW(GetDlgItem(hDlg, IDC_CPU14), BM_GETCHECK, 0, 0))
252 dwProcessAffinityMask |= 0x00004000;
253 if (SendMessageW(GetDlgItem(hDlg, IDC_CPU15), BM_GETCHECK, 0, 0))
254 dwProcessAffinityMask |= 0x00008000;
255 if (SendMessageW(GetDlgItem(hDlg, IDC_CPU16), BM_GETCHECK, 0, 0))
256 dwProcessAffinityMask |= 0x00010000;
257 if (SendMessageW(GetDlgItem(hDlg, IDC_CPU17), BM_GETCHECK, 0, 0))
258 dwProcessAffinityMask |= 0x00020000;
259 if (SendMessageW(GetDlgItem(hDlg, IDC_CPU18), BM_GETCHECK, 0, 0))
260 dwProcessAffinityMask |= 0x00040000;
261 if (SendMessageW(GetDlgItem(hDlg, IDC_CPU19), BM_GETCHECK, 0, 0))
262 dwProcessAffinityMask |= 0x00080000;
263 if (SendMessageW(GetDlgItem(hDlg, IDC_CPU20), BM_GETCHECK, 0, 0))
264 dwProcessAffinityMask |= 0x00100000;
265 if (SendMessageW(GetDlgItem(hDlg, IDC_CPU21), BM_GETCHECK, 0, 0))
266 dwProcessAffinityMask |= 0x00200000;
267 if (SendMessageW(GetDlgItem(hDlg, IDC_CPU22), BM_GETCHECK, 0, 0))
268 dwProcessAffinityMask |= 0x00400000;
269 if (SendMessageW(GetDlgItem(hDlg, IDC_CPU23), BM_GETCHECK, 0, 0))
270 dwProcessAffinityMask |= 0x00800000;
271 if (SendMessageW(GetDlgItem(hDlg, IDC_CPU24), BM_GETCHECK, 0, 0))
272 dwProcessAffinityMask |= 0x01000000;
273 if (SendMessageW(GetDlgItem(hDlg, IDC_CPU25), BM_GETCHECK, 0, 0))
274 dwProcessAffinityMask |= 0x02000000;
275 if (SendMessageW(GetDlgItem(hDlg, IDC_CPU26), BM_GETCHECK, 0, 0))
276 dwProcessAffinityMask |= 0x04000000;
277 if (SendMessageW(GetDlgItem(hDlg, IDC_CPU27), BM_GETCHECK, 0, 0))
278 dwProcessAffinityMask |= 0x08000000;
279 if (SendMessageW(GetDlgItem(hDlg, IDC_CPU28), BM_GETCHECK, 0, 0))
280 dwProcessAffinityMask |= 0x10000000;
281 if (SendMessageW(GetDlgItem(hDlg, IDC_CPU29), BM_GETCHECK, 0, 0))
282 dwProcessAffinityMask |= 0x20000000;
283 if (SendMessageW(GetDlgItem(hDlg, IDC_CPU30), BM_GETCHECK, 0, 0))
284 dwProcessAffinityMask |= 0x40000000;
285 if (SendMessageW(GetDlgItem(hDlg, IDC_CPU31), BM_GETCHECK, 0, 0))
286 dwProcessAffinityMask |= 0x80000000;
289 * Make sure they are giving the process affinity
290 * with at least one processor. I'd hate to see a
291 * process that is not in a wait state get deprived
292 * of it's cpu time.
294 if (!dwProcessAffinityMask) {
295 static const WCHAR wszErrorMsg[] = {'T','h','e',' ','p','r','o','c','e','s','s',' ',
296 'm','u','s','t',' ','h','a','v','e',' ',
297 'a','f','f','i','n','i','t','y',' ',
298 'w','i','t','h',' ','a','t',' ','l','e','a','s','t',' ',
299 'o','n','e',' ','p','r','o','c','e','s','s','o','r','.',0};
300 static const WCHAR wszErrorTitle[] = {'I','n','v','a','l','i','d',' ','O','p','t','i','o','n',0};
301 MessageBoxW(hDlg, wszErrorMsg, wszErrorTitle, MB_OK|MB_ICONSTOP);
302 return TRUE;
306 * Try to set the process affinity
308 if (!SetProcessAffinityMask(hProcessAffinityHandle, dwProcessAffinityMask)) {
309 GetLastErrorText(wstrErrorText, sizeof(wstrErrorText)/sizeof(WCHAR));
310 EndDialog(hDlg, LOWORD(wParam));
311 MessageBoxW(hMainWnd, wstrErrorText, wszUnable2Access, MB_OK|MB_ICONSTOP);
314 EndDialog(hDlg, LOWORD(wParam));
315 return TRUE;
318 break;
321 return 0;
324 void ProcessPage_OnSetAffinity(void)
326 LV_ITEMW lvitem;
327 ULONG Index;
328 DWORD dwProcessId;
329 WCHAR wstrErrorText[256];
331 for (Index=0; Index<(ULONG)ListView_GetItemCount(hProcessPageListCtrl); Index++) {
332 memset(&lvitem, 0, sizeof(LV_ITEMW));
333 lvitem.mask = LVIF_STATE;
334 lvitem.stateMask = LVIS_SELECTED;
335 lvitem.iItem = Index;
336 SendMessageW(hProcessPageListCtrl, LVM_GETITEMW, 0, (LPARAM) &lvitem);
337 if (lvitem.state & LVIS_SELECTED)
338 break;
340 dwProcessId = PerfDataGetProcessId(Index);
341 if ((ListView_GetSelectedCount(hProcessPageListCtrl) != 1) || (dwProcessId == 0))
342 return;
343 hProcessAffinityHandle = OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_SET_INFORMATION, FALSE, dwProcessId);
344 if (!hProcessAffinityHandle) {
345 GetLastErrorText(wstrErrorText, sizeof(wstrErrorText)/sizeof(WCHAR));
346 MessageBoxW(hMainWnd, wstrErrorText, wszUnable2Access, MB_OK|MB_ICONSTOP);
347 return;
349 DialogBoxW(hInst, MAKEINTRESOURCEW(IDD_AFFINITY_DIALOG), hMainWnd, AffinityDialogWndProc);
350 if (hProcessAffinityHandle) {
351 CloseHandle(hProcessAffinityHandle);
352 hProcessAffinityHandle = NULL;