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 */
32 #include "wine/unicode.h"
36 HANDLE hProcessAffinityHandle
;
38 WCHAR wszUnable2Access
[255];
40 static INT_PTR CALLBACK
41 AffinityDialogWndProc(HWND hDlg
, UINT message
, WPARAM wParam
, LPARAM lParam
)
43 DWORD_PTR dwProcessAffinityMask
= 0;
44 DWORD_PTR dwSystemAffinityMask
= 0;
45 WCHAR wstrErrorText
[256];
51 * Get the current affinity mask for the process and
52 * the number of CPUs present in the system
54 if (!GetProcessAffinityMask(hProcessAffinityHandle
, &dwProcessAffinityMask
, &dwSystemAffinityMask
)) {
55 GetLastErrorText(wstrErrorText
, sizeof(wstrErrorText
)/sizeof(WCHAR
));
57 LoadStringW(hInst
, IDS_AFFINITY_UNABLE2ACCESS
, wszUnable2Access
, sizeof(wszUnable2Access
)/sizeof(WCHAR
));
58 MessageBoxW(hMainWnd
, wstrErrorText
, wszUnable2Access
, MB_OK
|MB_ICONSTOP
);
62 * Enable a checkbox for each processor present in the system
64 if (dwSystemAffinityMask
& 0x00000001)
65 EnableWindow(GetDlgItem(hDlg
, IDC_CPU0
), TRUE
);
66 if (dwSystemAffinityMask
& 0x00000002)
67 EnableWindow(GetDlgItem(hDlg
, IDC_CPU1
), TRUE
);
68 if (dwSystemAffinityMask
& 0x00000004)
69 EnableWindow(GetDlgItem(hDlg
, IDC_CPU2
), TRUE
);
70 if (dwSystemAffinityMask
& 0x00000008)
71 EnableWindow(GetDlgItem(hDlg
, IDC_CPU3
), TRUE
);
72 if (dwSystemAffinityMask
& 0x00000010)
73 EnableWindow(GetDlgItem(hDlg
, IDC_CPU4
), TRUE
);
74 if (dwSystemAffinityMask
& 0x00000020)
75 EnableWindow(GetDlgItem(hDlg
, IDC_CPU5
), TRUE
);
76 if (dwSystemAffinityMask
& 0x00000040)
77 EnableWindow(GetDlgItem(hDlg
, IDC_CPU6
), TRUE
);
78 if (dwSystemAffinityMask
& 0x00000080)
79 EnableWindow(GetDlgItem(hDlg
, IDC_CPU7
), TRUE
);
80 if (dwSystemAffinityMask
& 0x00000100)
81 EnableWindow(GetDlgItem(hDlg
, IDC_CPU8
), TRUE
);
82 if (dwSystemAffinityMask
& 0x00000200)
83 EnableWindow(GetDlgItem(hDlg
, IDC_CPU9
), TRUE
);
84 if (dwSystemAffinityMask
& 0x00000400)
85 EnableWindow(GetDlgItem(hDlg
, IDC_CPU10
), TRUE
);
86 if (dwSystemAffinityMask
& 0x00000800)
87 EnableWindow(GetDlgItem(hDlg
, IDC_CPU11
), TRUE
);
88 if (dwSystemAffinityMask
& 0x00001000)
89 EnableWindow(GetDlgItem(hDlg
, IDC_CPU12
), TRUE
);
90 if (dwSystemAffinityMask
& 0x00002000)
91 EnableWindow(GetDlgItem(hDlg
, IDC_CPU13
), TRUE
);
92 if (dwSystemAffinityMask
& 0x00004000)
93 EnableWindow(GetDlgItem(hDlg
, IDC_CPU14
), TRUE
);
94 if (dwSystemAffinityMask
& 0x00008000)
95 EnableWindow(GetDlgItem(hDlg
, IDC_CPU15
), TRUE
);
96 if (dwSystemAffinityMask
& 0x00010000)
97 EnableWindow(GetDlgItem(hDlg
, IDC_CPU16
), TRUE
);
98 if (dwSystemAffinityMask
& 0x00020000)
99 EnableWindow(GetDlgItem(hDlg
, IDC_CPU17
), TRUE
);
100 if (dwSystemAffinityMask
& 0x00040000)
101 EnableWindow(GetDlgItem(hDlg
, IDC_CPU18
), TRUE
);
102 if (dwSystemAffinityMask
& 0x00080000)
103 EnableWindow(GetDlgItem(hDlg
, IDC_CPU19
), TRUE
);
104 if (dwSystemAffinityMask
& 0x00100000)
105 EnableWindow(GetDlgItem(hDlg
, IDC_CPU20
), TRUE
);
106 if (dwSystemAffinityMask
& 0x00200000)
107 EnableWindow(GetDlgItem(hDlg
, IDC_CPU21
), TRUE
);
108 if (dwSystemAffinityMask
& 0x00400000)
109 EnableWindow(GetDlgItem(hDlg
, IDC_CPU22
), TRUE
);
110 if (dwSystemAffinityMask
& 0x00800000)
111 EnableWindow(GetDlgItem(hDlg
, IDC_CPU23
), TRUE
);
112 if (dwSystemAffinityMask
& 0x01000000)
113 EnableWindow(GetDlgItem(hDlg
, IDC_CPU24
), TRUE
);
114 if (dwSystemAffinityMask
& 0x02000000)
115 EnableWindow(GetDlgItem(hDlg
, IDC_CPU25
), TRUE
);
116 if (dwSystemAffinityMask
& 0x04000000)
117 EnableWindow(GetDlgItem(hDlg
, IDC_CPU26
), TRUE
);
118 if (dwSystemAffinityMask
& 0x08000000)
119 EnableWindow(GetDlgItem(hDlg
, IDC_CPU27
), TRUE
);
120 if (dwSystemAffinityMask
& 0x10000000)
121 EnableWindow(GetDlgItem(hDlg
, IDC_CPU28
), TRUE
);
122 if (dwSystemAffinityMask
& 0x20000000)
123 EnableWindow(GetDlgItem(hDlg
, IDC_CPU29
), TRUE
);
124 if (dwSystemAffinityMask
& 0x40000000)
125 EnableWindow(GetDlgItem(hDlg
, IDC_CPU30
), TRUE
);
126 if (dwSystemAffinityMask
& 0x80000000)
127 EnableWindow(GetDlgItem(hDlg
, IDC_CPU31
), TRUE
);
131 * Check each checkbox that the current process
134 if (dwProcessAffinityMask
& 0x00000001)
135 SendMessageW(GetDlgItem(hDlg
, IDC_CPU0
), BM_SETCHECK
, BST_CHECKED
, 0);
136 if (dwProcessAffinityMask
& 0x00000002)
137 SendMessageW(GetDlgItem(hDlg
, IDC_CPU1
), BM_SETCHECK
, BST_CHECKED
, 0);
138 if (dwProcessAffinityMask
& 0x00000004)
139 SendMessageW(GetDlgItem(hDlg
, IDC_CPU2
), BM_SETCHECK
, BST_CHECKED
, 0);
140 if (dwProcessAffinityMask
& 0x00000008)
141 SendMessageW(GetDlgItem(hDlg
, IDC_CPU3
), BM_SETCHECK
, BST_CHECKED
, 0);
142 if (dwProcessAffinityMask
& 0x00000010)
143 SendMessageW(GetDlgItem(hDlg
, IDC_CPU4
), BM_SETCHECK
, BST_CHECKED
, 0);
144 if (dwProcessAffinityMask
& 0x00000020)
145 SendMessageW(GetDlgItem(hDlg
, IDC_CPU5
), BM_SETCHECK
, BST_CHECKED
, 0);
146 if (dwProcessAffinityMask
& 0x00000040)
147 SendMessageW(GetDlgItem(hDlg
, IDC_CPU6
), BM_SETCHECK
, BST_CHECKED
, 0);
148 if (dwProcessAffinityMask
& 0x00000080)
149 SendMessageW(GetDlgItem(hDlg
, IDC_CPU7
), BM_SETCHECK
, BST_CHECKED
, 0);
150 if (dwProcessAffinityMask
& 0x00000100)
151 SendMessageW(GetDlgItem(hDlg
, IDC_CPU8
), BM_SETCHECK
, BST_CHECKED
, 0);
152 if (dwProcessAffinityMask
& 0x00000200)
153 SendMessageW(GetDlgItem(hDlg
, IDC_CPU9
), BM_SETCHECK
, BST_CHECKED
, 0);
154 if (dwProcessAffinityMask
& 0x00000400)
155 SendMessageW(GetDlgItem(hDlg
, IDC_CPU10
), BM_SETCHECK
, BST_CHECKED
, 0);
156 if (dwProcessAffinityMask
& 0x00000800)
157 SendMessageW(GetDlgItem(hDlg
, IDC_CPU11
), BM_SETCHECK
, BST_CHECKED
, 0);
158 if (dwProcessAffinityMask
& 0x00001000)
159 SendMessageW(GetDlgItem(hDlg
, IDC_CPU12
), BM_SETCHECK
, BST_CHECKED
, 0);
160 if (dwProcessAffinityMask
& 0x00002000)
161 SendMessageW(GetDlgItem(hDlg
, IDC_CPU13
), BM_SETCHECK
, BST_CHECKED
, 0);
162 if (dwProcessAffinityMask
& 0x00004000)
163 SendMessageW(GetDlgItem(hDlg
, IDC_CPU14
), BM_SETCHECK
, BST_CHECKED
, 0);
164 if (dwProcessAffinityMask
& 0x00008000)
165 SendMessageW(GetDlgItem(hDlg
, IDC_CPU15
), BM_SETCHECK
, BST_CHECKED
, 0);
166 if (dwProcessAffinityMask
& 0x00010000)
167 SendMessageW(GetDlgItem(hDlg
, IDC_CPU16
), BM_SETCHECK
, BST_CHECKED
, 0);
168 if (dwProcessAffinityMask
& 0x00020000)
169 SendMessageW(GetDlgItem(hDlg
, IDC_CPU17
), BM_SETCHECK
, BST_CHECKED
, 0);
170 if (dwProcessAffinityMask
& 0x00040000)
171 SendMessageW(GetDlgItem(hDlg
, IDC_CPU18
), BM_SETCHECK
, BST_CHECKED
, 0);
172 if (dwProcessAffinityMask
& 0x00080000)
173 SendMessageW(GetDlgItem(hDlg
, IDC_CPU19
), BM_SETCHECK
, BST_CHECKED
, 0);
174 if (dwProcessAffinityMask
& 0x00100000)
175 SendMessageW(GetDlgItem(hDlg
, IDC_CPU20
), BM_SETCHECK
, BST_CHECKED
, 0);
176 if (dwProcessAffinityMask
& 0x00200000)
177 SendMessageW(GetDlgItem(hDlg
, IDC_CPU21
), BM_SETCHECK
, BST_CHECKED
, 0);
178 if (dwProcessAffinityMask
& 0x00400000)
179 SendMessageW(GetDlgItem(hDlg
, IDC_CPU22
), BM_SETCHECK
, BST_CHECKED
, 0);
180 if (dwProcessAffinityMask
& 0x00800000)
181 SendMessageW(GetDlgItem(hDlg
, IDC_CPU23
), BM_SETCHECK
, BST_CHECKED
, 0);
182 if (dwProcessAffinityMask
& 0x01000000)
183 SendMessageW(GetDlgItem(hDlg
, IDC_CPU24
), BM_SETCHECK
, BST_CHECKED
, 0);
184 if (dwProcessAffinityMask
& 0x02000000)
185 SendMessageW(GetDlgItem(hDlg
, IDC_CPU25
), BM_SETCHECK
, BST_CHECKED
, 0);
186 if (dwProcessAffinityMask
& 0x04000000)
187 SendMessageW(GetDlgItem(hDlg
, IDC_CPU26
), BM_SETCHECK
, BST_CHECKED
, 0);
188 if (dwProcessAffinityMask
& 0x08000000)
189 SendMessageW(GetDlgItem(hDlg
, IDC_CPU27
), BM_SETCHECK
, BST_CHECKED
, 0);
190 if (dwProcessAffinityMask
& 0x10000000)
191 SendMessageW(GetDlgItem(hDlg
, IDC_CPU28
), BM_SETCHECK
, BST_CHECKED
, 0);
192 if (dwProcessAffinityMask
& 0x20000000)
193 SendMessageW(GetDlgItem(hDlg
, IDC_CPU29
), BM_SETCHECK
, BST_CHECKED
, 0);
194 if (dwProcessAffinityMask
& 0x40000000)
195 SendMessageW(GetDlgItem(hDlg
, IDC_CPU30
), BM_SETCHECK
, BST_CHECKED
, 0);
196 if (dwProcessAffinityMask
& 0x80000000)
197 SendMessageW(GetDlgItem(hDlg
, IDC_CPU31
), BM_SETCHECK
, BST_CHECKED
, 0);
204 * If the user has cancelled the dialog box
207 if (LOWORD(wParam
) == IDCANCEL
) {
208 EndDialog(hDlg
, LOWORD(wParam
));
213 * The user has clicked OK -- so now we have
214 * to adjust the process affinity mask
216 if (LOWORD(wParam
) == IDOK
) {
218 * First we have to create a mask out of each
219 * checkbox that the user checked.
221 if (SendMessageW(GetDlgItem(hDlg
, IDC_CPU0
), BM_GETCHECK
, 0, 0))
222 dwProcessAffinityMask
|= 0x00000001;
223 if (SendMessageW(GetDlgItem(hDlg
, IDC_CPU1
), BM_GETCHECK
, 0, 0))
224 dwProcessAffinityMask
|= 0x00000002;
225 if (SendMessageW(GetDlgItem(hDlg
, IDC_CPU2
), BM_GETCHECK
, 0, 0))
226 dwProcessAffinityMask
|= 0x00000004;
227 if (SendMessageW(GetDlgItem(hDlg
, IDC_CPU3
), BM_GETCHECK
, 0, 0))
228 dwProcessAffinityMask
|= 0x00000008;
229 if (SendMessageW(GetDlgItem(hDlg
, IDC_CPU4
), BM_GETCHECK
, 0, 0))
230 dwProcessAffinityMask
|= 0x00000010;
231 if (SendMessageW(GetDlgItem(hDlg
, IDC_CPU5
), BM_GETCHECK
, 0, 0))
232 dwProcessAffinityMask
|= 0x00000020;
233 if (SendMessageW(GetDlgItem(hDlg
, IDC_CPU6
), BM_GETCHECK
, 0, 0))
234 dwProcessAffinityMask
|= 0x00000040;
235 if (SendMessageW(GetDlgItem(hDlg
, IDC_CPU7
), BM_GETCHECK
, 0, 0))
236 dwProcessAffinityMask
|= 0x00000080;
237 if (SendMessageW(GetDlgItem(hDlg
, IDC_CPU8
), BM_GETCHECK
, 0, 0))
238 dwProcessAffinityMask
|= 0x00000100;
239 if (SendMessageW(GetDlgItem(hDlg
, IDC_CPU9
), BM_GETCHECK
, 0, 0))
240 dwProcessAffinityMask
|= 0x00000200;
241 if (SendMessageW(GetDlgItem(hDlg
, IDC_CPU10
), BM_GETCHECK
, 0, 0))
242 dwProcessAffinityMask
|= 0x00000400;
243 if (SendMessageW(GetDlgItem(hDlg
, IDC_CPU11
), BM_GETCHECK
, 0, 0))
244 dwProcessAffinityMask
|= 0x00000800;
245 if (SendMessageW(GetDlgItem(hDlg
, IDC_CPU12
), BM_GETCHECK
, 0, 0))
246 dwProcessAffinityMask
|= 0x00001000;
247 if (SendMessageW(GetDlgItem(hDlg
, IDC_CPU13
), BM_GETCHECK
, 0, 0))
248 dwProcessAffinityMask
|= 0x00002000;
249 if (SendMessageW(GetDlgItem(hDlg
, IDC_CPU14
), BM_GETCHECK
, 0, 0))
250 dwProcessAffinityMask
|= 0x00004000;
251 if (SendMessageW(GetDlgItem(hDlg
, IDC_CPU15
), BM_GETCHECK
, 0, 0))
252 dwProcessAffinityMask
|= 0x00008000;
253 if (SendMessageW(GetDlgItem(hDlg
, IDC_CPU16
), BM_GETCHECK
, 0, 0))
254 dwProcessAffinityMask
|= 0x00010000;
255 if (SendMessageW(GetDlgItem(hDlg
, IDC_CPU17
), BM_GETCHECK
, 0, 0))
256 dwProcessAffinityMask
|= 0x00020000;
257 if (SendMessageW(GetDlgItem(hDlg
, IDC_CPU18
), BM_GETCHECK
, 0, 0))
258 dwProcessAffinityMask
|= 0x00040000;
259 if (SendMessageW(GetDlgItem(hDlg
, IDC_CPU19
), BM_GETCHECK
, 0, 0))
260 dwProcessAffinityMask
|= 0x00080000;
261 if (SendMessageW(GetDlgItem(hDlg
, IDC_CPU20
), BM_GETCHECK
, 0, 0))
262 dwProcessAffinityMask
|= 0x00100000;
263 if (SendMessageW(GetDlgItem(hDlg
, IDC_CPU21
), BM_GETCHECK
, 0, 0))
264 dwProcessAffinityMask
|= 0x00200000;
265 if (SendMessageW(GetDlgItem(hDlg
, IDC_CPU22
), BM_GETCHECK
, 0, 0))
266 dwProcessAffinityMask
|= 0x00400000;
267 if (SendMessageW(GetDlgItem(hDlg
, IDC_CPU23
), BM_GETCHECK
, 0, 0))
268 dwProcessAffinityMask
|= 0x00800000;
269 if (SendMessageW(GetDlgItem(hDlg
, IDC_CPU24
), BM_GETCHECK
, 0, 0))
270 dwProcessAffinityMask
|= 0x01000000;
271 if (SendMessageW(GetDlgItem(hDlg
, IDC_CPU25
), BM_GETCHECK
, 0, 0))
272 dwProcessAffinityMask
|= 0x02000000;
273 if (SendMessageW(GetDlgItem(hDlg
, IDC_CPU26
), BM_GETCHECK
, 0, 0))
274 dwProcessAffinityMask
|= 0x04000000;
275 if (SendMessageW(GetDlgItem(hDlg
, IDC_CPU27
), BM_GETCHECK
, 0, 0))
276 dwProcessAffinityMask
|= 0x08000000;
277 if (SendMessageW(GetDlgItem(hDlg
, IDC_CPU28
), BM_GETCHECK
, 0, 0))
278 dwProcessAffinityMask
|= 0x10000000;
279 if (SendMessageW(GetDlgItem(hDlg
, IDC_CPU29
), BM_GETCHECK
, 0, 0))
280 dwProcessAffinityMask
|= 0x20000000;
281 if (SendMessageW(GetDlgItem(hDlg
, IDC_CPU30
), BM_GETCHECK
, 0, 0))
282 dwProcessAffinityMask
|= 0x40000000;
283 if (SendMessageW(GetDlgItem(hDlg
, IDC_CPU31
), BM_GETCHECK
, 0, 0))
284 dwProcessAffinityMask
|= 0x80000000;
287 * Make sure they are giving the process affinity
288 * with at least one processor. I'd hate to see a
289 * process that is not in a wait state get deprived
292 if (!dwProcessAffinityMask
) {
293 WCHAR wszErrorMsg
[255];
294 WCHAR wszErrorTitle
[255];
295 LoadStringW(hInst
, IDS_AFFINITY_ERROR_MESSAGE
, wszErrorMsg
, sizeof(wszErrorMsg
)/sizeof(WCHAR
));
296 LoadStringW(hInst
, IDS_AFFINITY_ERROR_TITLE
, wszErrorTitle
, sizeof(wszErrorTitle
)/sizeof(WCHAR
));
297 MessageBoxW(hDlg
, wszErrorMsg
, wszErrorTitle
, MB_OK
|MB_ICONSTOP
);
302 * Try to set the process affinity
304 if (!SetProcessAffinityMask(hProcessAffinityHandle
, dwProcessAffinityMask
)) {
305 GetLastErrorText(wstrErrorText
, sizeof(wstrErrorText
)/sizeof(WCHAR
));
306 EndDialog(hDlg
, LOWORD(wParam
));
307 LoadStringW(hInst
, IDS_AFFINITY_UNABLE2ACCESS
, wszUnable2Access
, sizeof(wszUnable2Access
)/sizeof(WCHAR
));
308 MessageBoxW(hMainWnd
, wstrErrorText
, wszUnable2Access
, MB_OK
|MB_ICONSTOP
);
311 EndDialog(hDlg
, LOWORD(wParam
));
321 void ProcessPage_OnSetAffinity(void)
326 WCHAR wstrErrorText
[256];
328 Count
= SendMessageW(hProcessPageListCtrl
, LVM_GETITEMCOUNT
, 0, 0);
329 for (Index
=0; Index
<Count
; Index
++) {
330 memset(&lvitem
, 0, sizeof(LV_ITEMW
));
331 lvitem
.mask
= LVIF_STATE
;
332 lvitem
.stateMask
= LVIS_SELECTED
;
333 lvitem
.iItem
= Index
;
334 SendMessageW(hProcessPageListCtrl
, LVM_GETITEMW
, 0, (LPARAM
) &lvitem
);
335 if (lvitem
.state
& LVIS_SELECTED
)
339 Count
= SendMessageW(hProcessPageListCtrl
, LVM_GETSELECTEDCOUNT
, 0, 0);
340 dwProcessId
= PerfDataGetProcessId(Index
);
341 if ((Count
!= 1) || (dwProcessId
== 0))
343 hProcessAffinityHandle
= OpenProcess(PROCESS_QUERY_INFORMATION
|PROCESS_SET_INFORMATION
, FALSE
, dwProcessId
);
344 if (!hProcessAffinityHandle
) {
345 GetLastErrorText(wstrErrorText
, sizeof(wstrErrorText
)/sizeof(WCHAR
));
346 LoadStringW(hInst
, IDS_AFFINITY_UNABLE2ACCESS
, wszUnable2Access
, sizeof(wszUnable2Access
)/sizeof(WCHAR
));
347 MessageBoxW(hMainWnd
, wstrErrorText
, wszUnable2Access
, MB_OK
|MB_ICONSTOP
);
350 DialogBoxW(hInst
, MAKEINTRESOURCEW(IDD_AFFINITY_DIALOG
), hMainWnd
, AffinityDialogWndProc
);
351 if (hProcessAffinityHandle
) {
352 CloseHandle(hProcessAffinityHandle
);
353 hProcessAffinityHandle
= NULL
;