Reactos
1/*
2 * PROJECT: ReactOS Task Manager
3 * LICENSE: LGPL-2.1-or-later (https://spdx.org/licenses/LGPL-2.1-or-later)
4 * PURPOSE: Processor Affinity.
5 * COPYRIGHT: Copyright 1999-2001 Brian Palmer <brianp@reactos.org>
6 * Copyright 2005 Klemens Friedl <frik85@reactos.at>
7 */
8
9#include "precomp.h"
10
11HANDLE hProcessAffinityHandle;
12
13static const DWORD dwCpuTable[] = {
14 IDC_CPU0, IDC_CPU1, IDC_CPU2, IDC_CPU3,
15 IDC_CPU4, IDC_CPU5, IDC_CPU6, IDC_CPU7,
16 IDC_CPU8, IDC_CPU9, IDC_CPU10, IDC_CPU11,
17 IDC_CPU12, IDC_CPU13, IDC_CPU14, IDC_CPU15,
18 IDC_CPU16, IDC_CPU17, IDC_CPU18, IDC_CPU19,
19 IDC_CPU20, IDC_CPU21, IDC_CPU22, IDC_CPU23,
20 IDC_CPU24, IDC_CPU25, IDC_CPU26, IDC_CPU27,
21 IDC_CPU28, IDC_CPU29, IDC_CPU30, IDC_CPU31,
22};
23
24static INT_PTR CALLBACK AffinityDialogWndProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
25
26void ProcessPage_OnSetAffinity(void)
27{
28 DWORD dwProcessId;
29 WCHAR strErrorText[260];
30 WCHAR szTitle[256];
31
32 dwProcessId = GetSelectedProcessId();
33
34 if (dwProcessId == 0)
35 return;
36
37 hProcessAffinityHandle = OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_SET_INFORMATION, FALSE, dwProcessId);
38 if (!hProcessAffinityHandle) {
39 GetLastErrorText(strErrorText, _countof(strErrorText));
40 LoadStringW(hInst, IDS_MSG_ACCESSPROCESSAFF, szTitle, _countof(szTitle));
41 MessageBoxW(hMainWnd, strErrorText, szTitle, MB_OK|MB_ICONSTOP);
42 return;
43 }
44 DialogBoxW(hInst, MAKEINTRESOURCEW(IDD_AFFINITY_DIALOG), hMainWnd, AffinityDialogWndProc);
45 if (hProcessAffinityHandle) {
46 CloseHandle(hProcessAffinityHandle);
47 hProcessAffinityHandle = NULL;
48 }
49}
50
51INT_PTR CALLBACK
52AffinityDialogWndProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
53{
54 DWORD_PTR dwProcessAffinityMask = 0;
55 DWORD_PTR dwSystemAffinityMask = 0;
56 WCHAR strErrorText[260];
57 WCHAR szTitle[256];
58 BYTE nCpu;
59
60 switch (message) {
61 case WM_INITDIALOG:
62
63 /*
64 * Get the current affinity mask for the process and
65 * the number of CPUs present in the system
66 */
67 if (!GetProcessAffinityMask(hProcessAffinityHandle, &dwProcessAffinityMask, &dwSystemAffinityMask)) {
68 GetLastErrorText(strErrorText, _countof(strErrorText));
69 EndDialog(hDlg, 0);
70 LoadStringW(hInst, IDS_MSG_ACCESSPROCESSAFF, szTitle, _countof(szTitle));
71 MessageBoxW(hMainWnd, strErrorText, szTitle, MB_OK|MB_ICONSTOP);
72 }
73
74 for (nCpu = 0; nCpu < _countof(dwCpuTable); nCpu++) {
75 /*
76 * Enable a checkbox for each processor present in the system
77 */
78 if (dwSystemAffinityMask & ((ULONG_PTR)1 << nCpu))
79 EnableWindow(GetDlgItem(hDlg, dwCpuTable[nCpu]), TRUE);
80 /*
81 * Check each checkbox that the current process
82 * has affinity with
83 */
84 if (dwProcessAffinityMask & ((ULONG_PTR)1 << nCpu))
85 CheckDlgButton(hDlg, dwCpuTable[nCpu], BST_CHECKED);
86 }
87
88 return TRUE;
89
90 case WM_COMMAND:
91
92 /*
93 * If the user has cancelled the dialog box
94 * then just close it
95 */
96 if (LOWORD(wParam) == IDCANCEL) {
97 EndDialog(hDlg, LOWORD(wParam));
98 return TRUE;
99 }
100
101 /*
102 * The user has clicked OK -- so now we have
103 * to adjust the process affinity mask
104 */
105 if (LOWORD(wParam) == IDOK) {
106 for (nCpu = 0; nCpu < _countof(dwCpuTable); nCpu++) {
107 /*
108 * First we have to create a mask out of each
109 * checkbox that the user checked.
110 */
111 if (IsDlgButtonChecked(hDlg, dwCpuTable[nCpu]))
112 dwProcessAffinityMask |= ((ULONG_PTR)1 << nCpu);
113 }
114
115 /*
116 * Make sure they are giving the process affinity
117 * with at least one processor. I'd hate to see a
118 * process that is not in a wait state get deprived
119 * of it's cpu time.
120 */
121 if (!dwProcessAffinityMask) {
122 LoadStringW(hInst, IDS_MSG_PROCESSONEPRO, strErrorText, _countof(strErrorText));
123 LoadStringW(hInst, IDS_MSG_INVALIDOPTION, szTitle, _countof(szTitle));
124 MessageBoxW(hDlg, strErrorText, szTitle, MB_OK|MB_ICONSTOP);
125 return TRUE;
126 }
127
128 /*
129 * Try to set the process affinity
130 */
131 if (!SetProcessAffinityMask(hProcessAffinityHandle, dwProcessAffinityMask)) {
132 GetLastErrorText(strErrorText, _countof(strErrorText));
133 EndDialog(hDlg, LOWORD(wParam));
134 LoadStringW(hInst, IDS_MSG_ACCESSPROCESSAFF, szTitle, _countof(szTitle));
135 MessageBoxW(hMainWnd, strErrorText, szTitle, MB_OK|MB_ICONSTOP);
136 }
137
138 EndDialog(hDlg, LOWORD(wParam));
139 return TRUE;
140 }
141
142 break;
143 }
144
145 return 0;
146}