6 * Copyright (C) 1999 - 2001 Brian Palmer <brianp@reactos.org>
7 * Copyright (C) 2008 Vladimir Pankratov
8 * Copyright (C) 2019 Isira Seneviratne
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
35 HANDLE hProcessAffinityHandle
;
37 WCHAR wszUnable2Access
[255];
39 static INT_PTR CALLBACK
40 AffinityDialogWndProc(HWND hDlg
, UINT message
, WPARAM wParam
, LPARAM lParam
)
42 DWORD_PTR dwProcessAffinityMask
= 0;
43 DWORD_PTR dwSystemAffinityMask
= 0;
44 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
, ARRAY_SIZE(wstrErrorText
));
57 LoadStringW(hInst
, IDS_AFFINITY_UNABLE2ACCESS
, wszUnable2Access
, ARRAY_SIZE(wszUnable2Access
));
58 MessageBoxW(hMainWnd
, wstrErrorText
, wszUnable2Access
, MB_OK
|MB_ICONSTOP
);
62 * Enable a checkbox for each processor present in the system
64 for (i
= 0; i
< 32; i
++)
65 if (dwSystemAffinityMask
& (1 << i
))
66 EnableWindow(GetDlgItem(hDlg
, IDC_CPU0
+ i
), TRUE
);
70 * Check each checkbox that the current process
73 for (i
= 0; i
< 32; i
++)
74 if (dwProcessAffinityMask
& (1 << i
))
75 SendMessageW(GetDlgItem(hDlg
, IDC_CPU0
+ i
), BM_SETCHECK
, BST_CHECKED
, 0);
82 * If the user has cancelled the dialog box
85 if (LOWORD(wParam
) == IDCANCEL
) {
86 EndDialog(hDlg
, LOWORD(wParam
));
91 * The user has clicked OK -- so now we have
92 * to adjust the process affinity mask
94 if (LOWORD(wParam
) == IDOK
) {
96 * First we have to create a mask out of each
97 * checkbox that the user checked.
99 for (i
= 0; i
< 32; i
++)
100 if (SendMessageW(GetDlgItem(hDlg
, IDC_CPU0
+ i
), BM_GETCHECK
, 0, 0))
101 dwProcessAffinityMask
|= (1 << i
);
104 * Make sure they are giving the process affinity
105 * with at least one processor. I'd hate to see a
106 * process that is not in a wait state get deprived
109 if (!dwProcessAffinityMask
) {
110 WCHAR wszErrorMsg
[255];
111 WCHAR wszErrorTitle
[255];
112 LoadStringW(hInst
, IDS_AFFINITY_ERROR_MESSAGE
, wszErrorMsg
, ARRAY_SIZE(wszErrorMsg
));
113 LoadStringW(hInst
, IDS_AFFINITY_ERROR_TITLE
, wszErrorTitle
, ARRAY_SIZE(wszErrorTitle
));
114 MessageBoxW(hDlg
, wszErrorMsg
, wszErrorTitle
, MB_OK
|MB_ICONSTOP
);
119 * Try to set the process affinity
121 if (!SetProcessAffinityMask(hProcessAffinityHandle
, dwProcessAffinityMask
)) {
122 GetLastErrorText(wstrErrorText
, ARRAY_SIZE(wstrErrorText
));
123 EndDialog(hDlg
, LOWORD(wParam
));
124 LoadStringW(hInst
, IDS_AFFINITY_UNABLE2ACCESS
, wszUnable2Access
, ARRAY_SIZE(wszUnable2Access
));
125 MessageBoxW(hMainWnd
, wstrErrorText
, wszUnable2Access
, MB_OK
|MB_ICONSTOP
);
128 EndDialog(hDlg
, LOWORD(wParam
));
138 void ProcessPage_OnSetAffinity(void)
143 WCHAR wstrErrorText
[256];
145 Count
= SendMessageW(hProcessPageListCtrl
, LVM_GETITEMCOUNT
, 0, 0);
146 for (Index
=0; Index
<Count
; Index
++) {
147 memset(&lvitem
, 0, sizeof(LV_ITEMW
));
148 lvitem
.mask
= LVIF_STATE
;
149 lvitem
.stateMask
= LVIS_SELECTED
;
150 lvitem
.iItem
= Index
;
151 SendMessageW(hProcessPageListCtrl
, LVM_GETITEMW
, 0, (LPARAM
) &lvitem
);
152 if (lvitem
.state
& LVIS_SELECTED
)
156 Count
= SendMessageW(hProcessPageListCtrl
, LVM_GETSELECTEDCOUNT
, 0, 0);
157 dwProcessId
= PerfDataGetProcessId(Index
);
158 if ((Count
!= 1) || (dwProcessId
== 0))
160 hProcessAffinityHandle
= OpenProcess(PROCESS_QUERY_INFORMATION
|PROCESS_SET_INFORMATION
, FALSE
, dwProcessId
);
161 if (!hProcessAffinityHandle
) {
162 GetLastErrorText(wstrErrorText
, ARRAY_SIZE(wstrErrorText
));
163 LoadStringW(hInst
, IDS_AFFINITY_UNABLE2ACCESS
, wszUnable2Access
, ARRAY_SIZE(wszUnable2Access
));
164 MessageBoxW(hMainWnd
, wstrErrorText
, wszUnable2Access
, MB_OK
|MB_ICONSTOP
);
167 DialogBoxW(hInst
, MAKEINTRESOURCEW(IDD_AFFINITY_DIALOG
), hMainWnd
, AffinityDialogWndProc
);
168 if (hProcessAffinityHandle
) {
169 CloseHandle(hProcessAffinityHandle
);
170 hProcessAffinityHandle
= NULL
;