6 * Copyright (C) 1999 - 2001 Brian Palmer <brianp@reactos.org>
7 * 2005 Klemens Friedl <frik85@reactos.at>
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 HANDLE hProcessAffinityHandle
;
30 INT_PTR CALLBACK
AffinityDialogWndProc(HWND hDlg
, UINT message
, WPARAM wParam
, LPARAM lParam
);
32 void ProcessPage_OnSetAffinity(void)
37 TCHAR strErrorText
[260];
39 for (Index
=0; Index
<(ULONG
)ListView_GetItemCount(hProcessPageListCtrl
); Index
++) {
40 memset(&lvitem
, 0, sizeof(LV_ITEM
));
41 lvitem
.mask
= LVIF_STATE
;
42 lvitem
.stateMask
= LVIS_SELECTED
;
44 ListView_GetItem(hProcessPageListCtrl
, &lvitem
);
45 if (lvitem
.state
& LVIS_SELECTED
)
48 dwProcessId
= PerfDataGetProcessId(Index
);
49 if ((ListView_GetSelectedCount(hProcessPageListCtrl
) != 1) || (dwProcessId
== 0))
51 hProcessAffinityHandle
= OpenProcess(PROCESS_QUERY_INFORMATION
|PROCESS_SET_INFORMATION
, FALSE
, dwProcessId
);
52 if (!hProcessAffinityHandle
) {
53 GetLastErrorText(strErrorText
, 260);
54 LoadString(hInst
, IDS_MSG_ACCESSPROCESSAFF
, szTemp
, 256);
55 MessageBox(hMainWnd
, strErrorText
, szTemp
, MB_OK
|MB_ICONSTOP
);
58 DialogBox(hInst
, MAKEINTRESOURCE(IDD_AFFINITY_DIALOG
), hMainWnd
, AffinityDialogWndProc
);
59 if (hProcessAffinityHandle
) {
60 CloseHandle(hProcessAffinityHandle
);
61 hProcessAffinityHandle
= NULL
;
66 AffinityDialogWndProc(HWND hDlg
, UINT message
, WPARAM wParam
, LPARAM lParam
)
68 DWORD dwProcessAffinityMask
= 0;
69 DWORD dwSystemAffinityMask
= 0;
70 TCHAR strErrorText
[260];
76 * Get the current affinity mask for the process and
77 * the number of CPUs present in the system
79 if (!GetProcessAffinityMask(hProcessAffinityHandle
, &dwProcessAffinityMask
, &dwSystemAffinityMask
)) {
80 GetLastErrorText(strErrorText
, 260);
82 LoadString(hInst
, IDS_MSG_ACCESSPROCESSAFF
, szTemp
, 256);
83 MessageBox(hMainWnd
, strErrorText
, szTemp
, MB_OK
|MB_ICONSTOP
);
87 * Enable a checkbox for each processor present in the system
89 if (dwSystemAffinityMask
& 0x00000001)
90 EnableWindow(GetDlgItem(hDlg
, IDC_CPU0
), TRUE
);
91 if (dwSystemAffinityMask
& 0x00000002)
92 EnableWindow(GetDlgItem(hDlg
, IDC_CPU1
), TRUE
);
93 if (dwSystemAffinityMask
& 0x00000004)
94 EnableWindow(GetDlgItem(hDlg
, IDC_CPU2
), TRUE
);
95 if (dwSystemAffinityMask
& 0x00000008)
96 EnableWindow(GetDlgItem(hDlg
, IDC_CPU3
), TRUE
);
97 if (dwSystemAffinityMask
& 0x00000010)
98 EnableWindow(GetDlgItem(hDlg
, IDC_CPU4
), TRUE
);
99 if (dwSystemAffinityMask
& 0x00000020)
100 EnableWindow(GetDlgItem(hDlg
, IDC_CPU5
), TRUE
);
101 if (dwSystemAffinityMask
& 0x00000040)
102 EnableWindow(GetDlgItem(hDlg
, IDC_CPU6
), TRUE
);
103 if (dwSystemAffinityMask
& 0x00000080)
104 EnableWindow(GetDlgItem(hDlg
, IDC_CPU7
), TRUE
);
105 if (dwSystemAffinityMask
& 0x00000100)
106 EnableWindow(GetDlgItem(hDlg
, IDC_CPU8
), TRUE
);
107 if (dwSystemAffinityMask
& 0x00000200)
108 EnableWindow(GetDlgItem(hDlg
, IDC_CPU9
), TRUE
);
109 if (dwSystemAffinityMask
& 0x00000400)
110 EnableWindow(GetDlgItem(hDlg
, IDC_CPU10
), TRUE
);
111 if (dwSystemAffinityMask
& 0x00000800)
112 EnableWindow(GetDlgItem(hDlg
, IDC_CPU11
), TRUE
);
113 if (dwSystemAffinityMask
& 0x00001000)
114 EnableWindow(GetDlgItem(hDlg
, IDC_CPU12
), TRUE
);
115 if (dwSystemAffinityMask
& 0x00002000)
116 EnableWindow(GetDlgItem(hDlg
, IDC_CPU13
), TRUE
);
117 if (dwSystemAffinityMask
& 0x00004000)
118 EnableWindow(GetDlgItem(hDlg
, IDC_CPU14
), TRUE
);
119 if (dwSystemAffinityMask
& 0x00008000)
120 EnableWindow(GetDlgItem(hDlg
, IDC_CPU15
), TRUE
);
121 if (dwSystemAffinityMask
& 0x00010000)
122 EnableWindow(GetDlgItem(hDlg
, IDC_CPU16
), TRUE
);
123 if (dwSystemAffinityMask
& 0x00020000)
124 EnableWindow(GetDlgItem(hDlg
, IDC_CPU17
), TRUE
);
125 if (dwSystemAffinityMask
& 0x00040000)
126 EnableWindow(GetDlgItem(hDlg
, IDC_CPU18
), TRUE
);
127 if (dwSystemAffinityMask
& 0x00080000)
128 EnableWindow(GetDlgItem(hDlg
, IDC_CPU19
), TRUE
);
129 if (dwSystemAffinityMask
& 0x00100000)
130 EnableWindow(GetDlgItem(hDlg
, IDC_CPU20
), TRUE
);
131 if (dwSystemAffinityMask
& 0x00200000)
132 EnableWindow(GetDlgItem(hDlg
, IDC_CPU21
), TRUE
);
133 if (dwSystemAffinityMask
& 0x00400000)
134 EnableWindow(GetDlgItem(hDlg
, IDC_CPU22
), TRUE
);
135 if (dwSystemAffinityMask
& 0x00800000)
136 EnableWindow(GetDlgItem(hDlg
, IDC_CPU23
), TRUE
);
137 if (dwSystemAffinityMask
& 0x01000000)
138 EnableWindow(GetDlgItem(hDlg
, IDC_CPU24
), TRUE
);
139 if (dwSystemAffinityMask
& 0x02000000)
140 EnableWindow(GetDlgItem(hDlg
, IDC_CPU25
), TRUE
);
141 if (dwSystemAffinityMask
& 0x04000000)
142 EnableWindow(GetDlgItem(hDlg
, IDC_CPU26
), TRUE
);
143 if (dwSystemAffinityMask
& 0x08000000)
144 EnableWindow(GetDlgItem(hDlg
, IDC_CPU27
), TRUE
);
145 if (dwSystemAffinityMask
& 0x10000000)
146 EnableWindow(GetDlgItem(hDlg
, IDC_CPU28
), TRUE
);
147 if (dwSystemAffinityMask
& 0x20000000)
148 EnableWindow(GetDlgItem(hDlg
, IDC_CPU29
), TRUE
);
149 if (dwSystemAffinityMask
& 0x40000000)
150 EnableWindow(GetDlgItem(hDlg
, IDC_CPU30
), TRUE
);
151 if (dwSystemAffinityMask
& 0x80000000)
152 EnableWindow(GetDlgItem(hDlg
, IDC_CPU31
), TRUE
);
156 * Check each checkbox that the current process
159 if (dwProcessAffinityMask
& 0x00000001)
160 SendMessage(GetDlgItem(hDlg
, IDC_CPU0
), BM_SETCHECK
, BST_CHECKED
, 0);
161 if (dwProcessAffinityMask
& 0x00000002)
162 SendMessage(GetDlgItem(hDlg
, IDC_CPU1
), BM_SETCHECK
, BST_CHECKED
, 0);
163 if (dwProcessAffinityMask
& 0x00000004)
164 SendMessage(GetDlgItem(hDlg
, IDC_CPU2
), BM_SETCHECK
, BST_CHECKED
, 0);
165 if (dwProcessAffinityMask
& 0x00000008)
166 SendMessage(GetDlgItem(hDlg
, IDC_CPU3
), BM_SETCHECK
, BST_CHECKED
, 0);
167 if (dwProcessAffinityMask
& 0x00000010)
168 SendMessage(GetDlgItem(hDlg
, IDC_CPU4
), BM_SETCHECK
, BST_CHECKED
, 0);
169 if (dwProcessAffinityMask
& 0x00000020)
170 SendMessage(GetDlgItem(hDlg
, IDC_CPU5
), BM_SETCHECK
, BST_CHECKED
, 0);
171 if (dwProcessAffinityMask
& 0x00000040)
172 SendMessage(GetDlgItem(hDlg
, IDC_CPU6
), BM_SETCHECK
, BST_CHECKED
, 0);
173 if (dwProcessAffinityMask
& 0x00000080)
174 SendMessage(GetDlgItem(hDlg
, IDC_CPU7
), BM_SETCHECK
, BST_CHECKED
, 0);
175 if (dwProcessAffinityMask
& 0x00000100)
176 SendMessage(GetDlgItem(hDlg
, IDC_CPU8
), BM_SETCHECK
, BST_CHECKED
, 0);
177 if (dwProcessAffinityMask
& 0x00000200)
178 SendMessage(GetDlgItem(hDlg
, IDC_CPU9
), BM_SETCHECK
, BST_CHECKED
, 0);
179 if (dwProcessAffinityMask
& 0x00000400)
180 SendMessage(GetDlgItem(hDlg
, IDC_CPU10
), BM_SETCHECK
, BST_CHECKED
, 0);
181 if (dwProcessAffinityMask
& 0x00000800)
182 SendMessage(GetDlgItem(hDlg
, IDC_CPU11
), BM_SETCHECK
, BST_CHECKED
, 0);
183 if (dwProcessAffinityMask
& 0x00001000)
184 SendMessage(GetDlgItem(hDlg
, IDC_CPU12
), BM_SETCHECK
, BST_CHECKED
, 0);
185 if (dwProcessAffinityMask
& 0x00002000)
186 SendMessage(GetDlgItem(hDlg
, IDC_CPU13
), BM_SETCHECK
, BST_CHECKED
, 0);
187 if (dwProcessAffinityMask
& 0x00004000)
188 SendMessage(GetDlgItem(hDlg
, IDC_CPU14
), BM_SETCHECK
, BST_CHECKED
, 0);
189 if (dwProcessAffinityMask
& 0x00008000)
190 SendMessage(GetDlgItem(hDlg
, IDC_CPU15
), BM_SETCHECK
, BST_CHECKED
, 0);
191 if (dwProcessAffinityMask
& 0x00010000)
192 SendMessage(GetDlgItem(hDlg
, IDC_CPU16
), BM_SETCHECK
, BST_CHECKED
, 0);
193 if (dwProcessAffinityMask
& 0x00020000)
194 SendMessage(GetDlgItem(hDlg
, IDC_CPU17
), BM_SETCHECK
, BST_CHECKED
, 0);
195 if (dwProcessAffinityMask
& 0x00040000)
196 SendMessage(GetDlgItem(hDlg
, IDC_CPU18
), BM_SETCHECK
, BST_CHECKED
, 0);
197 if (dwProcessAffinityMask
& 0x00080000)
198 SendMessage(GetDlgItem(hDlg
, IDC_CPU19
), BM_SETCHECK
, BST_CHECKED
, 0);
199 if (dwProcessAffinityMask
& 0x00100000)
200 SendMessage(GetDlgItem(hDlg
, IDC_CPU20
), BM_SETCHECK
, BST_CHECKED
, 0);
201 if (dwProcessAffinityMask
& 0x00200000)
202 SendMessage(GetDlgItem(hDlg
, IDC_CPU21
), BM_SETCHECK
, BST_CHECKED
, 0);
203 if (dwProcessAffinityMask
& 0x00400000)
204 SendMessage(GetDlgItem(hDlg
, IDC_CPU22
), BM_SETCHECK
, BST_CHECKED
, 0);
205 if (dwProcessAffinityMask
& 0x00800000)
206 SendMessage(GetDlgItem(hDlg
, IDC_CPU23
), BM_SETCHECK
, BST_CHECKED
, 0);
207 if (dwProcessAffinityMask
& 0x01000000)
208 SendMessage(GetDlgItem(hDlg
, IDC_CPU24
), BM_SETCHECK
, BST_CHECKED
, 0);
209 if (dwProcessAffinityMask
& 0x02000000)
210 SendMessage(GetDlgItem(hDlg
, IDC_CPU25
), BM_SETCHECK
, BST_CHECKED
, 0);
211 if (dwProcessAffinityMask
& 0x04000000)
212 SendMessage(GetDlgItem(hDlg
, IDC_CPU26
), BM_SETCHECK
, BST_CHECKED
, 0);
213 if (dwProcessAffinityMask
& 0x08000000)
214 SendMessage(GetDlgItem(hDlg
, IDC_CPU27
), BM_SETCHECK
, BST_CHECKED
, 0);
215 if (dwProcessAffinityMask
& 0x10000000)
216 SendMessage(GetDlgItem(hDlg
, IDC_CPU28
), BM_SETCHECK
, BST_CHECKED
, 0);
217 if (dwProcessAffinityMask
& 0x20000000)
218 SendMessage(GetDlgItem(hDlg
, IDC_CPU29
), BM_SETCHECK
, BST_CHECKED
, 0);
219 if (dwProcessAffinityMask
& 0x40000000)
220 SendMessage(GetDlgItem(hDlg
, IDC_CPU30
), BM_SETCHECK
, BST_CHECKED
, 0);
221 if (dwProcessAffinityMask
& 0x80000000)
222 SendMessage(GetDlgItem(hDlg
, IDC_CPU31
), BM_SETCHECK
, BST_CHECKED
, 0);
229 * If the user has cancelled the dialog box
232 if (LOWORD(wParam
) == IDCANCEL
) {
233 EndDialog(hDlg
, LOWORD(wParam
));
238 * The user has clicked OK -- so now we have
239 * to adjust the process affinity mask
241 if (LOWORD(wParam
) == IDOK
) {
243 * First we have to create a mask out of each
244 * checkbox that the user checked.
246 if (SendMessage(GetDlgItem(hDlg
, IDC_CPU0
), BM_GETCHECK
, 0, 0))
247 dwProcessAffinityMask
|= 0x00000001;
248 if (SendMessage(GetDlgItem(hDlg
, IDC_CPU1
), BM_GETCHECK
, 0, 0))
249 dwProcessAffinityMask
|= 0x00000002;
250 if (SendMessage(GetDlgItem(hDlg
, IDC_CPU2
), BM_GETCHECK
, 0, 0))
251 dwProcessAffinityMask
|= 0x00000004;
252 if (SendMessage(GetDlgItem(hDlg
, IDC_CPU3
), BM_GETCHECK
, 0, 0))
253 dwProcessAffinityMask
|= 0x00000008;
254 if (SendMessage(GetDlgItem(hDlg
, IDC_CPU4
), BM_GETCHECK
, 0, 0))
255 dwProcessAffinityMask
|= 0x00000010;
256 if (SendMessage(GetDlgItem(hDlg
, IDC_CPU5
), BM_GETCHECK
, 0, 0))
257 dwProcessAffinityMask
|= 0x00000020;
258 if (SendMessage(GetDlgItem(hDlg
, IDC_CPU6
), BM_GETCHECK
, 0, 0))
259 dwProcessAffinityMask
|= 0x00000040;
260 if (SendMessage(GetDlgItem(hDlg
, IDC_CPU7
), BM_GETCHECK
, 0, 0))
261 dwProcessAffinityMask
|= 0x00000080;
262 if (SendMessage(GetDlgItem(hDlg
, IDC_CPU8
), BM_GETCHECK
, 0, 0))
263 dwProcessAffinityMask
|= 0x00000100;
264 if (SendMessage(GetDlgItem(hDlg
, IDC_CPU9
), BM_GETCHECK
, 0, 0))
265 dwProcessAffinityMask
|= 0x00000200;
266 if (SendMessage(GetDlgItem(hDlg
, IDC_CPU10
), BM_GETCHECK
, 0, 0))
267 dwProcessAffinityMask
|= 0x00000400;
268 if (SendMessage(GetDlgItem(hDlg
, IDC_CPU11
), BM_GETCHECK
, 0, 0))
269 dwProcessAffinityMask
|= 0x00000800;
270 if (SendMessage(GetDlgItem(hDlg
, IDC_CPU12
), BM_GETCHECK
, 0, 0))
271 dwProcessAffinityMask
|= 0x00001000;
272 if (SendMessage(GetDlgItem(hDlg
, IDC_CPU13
), BM_GETCHECK
, 0, 0))
273 dwProcessAffinityMask
|= 0x00002000;
274 if (SendMessage(GetDlgItem(hDlg
, IDC_CPU14
), BM_GETCHECK
, 0, 0))
275 dwProcessAffinityMask
|= 0x00004000;
276 if (SendMessage(GetDlgItem(hDlg
, IDC_CPU15
), BM_GETCHECK
, 0, 0))
277 dwProcessAffinityMask
|= 0x00008000;
278 if (SendMessage(GetDlgItem(hDlg
, IDC_CPU16
), BM_GETCHECK
, 0, 0))
279 dwProcessAffinityMask
|= 0x00010000;
280 if (SendMessage(GetDlgItem(hDlg
, IDC_CPU17
), BM_GETCHECK
, 0, 0))
281 dwProcessAffinityMask
|= 0x00020000;
282 if (SendMessage(GetDlgItem(hDlg
, IDC_CPU18
), BM_GETCHECK
, 0, 0))
283 dwProcessAffinityMask
|= 0x00040000;
284 if (SendMessage(GetDlgItem(hDlg
, IDC_CPU19
), BM_GETCHECK
, 0, 0))
285 dwProcessAffinityMask
|= 0x00080000;
286 if (SendMessage(GetDlgItem(hDlg
, IDC_CPU20
), BM_GETCHECK
, 0, 0))
287 dwProcessAffinityMask
|= 0x00100000;
288 if (SendMessage(GetDlgItem(hDlg
, IDC_CPU21
), BM_GETCHECK
, 0, 0))
289 dwProcessAffinityMask
|= 0x00200000;
290 if (SendMessage(GetDlgItem(hDlg
, IDC_CPU22
), BM_GETCHECK
, 0, 0))
291 dwProcessAffinityMask
|= 0x00400000;
292 if (SendMessage(GetDlgItem(hDlg
, IDC_CPU23
), BM_GETCHECK
, 0, 0))
293 dwProcessAffinityMask
|= 0x00800000;
294 if (SendMessage(GetDlgItem(hDlg
, IDC_CPU24
), BM_GETCHECK
, 0, 0))
295 dwProcessAffinityMask
|= 0x01000000;
296 if (SendMessage(GetDlgItem(hDlg
, IDC_CPU25
), BM_GETCHECK
, 0, 0))
297 dwProcessAffinityMask
|= 0x02000000;
298 if (SendMessage(GetDlgItem(hDlg
, IDC_CPU26
), BM_GETCHECK
, 0, 0))
299 dwProcessAffinityMask
|= 0x04000000;
300 if (SendMessage(GetDlgItem(hDlg
, IDC_CPU27
), BM_GETCHECK
, 0, 0))
301 dwProcessAffinityMask
|= 0x08000000;
302 if (SendMessage(GetDlgItem(hDlg
, IDC_CPU28
), BM_GETCHECK
, 0, 0))
303 dwProcessAffinityMask
|= 0x10000000;
304 if (SendMessage(GetDlgItem(hDlg
, IDC_CPU29
), BM_GETCHECK
, 0, 0))
305 dwProcessAffinityMask
|= 0x20000000;
306 if (SendMessage(GetDlgItem(hDlg
, IDC_CPU30
), BM_GETCHECK
, 0, 0))
307 dwProcessAffinityMask
|= 0x40000000;
308 if (SendMessage(GetDlgItem(hDlg
, IDC_CPU31
), BM_GETCHECK
, 0, 0))
309 dwProcessAffinityMask
|= 0x80000000;
312 * Make sure they are giving the process affinity
313 * with at least one processor. I'd hate to see a
314 * process that is not in a wait state get deprived
317 if (!dwProcessAffinityMask
) {
318 LoadString(hInst
, IDS_MSG_PROCESSONEPRO
, szTemp
, 256);
319 LoadString(hInst
, IDS_MSG_INVALIDOPTION
, szTempA
, 256);
320 MessageBox(hDlg
, szTemp
, szTempA
, MB_OK
|MB_ICONSTOP
);
325 * Try to set the process affinity
327 if (!SetProcessAffinityMask(hProcessAffinityHandle
, dwProcessAffinityMask
)) {
328 GetLastErrorText(strErrorText
, 260);
329 EndDialog(hDlg
, LOWORD(wParam
));
330 LoadString(hInst
, IDS_MSG_ACCESSPROCESSAFF
, szTemp
, 256);
331 MessageBox(hMainWnd
, strErrorText
, szTemp
, MB_OK
|MB_ICONSTOP
);
334 EndDialog(hDlg
, LOWORD(wParam
));