6 * Copyright (C) 1999 - 2001 Brian Palmer <brianp@reactos.org>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library 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 GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #define WIN32_LEAN_AND_MEAN /* Exclude rarely-used stuff from Windows headers */
36 HANDLE hProcessAffinityHandle
;
38 static INT_PTR CALLBACK
39 AffinityDialogWndProc(HWND hDlg
, UINT message
, WPARAM wParam
, LPARAM lParam
)
41 DWORD_PTR dwProcessAffinityMask
= 0;
42 DWORD_PTR dwSystemAffinityMask
= 0;
43 TCHAR strErrorText
[260];
49 * Get the current affinity mask for the process and
50 * the number of CPUs present in the system
52 if (!GetProcessAffinityMask(hProcessAffinityHandle
, &dwProcessAffinityMask
, &dwSystemAffinityMask
)) {
53 GetLastErrorText(strErrorText
, 260);
55 MessageBox(hMainWnd
, strErrorText
, _T("Unable to Access or Set Process Affinity"), MB_OK
|MB_ICONSTOP
);
59 * Enable a checkbox for each processor present in the system
61 if (dwSystemAffinityMask
& 0x00000001)
62 EnableWindow(GetDlgItem(hDlg
, IDC_CPU0
), TRUE
);
63 if (dwSystemAffinityMask
& 0x00000002)
64 EnableWindow(GetDlgItem(hDlg
, IDC_CPU1
), TRUE
);
65 if (dwSystemAffinityMask
& 0x00000004)
66 EnableWindow(GetDlgItem(hDlg
, IDC_CPU2
), TRUE
);
67 if (dwSystemAffinityMask
& 0x00000008)
68 EnableWindow(GetDlgItem(hDlg
, IDC_CPU3
), TRUE
);
69 if (dwSystemAffinityMask
& 0x00000010)
70 EnableWindow(GetDlgItem(hDlg
, IDC_CPU4
), TRUE
);
71 if (dwSystemAffinityMask
& 0x00000020)
72 EnableWindow(GetDlgItem(hDlg
, IDC_CPU5
), TRUE
);
73 if (dwSystemAffinityMask
& 0x00000040)
74 EnableWindow(GetDlgItem(hDlg
, IDC_CPU6
), TRUE
);
75 if (dwSystemAffinityMask
& 0x00000080)
76 EnableWindow(GetDlgItem(hDlg
, IDC_CPU7
), TRUE
);
77 if (dwSystemAffinityMask
& 0x00000100)
78 EnableWindow(GetDlgItem(hDlg
, IDC_CPU8
), TRUE
);
79 if (dwSystemAffinityMask
& 0x00000200)
80 EnableWindow(GetDlgItem(hDlg
, IDC_CPU9
), TRUE
);
81 if (dwSystemAffinityMask
& 0x00000400)
82 EnableWindow(GetDlgItem(hDlg
, IDC_CPU10
), TRUE
);
83 if (dwSystemAffinityMask
& 0x00000800)
84 EnableWindow(GetDlgItem(hDlg
, IDC_CPU11
), TRUE
);
85 if (dwSystemAffinityMask
& 0x00001000)
86 EnableWindow(GetDlgItem(hDlg
, IDC_CPU12
), TRUE
);
87 if (dwSystemAffinityMask
& 0x00002000)
88 EnableWindow(GetDlgItem(hDlg
, IDC_CPU13
), TRUE
);
89 if (dwSystemAffinityMask
& 0x00004000)
90 EnableWindow(GetDlgItem(hDlg
, IDC_CPU14
), TRUE
);
91 if (dwSystemAffinityMask
& 0x00008000)
92 EnableWindow(GetDlgItem(hDlg
, IDC_CPU15
), TRUE
);
93 if (dwSystemAffinityMask
& 0x00010000)
94 EnableWindow(GetDlgItem(hDlg
, IDC_CPU16
), TRUE
);
95 if (dwSystemAffinityMask
& 0x00020000)
96 EnableWindow(GetDlgItem(hDlg
, IDC_CPU17
), TRUE
);
97 if (dwSystemAffinityMask
& 0x00040000)
98 EnableWindow(GetDlgItem(hDlg
, IDC_CPU18
), TRUE
);
99 if (dwSystemAffinityMask
& 0x00080000)
100 EnableWindow(GetDlgItem(hDlg
, IDC_CPU19
), TRUE
);
101 if (dwSystemAffinityMask
& 0x00100000)
102 EnableWindow(GetDlgItem(hDlg
, IDC_CPU20
), TRUE
);
103 if (dwSystemAffinityMask
& 0x00200000)
104 EnableWindow(GetDlgItem(hDlg
, IDC_CPU21
), TRUE
);
105 if (dwSystemAffinityMask
& 0x00400000)
106 EnableWindow(GetDlgItem(hDlg
, IDC_CPU22
), TRUE
);
107 if (dwSystemAffinityMask
& 0x00800000)
108 EnableWindow(GetDlgItem(hDlg
, IDC_CPU23
), TRUE
);
109 if (dwSystemAffinityMask
& 0x01000000)
110 EnableWindow(GetDlgItem(hDlg
, IDC_CPU24
), TRUE
);
111 if (dwSystemAffinityMask
& 0x02000000)
112 EnableWindow(GetDlgItem(hDlg
, IDC_CPU25
), TRUE
);
113 if (dwSystemAffinityMask
& 0x04000000)
114 EnableWindow(GetDlgItem(hDlg
, IDC_CPU26
), TRUE
);
115 if (dwSystemAffinityMask
& 0x08000000)
116 EnableWindow(GetDlgItem(hDlg
, IDC_CPU27
), TRUE
);
117 if (dwSystemAffinityMask
& 0x10000000)
118 EnableWindow(GetDlgItem(hDlg
, IDC_CPU28
), TRUE
);
119 if (dwSystemAffinityMask
& 0x20000000)
120 EnableWindow(GetDlgItem(hDlg
, IDC_CPU29
), TRUE
);
121 if (dwSystemAffinityMask
& 0x40000000)
122 EnableWindow(GetDlgItem(hDlg
, IDC_CPU30
), TRUE
);
123 if (dwSystemAffinityMask
& 0x80000000)
124 EnableWindow(GetDlgItem(hDlg
, IDC_CPU31
), TRUE
);
128 * Check each checkbox that the current process
131 if (dwProcessAffinityMask
& 0x00000001)
132 SendMessage(GetDlgItem(hDlg
, IDC_CPU0
), BM_SETCHECK
, BST_CHECKED
, 0);
133 if (dwProcessAffinityMask
& 0x00000002)
134 SendMessage(GetDlgItem(hDlg
, IDC_CPU1
), BM_SETCHECK
, BST_CHECKED
, 0);
135 if (dwProcessAffinityMask
& 0x00000004)
136 SendMessage(GetDlgItem(hDlg
, IDC_CPU2
), BM_SETCHECK
, BST_CHECKED
, 0);
137 if (dwProcessAffinityMask
& 0x00000008)
138 SendMessage(GetDlgItem(hDlg
, IDC_CPU3
), BM_SETCHECK
, BST_CHECKED
, 0);
139 if (dwProcessAffinityMask
& 0x00000010)
140 SendMessage(GetDlgItem(hDlg
, IDC_CPU4
), BM_SETCHECK
, BST_CHECKED
, 0);
141 if (dwProcessAffinityMask
& 0x00000020)
142 SendMessage(GetDlgItem(hDlg
, IDC_CPU5
), BM_SETCHECK
, BST_CHECKED
, 0);
143 if (dwProcessAffinityMask
& 0x00000040)
144 SendMessage(GetDlgItem(hDlg
, IDC_CPU6
), BM_SETCHECK
, BST_CHECKED
, 0);
145 if (dwProcessAffinityMask
& 0x00000080)
146 SendMessage(GetDlgItem(hDlg
, IDC_CPU7
), BM_SETCHECK
, BST_CHECKED
, 0);
147 if (dwProcessAffinityMask
& 0x00000100)
148 SendMessage(GetDlgItem(hDlg
, IDC_CPU8
), BM_SETCHECK
, BST_CHECKED
, 0);
149 if (dwProcessAffinityMask
& 0x00000200)
150 SendMessage(GetDlgItem(hDlg
, IDC_CPU9
), BM_SETCHECK
, BST_CHECKED
, 0);
151 if (dwProcessAffinityMask
& 0x00000400)
152 SendMessage(GetDlgItem(hDlg
, IDC_CPU10
), BM_SETCHECK
, BST_CHECKED
, 0);
153 if (dwProcessAffinityMask
& 0x00000800)
154 SendMessage(GetDlgItem(hDlg
, IDC_CPU11
), BM_SETCHECK
, BST_CHECKED
, 0);
155 if (dwProcessAffinityMask
& 0x00001000)
156 SendMessage(GetDlgItem(hDlg
, IDC_CPU12
), BM_SETCHECK
, BST_CHECKED
, 0);
157 if (dwProcessAffinityMask
& 0x00002000)
158 SendMessage(GetDlgItem(hDlg
, IDC_CPU13
), BM_SETCHECK
, BST_CHECKED
, 0);
159 if (dwProcessAffinityMask
& 0x00004000)
160 SendMessage(GetDlgItem(hDlg
, IDC_CPU14
), BM_SETCHECK
, BST_CHECKED
, 0);
161 if (dwProcessAffinityMask
& 0x00008000)
162 SendMessage(GetDlgItem(hDlg
, IDC_CPU15
), BM_SETCHECK
, BST_CHECKED
, 0);
163 if (dwProcessAffinityMask
& 0x00010000)
164 SendMessage(GetDlgItem(hDlg
, IDC_CPU16
), BM_SETCHECK
, BST_CHECKED
, 0);
165 if (dwProcessAffinityMask
& 0x00020000)
166 SendMessage(GetDlgItem(hDlg
, IDC_CPU17
), BM_SETCHECK
, BST_CHECKED
, 0);
167 if (dwProcessAffinityMask
& 0x00040000)
168 SendMessage(GetDlgItem(hDlg
, IDC_CPU18
), BM_SETCHECK
, BST_CHECKED
, 0);
169 if (dwProcessAffinityMask
& 0x00080000)
170 SendMessage(GetDlgItem(hDlg
, IDC_CPU19
), BM_SETCHECK
, BST_CHECKED
, 0);
171 if (dwProcessAffinityMask
& 0x00100000)
172 SendMessage(GetDlgItem(hDlg
, IDC_CPU20
), BM_SETCHECK
, BST_CHECKED
, 0);
173 if (dwProcessAffinityMask
& 0x00200000)
174 SendMessage(GetDlgItem(hDlg
, IDC_CPU21
), BM_SETCHECK
, BST_CHECKED
, 0);
175 if (dwProcessAffinityMask
& 0x00400000)
176 SendMessage(GetDlgItem(hDlg
, IDC_CPU22
), BM_SETCHECK
, BST_CHECKED
, 0);
177 if (dwProcessAffinityMask
& 0x00800000)
178 SendMessage(GetDlgItem(hDlg
, IDC_CPU23
), BM_SETCHECK
, BST_CHECKED
, 0);
179 if (dwProcessAffinityMask
& 0x01000000)
180 SendMessage(GetDlgItem(hDlg
, IDC_CPU24
), BM_SETCHECK
, BST_CHECKED
, 0);
181 if (dwProcessAffinityMask
& 0x02000000)
182 SendMessage(GetDlgItem(hDlg
, IDC_CPU25
), BM_SETCHECK
, BST_CHECKED
, 0);
183 if (dwProcessAffinityMask
& 0x04000000)
184 SendMessage(GetDlgItem(hDlg
, IDC_CPU26
), BM_SETCHECK
, BST_CHECKED
, 0);
185 if (dwProcessAffinityMask
& 0x08000000)
186 SendMessage(GetDlgItem(hDlg
, IDC_CPU27
), BM_SETCHECK
, BST_CHECKED
, 0);
187 if (dwProcessAffinityMask
& 0x10000000)
188 SendMessage(GetDlgItem(hDlg
, IDC_CPU28
), BM_SETCHECK
, BST_CHECKED
, 0);
189 if (dwProcessAffinityMask
& 0x20000000)
190 SendMessage(GetDlgItem(hDlg
, IDC_CPU29
), BM_SETCHECK
, BST_CHECKED
, 0);
191 if (dwProcessAffinityMask
& 0x40000000)
192 SendMessage(GetDlgItem(hDlg
, IDC_CPU30
), BM_SETCHECK
, BST_CHECKED
, 0);
193 if (dwProcessAffinityMask
& 0x80000000)
194 SendMessage(GetDlgItem(hDlg
, IDC_CPU31
), BM_SETCHECK
, BST_CHECKED
, 0);
201 * If the user has cancelled the dialog box
204 if (LOWORD(wParam
) == IDCANCEL
) {
205 EndDialog(hDlg
, LOWORD(wParam
));
210 * The user has clicked OK -- so now we have
211 * to adjust the process affinity mask
213 if (LOWORD(wParam
) == IDOK
) {
215 * First we have to create a mask out of each
216 * checkbox that the user checked.
218 if (SendMessage(GetDlgItem(hDlg
, IDC_CPU0
), BM_GETCHECK
, 0, 0))
219 dwProcessAffinityMask
|= 0x00000001;
220 if (SendMessage(GetDlgItem(hDlg
, IDC_CPU1
), BM_GETCHECK
, 0, 0))
221 dwProcessAffinityMask
|= 0x00000002;
222 if (SendMessage(GetDlgItem(hDlg
, IDC_CPU2
), BM_GETCHECK
, 0, 0))
223 dwProcessAffinityMask
|= 0x00000004;
224 if (SendMessage(GetDlgItem(hDlg
, IDC_CPU3
), BM_GETCHECK
, 0, 0))
225 dwProcessAffinityMask
|= 0x00000008;
226 if (SendMessage(GetDlgItem(hDlg
, IDC_CPU4
), BM_GETCHECK
, 0, 0))
227 dwProcessAffinityMask
|= 0x00000010;
228 if (SendMessage(GetDlgItem(hDlg
, IDC_CPU5
), BM_GETCHECK
, 0, 0))
229 dwProcessAffinityMask
|= 0x00000020;
230 if (SendMessage(GetDlgItem(hDlg
, IDC_CPU6
), BM_GETCHECK
, 0, 0))
231 dwProcessAffinityMask
|= 0x00000040;
232 if (SendMessage(GetDlgItem(hDlg
, IDC_CPU7
), BM_GETCHECK
, 0, 0))
233 dwProcessAffinityMask
|= 0x00000080;
234 if (SendMessage(GetDlgItem(hDlg
, IDC_CPU8
), BM_GETCHECK
, 0, 0))
235 dwProcessAffinityMask
|= 0x00000100;
236 if (SendMessage(GetDlgItem(hDlg
, IDC_CPU9
), BM_GETCHECK
, 0, 0))
237 dwProcessAffinityMask
|= 0x00000200;
238 if (SendMessage(GetDlgItem(hDlg
, IDC_CPU10
), BM_GETCHECK
, 0, 0))
239 dwProcessAffinityMask
|= 0x00000400;
240 if (SendMessage(GetDlgItem(hDlg
, IDC_CPU11
), BM_GETCHECK
, 0, 0))
241 dwProcessAffinityMask
|= 0x00000800;
242 if (SendMessage(GetDlgItem(hDlg
, IDC_CPU12
), BM_GETCHECK
, 0, 0))
243 dwProcessAffinityMask
|= 0x00001000;
244 if (SendMessage(GetDlgItem(hDlg
, IDC_CPU13
), BM_GETCHECK
, 0, 0))
245 dwProcessAffinityMask
|= 0x00002000;
246 if (SendMessage(GetDlgItem(hDlg
, IDC_CPU14
), BM_GETCHECK
, 0, 0))
247 dwProcessAffinityMask
|= 0x00004000;
248 if (SendMessage(GetDlgItem(hDlg
, IDC_CPU15
), BM_GETCHECK
, 0, 0))
249 dwProcessAffinityMask
|= 0x00008000;
250 if (SendMessage(GetDlgItem(hDlg
, IDC_CPU16
), BM_GETCHECK
, 0, 0))
251 dwProcessAffinityMask
|= 0x00010000;
252 if (SendMessage(GetDlgItem(hDlg
, IDC_CPU17
), BM_GETCHECK
, 0, 0))
253 dwProcessAffinityMask
|= 0x00020000;
254 if (SendMessage(GetDlgItem(hDlg
, IDC_CPU18
), BM_GETCHECK
, 0, 0))
255 dwProcessAffinityMask
|= 0x00040000;
256 if (SendMessage(GetDlgItem(hDlg
, IDC_CPU19
), BM_GETCHECK
, 0, 0))
257 dwProcessAffinityMask
|= 0x00080000;
258 if (SendMessage(GetDlgItem(hDlg
, IDC_CPU20
), BM_GETCHECK
, 0, 0))
259 dwProcessAffinityMask
|= 0x00100000;
260 if (SendMessage(GetDlgItem(hDlg
, IDC_CPU21
), BM_GETCHECK
, 0, 0))
261 dwProcessAffinityMask
|= 0x00200000;
262 if (SendMessage(GetDlgItem(hDlg
, IDC_CPU22
), BM_GETCHECK
, 0, 0))
263 dwProcessAffinityMask
|= 0x00400000;
264 if (SendMessage(GetDlgItem(hDlg
, IDC_CPU23
), BM_GETCHECK
, 0, 0))
265 dwProcessAffinityMask
|= 0x00800000;
266 if (SendMessage(GetDlgItem(hDlg
, IDC_CPU24
), BM_GETCHECK
, 0, 0))
267 dwProcessAffinityMask
|= 0x01000000;
268 if (SendMessage(GetDlgItem(hDlg
, IDC_CPU25
), BM_GETCHECK
, 0, 0))
269 dwProcessAffinityMask
|= 0x02000000;
270 if (SendMessage(GetDlgItem(hDlg
, IDC_CPU26
), BM_GETCHECK
, 0, 0))
271 dwProcessAffinityMask
|= 0x04000000;
272 if (SendMessage(GetDlgItem(hDlg
, IDC_CPU27
), BM_GETCHECK
, 0, 0))
273 dwProcessAffinityMask
|= 0x08000000;
274 if (SendMessage(GetDlgItem(hDlg
, IDC_CPU28
), BM_GETCHECK
, 0, 0))
275 dwProcessAffinityMask
|= 0x10000000;
276 if (SendMessage(GetDlgItem(hDlg
, IDC_CPU29
), BM_GETCHECK
, 0, 0))
277 dwProcessAffinityMask
|= 0x20000000;
278 if (SendMessage(GetDlgItem(hDlg
, IDC_CPU30
), BM_GETCHECK
, 0, 0))
279 dwProcessAffinityMask
|= 0x40000000;
280 if (SendMessage(GetDlgItem(hDlg
, IDC_CPU31
), BM_GETCHECK
, 0, 0))
281 dwProcessAffinityMask
|= 0x80000000;
284 * Make sure they are giving the process affinity
285 * with at least one processor. I'd hate to see a
286 * process that is not in a wait state get deprived
289 if (!dwProcessAffinityMask
) {
290 MessageBox(hDlg
, _T("The process must have affinity with at least one processor."), _T("Invalid Option"), MB_OK
|MB_ICONSTOP
);
295 * Try to set the process affinity
297 if (!SetProcessAffinityMask(hProcessAffinityHandle
, dwProcessAffinityMask
)) {
298 GetLastErrorText(strErrorText
, 260);
299 EndDialog(hDlg
, LOWORD(wParam
));
300 MessageBox(hMainWnd
, strErrorText
, _T("Unable to Access or Set Process Affinity"), MB_OK
|MB_ICONSTOP
);
303 EndDialog(hDlg
, LOWORD(wParam
));
313 void ProcessPage_OnSetAffinity(void)
318 TCHAR strErrorText
[260];
320 for (Index
=0; Index
<(ULONG
)ListView_GetItemCount(hProcessPageListCtrl
); Index
++) {
321 memset(&lvitem
, 0, sizeof(LV_ITEM
));
322 lvitem
.mask
= LVIF_STATE
;
323 lvitem
.stateMask
= LVIS_SELECTED
;
324 lvitem
.iItem
= Index
;
325 SendMessage(hProcessPageListCtrl
, LVM_GETITEM
, 0, (LPARAM
) &lvitem
);
326 if (lvitem
.state
& LVIS_SELECTED
)
329 dwProcessId
= PerfDataGetProcessId(Index
);
330 if ((ListView_GetSelectedCount(hProcessPageListCtrl
) != 1) || (dwProcessId
== 0))
332 hProcessAffinityHandle
= OpenProcess(PROCESS_QUERY_INFORMATION
|PROCESS_SET_INFORMATION
, FALSE
, dwProcessId
);
333 if (!hProcessAffinityHandle
) {
334 GetLastErrorText(strErrorText
, 260);
335 MessageBox(hMainWnd
, strErrorText
, _T("Unable to Access or Set Process Affinity"), MB_OK
|MB_ICONSTOP
);
338 DialogBox(hInst
, MAKEINTRESOURCE(IDD_AFFINITY_DIALOG
), hMainWnd
, AffinityDialogWndProc
);
339 if (hProcessAffinityHandle
) {
340 CloseHandle(hProcessAffinityHandle
);
341 hProcessAffinityHandle
= NULL
;