Change some Dll* functions so they are exported by name like on
[wine] / programs / taskmgr / priority.c
1 /*
2  *  ReactOS Task Manager
3  *
4  *  priority.c
5  *
6  *  Copyright (C) 1999 - 2001  Brian Palmer  <brianp@reactos.org>
7  *
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.
12  *
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.
17  *
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22     
23 #define WIN32_LEAN_AND_MEAN    /* Exclude rarely-used stuff from Windows headers */
24 #include <windows.h>
25 #include <commctrl.h>
26 #include <stdlib.h>
27 #include <malloc.h>
28 #include <memory.h>
29 #include <tchar.h>
30 #include <stdio.h>
31 #include <winnt.h>
32     
33 #include "taskmgr.h"
34 #include "perfdata.h"
35
36 void ProcessPage_OnSetPriorityRealTime(void)
37 {
38     LVITEM            lvitem;
39     ULONG            Index;
40     DWORD            dwProcessId;
41     HANDLE            hProcess;
42     TCHAR            strErrorText[260];
43
44     for (Index=0; Index<(ULONG)ListView_GetItemCount(hProcessPageListCtrl); Index++)
45     {
46         memset(&lvitem, 0, sizeof(LVITEM));
47
48         lvitem.mask = LVIF_STATE;
49         lvitem.stateMask = LVIS_SELECTED;
50         lvitem.iItem = Index;
51
52         ListView_GetItem(hProcessPageListCtrl, &lvitem);
53
54         if (lvitem.state & LVIS_SELECTED)
55             break;
56     }
57
58     dwProcessId = PerfDataGetProcessId(Index);
59
60     if ((ListView_GetSelectedCount(hProcessPageListCtrl) != 1) || (dwProcessId == 0))
61         return;
62
63     if (MessageBox(hMainWnd, _T("WARNING: Changing the priority class of this process may\ncause undesired results including system instability. Are you\nsure you want to change the priority class?"), _T("Task Manager Warning"), MB_YESNO|MB_ICONWARNING) != IDYES)
64         return;
65
66     hProcess = OpenProcess(PROCESS_SET_INFORMATION, FALSE, dwProcessId);
67
68     if (!hProcess)
69     {
70         GetLastErrorText(strErrorText, 260);
71         MessageBox(hMainWnd, strErrorText, _T("Unable to Change Priority"), MB_OK|MB_ICONSTOP);
72         return;
73     }
74
75     if (!SetPriorityClass(hProcess, REALTIME_PRIORITY_CLASS))
76     {
77         GetLastErrorText(strErrorText, 260);
78         MessageBox(hMainWnd, strErrorText, _T("Unable to Change Priority"), MB_OK|MB_ICONSTOP);
79     }
80
81     CloseHandle(hProcess);
82 }
83
84 void ProcessPage_OnSetPriorityHigh(void)
85 {
86     LVITEM            lvitem;
87     ULONG            Index;
88     DWORD            dwProcessId;
89     HANDLE            hProcess;
90     TCHAR            strErrorText[260];
91
92     for (Index=0; Index<(ULONG)ListView_GetItemCount(hProcessPageListCtrl); Index++)
93     {
94         memset(&lvitem, 0, sizeof(LVITEM));
95
96         lvitem.mask = LVIF_STATE;
97         lvitem.stateMask = LVIS_SELECTED;
98         lvitem.iItem = Index;
99
100         ListView_GetItem(hProcessPageListCtrl, &lvitem);
101
102         if (lvitem.state & LVIS_SELECTED)
103             break;
104     }
105
106     dwProcessId = PerfDataGetProcessId(Index);
107
108     if ((ListView_GetSelectedCount(hProcessPageListCtrl) != 1) || (dwProcessId == 0))
109         return;
110
111     if (MessageBox(hMainWnd, _T("WARNING: Changing the priority class of this process may\ncause undesired results including system instability. Are you\nsure you want to change the priority class?"), _T("Task Manager Warning"), MB_YESNO|MB_ICONWARNING) != IDYES)
112         return;
113
114     hProcess = OpenProcess(PROCESS_SET_INFORMATION, FALSE, dwProcessId);
115
116     if (!hProcess)
117     {
118         GetLastErrorText(strErrorText, 260);
119         MessageBox(hMainWnd, strErrorText, _T("Unable to Change Priority"), MB_OK|MB_ICONSTOP);
120         return;
121     }
122
123     if (!SetPriorityClass(hProcess, HIGH_PRIORITY_CLASS))
124     {
125         GetLastErrorText(strErrorText, 260);
126         MessageBox(hMainWnd, strErrorText, _T("Unable to Change Priority"), MB_OK|MB_ICONSTOP);
127     }
128
129     CloseHandle(hProcess);
130 }
131
132 void ProcessPage_OnSetPriorityAboveNormal(void)
133 {
134     LVITEM            lvitem;
135     ULONG            Index;
136     DWORD            dwProcessId;
137     HANDLE            hProcess;
138     TCHAR            strErrorText[260];
139
140     for (Index=0; Index<(ULONG)ListView_GetItemCount(hProcessPageListCtrl); Index++)
141     {
142         memset(&lvitem, 0, sizeof(LVITEM));
143
144         lvitem.mask = LVIF_STATE;
145         lvitem.stateMask = LVIS_SELECTED;
146         lvitem.iItem = Index;
147
148         ListView_GetItem(hProcessPageListCtrl, &lvitem);
149
150         if (lvitem.state & LVIS_SELECTED)
151             break;
152     }
153
154     dwProcessId = PerfDataGetProcessId(Index);
155
156     if ((ListView_GetSelectedCount(hProcessPageListCtrl) != 1) || (dwProcessId == 0))
157         return;
158
159     if (MessageBox(hMainWnd, _T("WARNING: Changing the priority class of this process may\ncause undesired results including system instability. Are you\nsure you want to change the priority class?"), _T("Task Manager Warning"), MB_YESNO|MB_ICONWARNING) != IDYES)
160         return;
161
162     hProcess = OpenProcess(PROCESS_SET_INFORMATION, FALSE, dwProcessId);
163
164     if (!hProcess)
165     {
166         GetLastErrorText(strErrorText, 260);
167         MessageBox(hMainWnd, strErrorText, _T("Unable to Change Priority"), MB_OK|MB_ICONSTOP);
168         return;
169     }
170
171     if (!SetPriorityClass(hProcess, ABOVE_NORMAL_PRIORITY_CLASS))
172     {
173         GetLastErrorText(strErrorText, 260);
174         MessageBox(hMainWnd, strErrorText, _T("Unable to Change Priority"), MB_OK|MB_ICONSTOP);
175     }
176
177     CloseHandle(hProcess);
178 }
179
180 void ProcessPage_OnSetPriorityNormal(void)
181 {
182     LVITEM            lvitem;
183     ULONG            Index;
184     DWORD            dwProcessId;
185     HANDLE            hProcess;
186     TCHAR            strErrorText[260];
187
188     for (Index=0; Index<(ULONG)ListView_GetItemCount(hProcessPageListCtrl); Index++)
189     {
190         memset(&lvitem, 0, sizeof(LVITEM));
191
192         lvitem.mask = LVIF_STATE;
193         lvitem.stateMask = LVIS_SELECTED;
194         lvitem.iItem = Index;
195
196         ListView_GetItem(hProcessPageListCtrl, &lvitem);
197
198         if (lvitem.state & LVIS_SELECTED)
199             break;
200     }
201
202     dwProcessId = PerfDataGetProcessId(Index);
203
204     if ((ListView_GetSelectedCount(hProcessPageListCtrl) != 1) || (dwProcessId == 0))
205         return;
206
207     if (MessageBox(hMainWnd, _T("WARNING: Changing the priority class of this process may\ncause undesired results including system instability. Are you\nsure you want to change the priority class?"), _T("Task Manager Warning"), MB_YESNO|MB_ICONWARNING) != IDYES)
208         return;
209
210     hProcess = OpenProcess(PROCESS_SET_INFORMATION, FALSE, dwProcessId);
211
212     if (!hProcess)
213     {
214         GetLastErrorText(strErrorText, 260);
215         MessageBox(hMainWnd, strErrorText, _T("Unable to Change Priority"), MB_OK|MB_ICONSTOP);
216         return;
217     }
218
219     if (!SetPriorityClass(hProcess, NORMAL_PRIORITY_CLASS))
220     {
221         GetLastErrorText(strErrorText, 260);
222         MessageBox(hMainWnd, strErrorText, _T("Unable to Change Priority"), MB_OK|MB_ICONSTOP);
223     }
224
225     CloseHandle(hProcess);
226 }
227
228 void ProcessPage_OnSetPriorityBelowNormal(void)
229 {
230     LVITEM            lvitem;
231     ULONG            Index;
232     DWORD            dwProcessId;
233     HANDLE            hProcess;
234     TCHAR            strErrorText[260];
235
236     for (Index=0; Index<(ULONG)ListView_GetItemCount(hProcessPageListCtrl); Index++)
237     {
238         memset(&lvitem, 0, sizeof(LVITEM));
239
240         lvitem.mask = LVIF_STATE;
241         lvitem.stateMask = LVIS_SELECTED;
242         lvitem.iItem = Index;
243
244         ListView_GetItem(hProcessPageListCtrl, &lvitem);
245
246         if (lvitem.state & LVIS_SELECTED)
247             break;
248     }
249
250     dwProcessId = PerfDataGetProcessId(Index);
251
252     if ((ListView_GetSelectedCount(hProcessPageListCtrl) != 1) || (dwProcessId == 0))
253         return;
254
255     if (MessageBox(hMainWnd, _T("WARNING: Changing the priority class of this process may\ncause undesired results including system instability. Are you\nsure you want to change the priority class?"), _T("Task Manager Warning"), MB_YESNO|MB_ICONWARNING) != IDYES)
256         return;
257
258     hProcess = OpenProcess(PROCESS_SET_INFORMATION, FALSE, dwProcessId);
259
260     if (!hProcess)
261     {
262         GetLastErrorText(strErrorText, 260);
263         MessageBox(hMainWnd, strErrorText, _T("Unable to Change Priority"), MB_OK|MB_ICONSTOP);
264         return;
265     }
266
267     if (!SetPriorityClass(hProcess, BELOW_NORMAL_PRIORITY_CLASS))
268     {
269         GetLastErrorText(strErrorText, 260);
270         MessageBox(hMainWnd, strErrorText, _T("Unable to Change Priority"), MB_OK|MB_ICONSTOP);
271     }
272
273     CloseHandle(hProcess);
274 }
275
276 void ProcessPage_OnSetPriorityLow(void)
277 {
278     LVITEM            lvitem;
279     ULONG            Index;
280     DWORD            dwProcessId;
281     HANDLE            hProcess;
282     TCHAR            strErrorText[260];
283
284     for (Index=0; Index<(ULONG)ListView_GetItemCount(hProcessPageListCtrl); Index++)
285     {
286         memset(&lvitem, 0, sizeof(LVITEM));
287
288         lvitem.mask = LVIF_STATE;
289         lvitem.stateMask = LVIS_SELECTED;
290         lvitem.iItem = Index;
291
292         ListView_GetItem(hProcessPageListCtrl, &lvitem);
293
294         if (lvitem.state & LVIS_SELECTED)
295             break;
296     }
297
298     dwProcessId = PerfDataGetProcessId(Index);
299
300     if ((ListView_GetSelectedCount(hProcessPageListCtrl) != 1) || (dwProcessId == 0))
301         return;
302
303     if (MessageBox(hMainWnd, _T("WARNING: Changing the priority class of this process may\ncause undesired results including system instability. Are you\nsure you want to change the priority class?"), _T("Task Manager Warning"), MB_YESNO|MB_ICONWARNING) != IDYES)
304         return;
305
306     hProcess = OpenProcess(PROCESS_SET_INFORMATION, FALSE, dwProcessId);
307
308     if (!hProcess)
309     {
310         GetLastErrorText(strErrorText, 260);
311         MessageBox(hMainWnd, strErrorText, _T("Unable to Change Priority"), MB_OK|MB_ICONSTOP);
312         return;
313     }
314
315     if (!SetPriorityClass(hProcess, IDLE_PRIORITY_CLASS))
316     {
317         GetLastErrorText(strErrorText, 260);
318         MessageBox(hMainWnd, strErrorText, _T("Unable to Change Priority"), MB_OK|MB_ICONSTOP);
319     }
320
321     CloseHandle(hProcess);
322 }