* Sync with recent trunk (r52637).
[reactos.git] / base / applications / taskmgr / procpage.c
1 /*
2 * ReactOS Task Manager
3 *
4 * procpage.c
5 *
6 * Copyright (C) 1999 - 2001 Brian Palmer <brianp@reactos.org>
7 * Copyright (C) 2009 Maxime Vernier <maxime.vernier@gmail.com>
8 *
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.
13 *
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.
18 *
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 #include <precomp.h>
25
26 #define CMP(x1, x2)\
27 (x1 < x2 ? -1 : (x1 > x2 ? 1 : 0))
28
29 typedef struct
30 {
31 ULONG ProcessId;
32 } PROCESS_PAGE_LIST_ITEM, *LPPROCESS_PAGE_LIST_ITEM;
33
34 HWND hProcessPage; /* Process List Property Page */
35
36 HWND hProcessPageListCtrl; /* Process ListCtrl Window */
37 HWND hProcessPageHeaderCtrl; /* Process Header Control */
38 HWND hProcessPageEndProcessButton; /* Process End Process button */
39 HWND hProcessPageShowAllProcessesButton;/* Process Show All Processes checkbox */
40
41 static int nProcessPageWidth;
42 static int nProcessPageHeight;
43 #ifdef RUN_PROC_PAGE
44 static HANDLE hProcessThread = NULL;
45 static DWORD dwProcessThread;
46 #endif
47
48 int CALLBACK ProcessPageCompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort);
49 void AddProcess(ULONG Index);
50 void UpdateProcesses();
51 void gethmsfromlargeint(LARGE_INTEGER largeint, DWORD *dwHours, DWORD *dwMinutes, DWORD *dwSeconds);
52 void ProcessPageOnNotify(WPARAM wParam, LPARAM lParam);
53 void CommaSeparateNumberString(LPWSTR strNumber, int nMaxCount);
54 void ProcessPageShowContextMenu(DWORD dwProcessId);
55 BOOL PerfDataGetText(ULONG Index, ULONG ColumnIndex, LPTSTR lpText, int nMaxCount);
56 DWORD WINAPI ProcessPageRefreshThread(void *lpParameter);
57 int ProcessRunning(ULONG ProcessId);
58
59 int ProcGetIndexByProcessId(DWORD dwProcessId)
60 {
61 int i;
62 LVITEM item;
63 LPPROCESS_PAGE_LIST_ITEM pData;
64
65 for (i=0; i<ListView_GetItemCount(hProcessPageListCtrl); i++)
66 {
67 memset(&item, 0, sizeof(LV_ITEM));
68 item.mask = LVIF_PARAM;
69 item.iItem = i;
70 (void)ListView_GetItem(hProcessPageListCtrl, &item);
71 pData = (LPPROCESS_PAGE_LIST_ITEM)item.lParam;
72 if (pData->ProcessId == dwProcessId)
73 {
74 return i;
75 }
76 }
77 return 0;
78 }
79
80 DWORD GetSelectedProcessId(void)
81 {
82 int Index;
83 LVITEM lvitem;
84
85 if(ListView_GetSelectedCount(hProcessPageListCtrl) == 1)
86 {
87 Index = ListView_GetSelectionMark(hProcessPageListCtrl);
88
89 memset(&lvitem, 0, sizeof(LVITEM));
90
91 lvitem.mask = LVIF_PARAM;
92 lvitem.iItem = Index;
93
94 (void)ListView_GetItem(hProcessPageListCtrl, &lvitem);
95
96 if (lvitem.lParam)
97 return ((LPPROCESS_PAGE_LIST_ITEM)lvitem.lParam)->ProcessId;
98 }
99
100 return 0;
101 }
102
103 INT_PTR CALLBACK
104 ProcessPageWndProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
105 {
106 RECT rc;
107 int nXDifference;
108 int nYDifference;
109 int cx, cy;
110
111 switch (message) {
112 case WM_INITDIALOG:
113 /*
114 * Save the width and height
115 */
116 GetClientRect(hDlg, &rc);
117 nProcessPageWidth = rc.right;
118 nProcessPageHeight = rc.bottom;
119
120 /* Update window position */
121 SetWindowPos(hDlg, NULL, 15, 30, 0, 0, SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOSIZE|SWP_NOZORDER);
122
123 /*
124 * Get handles to the controls
125 */
126 hProcessPageListCtrl = GetDlgItem(hDlg, IDC_PROCESSLIST);
127 hProcessPageHeaderCtrl = ListView_GetHeader(hProcessPageListCtrl);
128 hProcessPageEndProcessButton = GetDlgItem(hDlg, IDC_ENDPROCESS);
129 hProcessPageShowAllProcessesButton = GetDlgItem(hDlg, IDC_SHOWALLPROCESSES);
130
131 /*
132 * Set the title, and extended window styles for the list control
133 */
134 SetWindowTextW(hProcessPageListCtrl, L"Processes");
135 (void)ListView_SetExtendedListViewStyle(hProcessPageListCtrl, ListView_GetExtendedListViewStyle(hProcessPageListCtrl) | LVS_EX_FULLROWSELECT | LVS_EX_HEADERDRAGDROP);
136
137 AddColumns();
138
139 /*
140 * Subclass the process list control so we can intercept WM_ERASEBKGND
141 */
142 OldProcessListWndProc = (WNDPROC)(LONG_PTR) SetWindowLongPtrW(hProcessPageListCtrl, GWL_WNDPROC, (LONG_PTR)ProcessListWndProc);
143
144 #ifdef RUN_PROC_PAGE
145 /* Start our refresh thread */
146 hProcessThread = CreateThread(NULL, 0, ProcessPageRefreshThread, NULL, 0, &dwProcessThread);
147 #endif
148 return TRUE;
149
150 case WM_DESTROY:
151 /* Close the event handle, this will make the */
152 /* refresh thread exit when the wait fails */
153 #ifdef RUN_PROC_PAGE
154 EndLocalThread(&hProcessThread, dwProcessThread);
155 #endif
156 SaveColumnSettings();
157 break;
158
159 case WM_COMMAND:
160 /* Handle the button clicks */
161 switch (LOWORD(wParam))
162 {
163 case IDC_ENDPROCESS:
164 ProcessPage_OnEndProcess();
165 }
166 break;
167
168 case WM_SIZE:
169 if (wParam == SIZE_MINIMIZED)
170 return 0;
171
172 cx = LOWORD(lParam);
173 cy = HIWORD(lParam);
174 nXDifference = cx - nProcessPageWidth;
175 nYDifference = cy - nProcessPageHeight;
176 nProcessPageWidth = cx;
177 nProcessPageHeight = cy;
178
179 /* Reposition the application page's controls */
180 GetWindowRect(hProcessPageListCtrl, &rc);
181 cx = (rc.right - rc.left) + nXDifference;
182 cy = (rc.bottom - rc.top) + nYDifference;
183 SetWindowPos(hProcessPageListCtrl, NULL, 0, 0, cx, cy, SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOMOVE|SWP_NOZORDER);
184 InvalidateRect(hProcessPageListCtrl, NULL, TRUE);
185
186 GetClientRect(hProcessPageEndProcessButton, &rc);
187 MapWindowPoints(hProcessPageEndProcessButton, hDlg, (LPPOINT)(PRECT)(&rc), (sizeof(RECT)/sizeof(POINT)) );
188 cx = rc.left + nXDifference;
189 cy = rc.top + nYDifference;
190 SetWindowPos(hProcessPageEndProcessButton, NULL, cx, cy, 0, 0, SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOSIZE|SWP_NOZORDER);
191 InvalidateRect(hProcessPageEndProcessButton, NULL, TRUE);
192
193 GetClientRect(hProcessPageShowAllProcessesButton, &rc);
194 MapWindowPoints(hProcessPageShowAllProcessesButton, hDlg, (LPPOINT)(PRECT)(&rc), (sizeof(RECT)/sizeof(POINT)) );
195 cx = rc.left;
196 cy = rc.top + nYDifference;
197 SetWindowPos(hProcessPageShowAllProcessesButton, NULL, cx, cy, 0, 0, SWP_NOACTIVATE|SWP_NOOWNERZORDER|SWP_NOSIZE|SWP_NOZORDER);
198 InvalidateRect(hProcessPageShowAllProcessesButton, NULL, TRUE);
199 break;
200
201 case WM_NOTIFY:
202 ProcessPageOnNotify(wParam, lParam);
203 break;
204
205 case WM_KEYDOWN:
206 if (wParam == VK_DELETE)
207 ProcessPage_OnEndProcess();
208 break;
209 }
210
211 return 0;
212 }
213
214 void ProcessPageOnNotify(WPARAM wParam, LPARAM lParam)
215 {
216 int idctrl;
217 LPNMHDR pnmh;
218 LPNMLISTVIEW pnmv;
219 NMLVDISPINFO* pnmdi;
220 LPNMHEADER pnmhdr;
221 ULONG Index;
222 ULONG ColumnIndex;
223 LPPROCESS_PAGE_LIST_ITEM pData;
224
225 idctrl = (int) wParam;
226 pnmh = (LPNMHDR) lParam;
227 pnmv = (LPNMLISTVIEW) lParam;
228 pnmdi = (NMLVDISPINFO*) lParam;
229 pnmhdr = (LPNMHEADER) lParam;
230
231 if (pnmh->hwndFrom == hProcessPageListCtrl)
232 {
233 switch (pnmh->code)
234 {
235 #if 0
236 case LVN_ITEMCHANGED:
237 ProcessPageUpdate();
238 break;
239 #endif
240
241 case LVN_GETDISPINFO:
242
243 if (!(pnmdi->item.mask & LVIF_TEXT))
244 break;
245
246 pData = (LPPROCESS_PAGE_LIST_ITEM)pnmdi->item.lParam;
247 Index = PerfDataGetProcessIndex(pData->ProcessId);
248 ColumnIndex = pnmdi->item.iSubItem;
249
250 PerfDataGetText(Index, ColumnIndex, pnmdi->item.pszText, pnmdi->item.cchTextMax);
251
252 break;
253
254 case NM_RCLICK:
255
256 ProcessPageShowContextMenu(GetSelectedProcessId());
257 break;
258
259 case LVN_KEYDOWN:
260
261 if (((LPNMLVKEYDOWN)lParam)->wVKey == VK_DELETE)
262 ProcessPage_OnEndProcess();
263 break;
264
265 }
266 }
267 else if (pnmh->hwndFrom == hProcessPageHeaderCtrl)
268 {
269 switch (pnmh->code)
270 {
271 case HDN_ITEMCLICK:
272
273 TaskManagerSettings.SortColumn = ColumnDataHints[pnmhdr->iItem];
274 TaskManagerSettings.SortAscending = !TaskManagerSettings.SortAscending;
275 (void)ListView_SortItems(hProcessPageListCtrl, ProcessPageCompareFunc, NULL);
276
277 break;
278
279 case HDN_ITEMCHANGED:
280
281 UpdateColumnDataHints();
282
283 break;
284
285 case HDN_ENDDRAG:
286
287 UpdateColumnDataHints();
288
289 break;
290
291 }
292 }
293 }
294
295 void CommaSeparateNumberString(LPWSTR strNumber, int nMaxCount)
296 {
297 WCHAR temp[260];
298 UINT i, j, k;
299
300 for (i=0,j=0; i<(wcslen(strNumber) % 3); i++, j++)
301 temp[j] = strNumber[i];
302 for (k=0; i<wcslen(strNumber); i++,j++,k++) {
303 if ((k % 3 == 0) && (j > 0))
304 temp[j++] = L',';
305 temp[j] = strNumber[i];
306 }
307 temp[j] = L'\0';
308 wcsncpy(strNumber, temp, nMaxCount);
309 }
310
311 void ProcessPageShowContextMenu(DWORD dwProcessId)
312 {
313 HMENU hMenu;
314 HMENU hSubMenu;
315 HMENU hPriorityMenu;
316 POINT pt;
317 SYSTEM_INFO si;
318 HANDLE hProcess;
319 DWORD dwProcessPriorityClass;
320 WCHAR strDebugger[260];
321 DWORD dwDebuggerSize;
322 HKEY hKey;
323
324 memset(&si, 0, sizeof(SYSTEM_INFO));
325
326 GetCursorPos(&pt);
327 GetSystemInfo(&si);
328
329 hMenu = LoadMenuW(hInst, MAKEINTRESOURCEW(IDR_PROCESS_PAGE_CONTEXT));
330 hSubMenu = GetSubMenu(hMenu, 0);
331 hPriorityMenu = GetSubMenu(hSubMenu, 4);
332
333 hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, dwProcessId);
334 dwProcessPriorityClass = GetPriorityClass(hProcess);
335 CloseHandle(hProcess);
336
337 if (si.dwNumberOfProcessors < 2)
338 RemoveMenu(hSubMenu, ID_PROCESS_PAGE_SETAFFINITY, MF_BYCOMMAND);
339
340 if (!DebugChannelsAreSupported())
341 RemoveMenu(hSubMenu, ID_PROCESS_PAGE_DEBUGCHANNELS, MF_BYCOMMAND);
342
343 switch (dwProcessPriorityClass) {
344 case REALTIME_PRIORITY_CLASS:
345 CheckMenuRadioItem(hPriorityMenu, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, ID_PROCESS_PAGE_SETPRIORITY_LOW, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, MF_BYCOMMAND);
346 break;
347 case HIGH_PRIORITY_CLASS:
348 CheckMenuRadioItem(hPriorityMenu, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, ID_PROCESS_PAGE_SETPRIORITY_LOW, ID_PROCESS_PAGE_SETPRIORITY_HIGH, MF_BYCOMMAND);
349 break;
350 case ABOVE_NORMAL_PRIORITY_CLASS:
351 CheckMenuRadioItem(hPriorityMenu, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, ID_PROCESS_PAGE_SETPRIORITY_LOW, ID_PROCESS_PAGE_SETPRIORITY_ABOVENORMAL, MF_BYCOMMAND);
352 break;
353 case NORMAL_PRIORITY_CLASS:
354 CheckMenuRadioItem(hPriorityMenu, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, ID_PROCESS_PAGE_SETPRIORITY_LOW, ID_PROCESS_PAGE_SETPRIORITY_NORMAL, MF_BYCOMMAND);
355 break;
356 case BELOW_NORMAL_PRIORITY_CLASS:
357 CheckMenuRadioItem(hPriorityMenu, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, ID_PROCESS_PAGE_SETPRIORITY_LOW, ID_PROCESS_PAGE_SETPRIORITY_BELOWNORMAL, MF_BYCOMMAND);
358 break;
359 case IDLE_PRIORITY_CLASS:
360 CheckMenuRadioItem(hPriorityMenu, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, ID_PROCESS_PAGE_SETPRIORITY_LOW, ID_PROCESS_PAGE_SETPRIORITY_LOW, MF_BYCOMMAND);
361 break;
362 }
363
364 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\Windows NT\\CurrentVersion\\AeDebug", 0, KEY_READ, &hKey) == ERROR_SUCCESS)
365 {
366 dwDebuggerSize = 260;
367 if (RegQueryValueExW(hKey, L"Debugger", NULL, NULL, (LPBYTE)strDebugger, &dwDebuggerSize) == ERROR_SUCCESS)
368 {
369 CharUpper(strDebugger);
370 if (wcsstr(strDebugger, L"DRWTSN32"))
371 EnableMenuItem(hSubMenu, ID_PROCESS_PAGE_DEBUG, MF_BYCOMMAND|MF_DISABLED|MF_GRAYED);
372 }
373 else
374 EnableMenuItem(hSubMenu, ID_PROCESS_PAGE_DEBUG, MF_BYCOMMAND|MF_DISABLED|MF_GRAYED);
375
376 RegCloseKey(hKey);
377 } else {
378 EnableMenuItem(hSubMenu, ID_PROCESS_PAGE_DEBUG, MF_BYCOMMAND|MF_DISABLED|MF_GRAYED);
379 }
380 TrackPopupMenu(hSubMenu, TPM_LEFTALIGN|TPM_TOPALIGN|TPM_LEFTBUTTON, pt.x, pt.y, 0, hMainWnd, NULL);
381 DestroyMenu(hMenu);
382 }
383
384 void RefreshProcessPage(void)
385 {
386 #ifdef RUN_PROC_PAGE
387 /* Signal the event so that our refresh thread */
388 /* will wake up and refresh the process page */
389 PostThreadMessage(dwProcessThread, WM_TIMER, 0, 0);
390 #endif
391 }
392
393 DWORD WINAPI ProcessPageRefreshThread(void *lpParameter)
394 {
395 ULONG OldProcessorUsage = 0;
396 ULONG OldProcessCount = 0;
397 WCHAR szCpuUsage[256], szProcesses[256];
398 MSG msg;
399
400 LoadStringW(hInst, IDS_STATUS_CPUUSAGE, szCpuUsage, 256);
401 LoadStringW(hInst, IDS_STATUS_PROCESSES, szProcesses, 256);
402
403 while (1) {
404 /* Wait for an the event or application close */
405 if (GetMessage(&msg, NULL, 0, 0) <= 0)
406 return 0;
407
408 if (msg.message == WM_TIMER) {
409 WCHAR text[260];
410
411 UpdateProcesses();
412
413 if (IsWindowVisible(hProcessPage))
414 InvalidateRect(hProcessPageListCtrl, NULL, FALSE);
415
416 if (OldProcessorUsage != PerfDataGetProcessorUsage()) {
417 OldProcessorUsage = PerfDataGetProcessorUsage();
418 wsprintfW(text, szCpuUsage, OldProcessorUsage);
419 SendMessageW(hStatusWnd, SB_SETTEXT, 1, (LPARAM)text);
420 }
421 if (OldProcessCount != PerfDataGetProcessCount()) {
422 OldProcessCount = PerfDataGetProcessCount();
423 wsprintfW(text, szProcesses, OldProcessCount);
424 SendMessageW(hStatusWnd, SB_SETTEXT, 0, (LPARAM)text);
425 }
426 }
427 }
428 return 0;
429 }
430
431 void UpdateProcesses()
432 {
433 int i;
434 ULONG l;
435 LV_ITEM item;
436 LPPROCESS_PAGE_LIST_ITEM pData;
437
438 SendMessage(hProcessPageListCtrl, WM_SETREDRAW, FALSE, 0);
439
440 /* Remove old processes */
441 for (i = 0; i < ListView_GetItemCount(hProcessPageListCtrl); i++)
442 {
443 memset(&item, 0, sizeof (LV_ITEM));
444 item.mask = LVIF_PARAM;
445 item.iItem = i;
446 (void)ListView_GetItem(hProcessPageListCtrl, &item);
447 pData = (LPPROCESS_PAGE_LIST_ITEM)item.lParam;
448 if (!ProcessRunning(pData->ProcessId))
449 {
450 (void)ListView_DeleteItem(hProcessPageListCtrl, i);
451 HeapFree(GetProcessHeap(), 0, pData);
452 }
453 }
454
455 /* Check for difference in listview process and performance process counts */
456 if (ListView_GetItemCount(hProcessPageListCtrl) != PerfDataGetProcessCount())
457 {
458 /* Add new processes by checking against the current items */
459 for (l = 0; l < PerfDataGetProcessCount(); l++)
460 {
461 AddProcess(l);
462 }
463 }
464
465 if (TaskManagerSettings.SortColumn != -1)
466 {
467 (void)ListView_SortItems(hProcessPageListCtrl, ProcessPageCompareFunc, NULL);
468 }
469
470 SendMessage(hProcessPageListCtrl, WM_SETREDRAW, TRUE, 0);
471 }
472
473 BOOL ProcessRunning(ULONG ProcessId)
474 {
475 HANDLE hProcess;
476 DWORD exitCode;
477
478 if (ProcessId == 0) {
479 return TRUE;
480 }
481
482 hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, ProcessId);
483 if (hProcess == NULL) {
484 return FALSE;
485 }
486
487 if (GetExitCodeProcess(hProcess, &exitCode)) {
488 CloseHandle(hProcess);
489 return (exitCode == STILL_ACTIVE);
490 }
491
492 CloseHandle(hProcess);
493 return FALSE;
494 }
495
496 void AddProcess(ULONG Index)
497 {
498 LPPROCESS_PAGE_LIST_ITEM pData;
499 int i;
500 LV_ITEM item;
501 BOOL bAlreadyInList = FALSE;
502 ULONG pid;
503
504 pid = PerfDataGetProcessId(Index);
505
506 /* Check to see if it's already in our list */
507 for (i=0; i<ListView_GetItemCount(hProcessPageListCtrl); i++)
508 {
509 memset(&item, 0, sizeof(LV_ITEM));
510 item.mask = LVIF_PARAM;
511 item.iItem = i;
512 (void)ListView_GetItem(hProcessPageListCtrl, &item);
513 pData = (LPPROCESS_PAGE_LIST_ITEM)item.lParam;
514 if (pData->ProcessId == pid)
515 {
516 bAlreadyInList = TRUE;
517 break;
518 }
519 }
520 if (!bAlreadyInList) /* Add */
521 {
522 pData = (LPPROCESS_PAGE_LIST_ITEM)HeapAlloc(GetProcessHeap(), 0, sizeof(PROCESS_PAGE_LIST_ITEM));
523 pData->ProcessId = pid;
524
525 /* Add the item to the list */
526 memset(&item, 0, sizeof(LV_ITEM));
527 item.mask = LVIF_TEXT|LVIF_PARAM;
528 item.pszText = LPSTR_TEXTCALLBACK;
529 item.iItem = ListView_GetItemCount(hProcessPageListCtrl);
530 item.lParam = (LPARAM)pData;
531 (void)ListView_InsertItem(hProcessPageListCtrl, &item);
532 }
533 }
534
535 BOOL PerfDataGetText(ULONG Index, ULONG ColumnIndex, LPTSTR lpText, int nMaxCount)
536 {
537 IO_COUNTERS iocounters;
538 LARGE_INTEGER time;
539
540 if (ColumnDataHints[ColumnIndex] == COLUMN_IMAGENAME)
541 PerfDataGetImageName(Index, lpText, nMaxCount);
542 if (ColumnDataHints[ColumnIndex] == COLUMN_PID)
543 wsprintfW(lpText, L"%d", PerfDataGetProcessId(Index));
544 if (ColumnDataHints[ColumnIndex] == COLUMN_USERNAME)
545 PerfDataGetUserName(Index, lpText, nMaxCount);
546 if (ColumnDataHints[ColumnIndex] == COLUMN_SESSIONID)
547 wsprintfW(lpText, L"%d", PerfDataGetSessionId(Index));
548 if (ColumnDataHints[ColumnIndex] == COLUMN_CPUUSAGE)
549 wsprintfW(lpText, L"%02d", PerfDataGetCPUUsage(Index));
550 if (ColumnDataHints[ColumnIndex] == COLUMN_CPUTIME)
551 {
552 DWORD dwHours;
553 DWORD dwMinutes;
554 DWORD dwSeconds;
555
556 time = PerfDataGetCPUTime(Index);
557 gethmsfromlargeint(time, &dwHours, &dwMinutes, &dwSeconds);
558 wsprintfW(lpText, L"%d:%02d:%02d", dwHours, dwMinutes, dwSeconds);
559 }
560 if (ColumnDataHints[ColumnIndex] == COLUMN_MEMORYUSAGE)
561 {
562 wsprintfW(lpText, L"%d", PerfDataGetWorkingSetSizeBytes(Index) / 1024);
563 CommaSeparateNumberString(lpText, nMaxCount);
564 wcscat(lpText, L" K");
565 }
566 if (ColumnDataHints[ColumnIndex] == COLUMN_PEAKMEMORYUSAGE)
567 {
568 wsprintfW(lpText, L"%d", PerfDataGetPeakWorkingSetSizeBytes(Index) / 1024);
569 CommaSeparateNumberString(lpText, nMaxCount);
570 wcscat(lpText, L" K");
571 }
572 if (ColumnDataHints[ColumnIndex] == COLUMN_MEMORYUSAGEDELTA)
573 {
574 wsprintfW(lpText, L"%d", PerfDataGetWorkingSetSizeDelta(Index) / 1024);
575 CommaSeparateNumberString(lpText, nMaxCount);
576 wcscat(lpText, L" K");
577 }
578 if (ColumnDataHints[ColumnIndex] == COLUMN_PAGEFAULTS)
579 {
580 wsprintfW(lpText, L"%d", PerfDataGetPageFaultCount(Index));
581 CommaSeparateNumberString(lpText, nMaxCount);
582 }
583 if (ColumnDataHints[ColumnIndex] == COLUMN_PAGEFAULTSDELTA)
584 {
585 wsprintfW(lpText, L"%d", PerfDataGetPageFaultCountDelta(Index));
586 CommaSeparateNumberString(lpText, nMaxCount);
587 }
588 if (ColumnDataHints[ColumnIndex] == COLUMN_VIRTUALMEMORYSIZE)
589 {
590 wsprintfW(lpText, L"%d", PerfDataGetVirtualMemorySizeBytes(Index) / 1024);
591 CommaSeparateNumberString(lpText, nMaxCount);
592 wcscat(lpText, L" K");
593 }
594 if (ColumnDataHints[ColumnIndex] == COLUMN_PAGEDPOOL)
595 {
596 wsprintfW(lpText, L"%d", PerfDataGetPagedPoolUsagePages(Index) / 1024);
597 CommaSeparateNumberString(lpText, nMaxCount);
598 wcscat(lpText, L" K");
599 }
600 if (ColumnDataHints[ColumnIndex] == COLUMN_NONPAGEDPOOL)
601 {
602 wsprintfW(lpText, L"%d", PerfDataGetNonPagedPoolUsagePages(Index) / 1024);
603 CommaSeparateNumberString(lpText, nMaxCount);
604 wcscat(lpText, L" K");
605 }
606 if (ColumnDataHints[ColumnIndex] == COLUMN_BASEPRIORITY)
607 wsprintfW(lpText, L"%d", PerfDataGetBasePriority(Index));
608 if (ColumnDataHints[ColumnIndex] == COLUMN_HANDLECOUNT)
609 {
610 wsprintfW(lpText, L"%d", PerfDataGetHandleCount(Index));
611 CommaSeparateNumberString(lpText, nMaxCount);
612 }
613 if (ColumnDataHints[ColumnIndex] == COLUMN_THREADCOUNT)
614 {
615 wsprintfW(lpText, L"%d", PerfDataGetThreadCount(Index));
616 CommaSeparateNumberString(lpText, nMaxCount);
617 }
618 if (ColumnDataHints[ColumnIndex] == COLUMN_USEROBJECTS)
619 {
620 wsprintfW(lpText, L"%d", PerfDataGetUSERObjectCount(Index));
621 CommaSeparateNumberString(lpText, nMaxCount);
622 }
623 if (ColumnDataHints[ColumnIndex] == COLUMN_GDIOBJECTS)
624 {
625 wsprintfW(lpText, L"%d", PerfDataGetGDIObjectCount(Index));
626 CommaSeparateNumberString(lpText, nMaxCount);
627 }
628 if (ColumnDataHints[ColumnIndex] == COLUMN_IOREADS)
629 {
630 PerfDataGetIOCounters(Index, &iocounters);
631 /* wsprintfW(pnmdi->item.pszText, L"%d", iocounters.ReadOperationCount); */
632 _ui64tow(iocounters.ReadOperationCount, lpText, 10);
633 CommaSeparateNumberString(lpText, nMaxCount);
634 }
635 if (ColumnDataHints[ColumnIndex] == COLUMN_IOWRITES)
636 {
637 PerfDataGetIOCounters(Index, &iocounters);
638 /* wsprintfW(pnmdi->item.pszText, L"%d", iocounters.WriteOperationCount); */
639 _ui64tow(iocounters.WriteOperationCount, lpText, 10);
640 CommaSeparateNumberString(lpText, nMaxCount);
641 }
642 if (ColumnDataHints[ColumnIndex] == COLUMN_IOOTHER)
643 {
644 PerfDataGetIOCounters(Index, &iocounters);
645 /* wsprintfW(pnmdi->item.pszText, L"%d", iocounters.OtherOperationCount); */
646 _ui64tow(iocounters.OtherOperationCount, lpText, 10);
647 CommaSeparateNumberString(lpText, nMaxCount);
648 }
649 if (ColumnDataHints[ColumnIndex] == COLUMN_IOREADBYTES)
650 {
651 PerfDataGetIOCounters(Index, &iocounters);
652 /* wsprintfW(pnmdi->item.pszText, L"%d", iocounters.ReadTransferCount); */
653 _ui64tow(iocounters.ReadTransferCount, lpText, 10);
654 CommaSeparateNumberString(lpText, nMaxCount);
655 }
656 if (ColumnDataHints[ColumnIndex] == COLUMN_IOWRITEBYTES)
657 {
658 PerfDataGetIOCounters(Index, &iocounters);
659 /* wsprintfW(pnmdi->item.pszText, L"%d", iocounters.WriteTransferCount); */
660 _ui64tow(iocounters.WriteTransferCount, lpText, 10);
661 CommaSeparateNumberString(lpText, nMaxCount);
662 }
663 if (ColumnDataHints[ColumnIndex] == COLUMN_IOOTHERBYTES)
664 {
665 PerfDataGetIOCounters(Index, &iocounters);
666 /* wsprintfW(pnmdi->item.pszText, L"%d", iocounters.OtherTransferCount); */
667 _ui64tow(iocounters.OtherTransferCount, lpText, 10);
668 CommaSeparateNumberString(lpText, nMaxCount);
669 }
670
671 return FALSE;
672 }
673
674
675 void gethmsfromlargeint(LARGE_INTEGER largeint, DWORD *dwHours, DWORD *dwMinutes, DWORD *dwSeconds)
676 {
677 #ifdef _MSC_VER
678 *dwHours = (DWORD)(largeint.QuadPart / 36000000000L);
679 *dwMinutes = (DWORD)((largeint.QuadPart % 36000000000L) / 600000000L);
680 *dwSeconds = (DWORD)(((largeint.QuadPart % 36000000000L) % 600000000L) / 10000000L);
681 #else
682 *dwHours = (DWORD)(largeint.QuadPart / 36000000000LL);
683 *dwMinutes = (DWORD)((largeint.QuadPart % 36000000000LL) / 600000000LL);
684 *dwSeconds = (DWORD)(((largeint.QuadPart % 36000000000LL) % 600000000LL) / 10000000LL);
685 #endif
686 }
687
688 int largeintcmp(LARGE_INTEGER l1, LARGE_INTEGER l2)
689 {
690 int ret = 0;
691 DWORD dwHours1;
692 DWORD dwMinutes1;
693 DWORD dwSeconds1;
694 DWORD dwHours2;
695 DWORD dwMinutes2;
696 DWORD dwSeconds2;
697
698 gethmsfromlargeint(l1, &dwHours1, &dwMinutes1, &dwSeconds1);
699 gethmsfromlargeint(l2, &dwHours2, &dwMinutes2, &dwSeconds2);
700 ret = CMP(dwHours1, dwHours2);
701 if (ret == 0)
702 {
703 ret = CMP(dwMinutes1, dwMinutes2);
704 if (ret == 0)
705 {
706 ret = CMP(dwSeconds1, dwSeconds2);
707 }
708 }
709 return ret;
710 }
711
712 int CALLBACK ProcessPageCompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
713 {
714 int ret = 0;
715 LPPROCESS_PAGE_LIST_ITEM Param1;
716 LPPROCESS_PAGE_LIST_ITEM Param2;
717 ULONG IndexParam1;
718 ULONG IndexParam2;
719 WCHAR text1[260];
720 WCHAR text2[260];
721 ULONG l1;
722 ULONG l2;
723 LARGE_INTEGER time1;
724 LARGE_INTEGER time2;
725 IO_COUNTERS iocounters1;
726 IO_COUNTERS iocounters2;
727 ULONGLONG ull1;
728 ULONGLONG ull2;
729
730 if (TaskManagerSettings.SortAscending) {
731 Param1 = (LPPROCESS_PAGE_LIST_ITEM)lParam1;
732 Param2 = (LPPROCESS_PAGE_LIST_ITEM)lParam2;
733 } else {
734 Param1 = (LPPROCESS_PAGE_LIST_ITEM)lParam2;
735 Param2 = (LPPROCESS_PAGE_LIST_ITEM)lParam1;
736 }
737 IndexParam1 = PerfDataGetProcessIndex(Param1->ProcessId);
738 IndexParam2 = PerfDataGetProcessIndex(Param2->ProcessId);
739
740 if (TaskManagerSettings.SortColumn == COLUMN_IMAGENAME)
741 {
742 PerfDataGetImageName(IndexParam1, text1, sizeof (text1) / sizeof (*text1));
743 PerfDataGetImageName(IndexParam2, text2, sizeof (text2) / sizeof (*text2));
744 ret = _wcsicmp(text1, text2);
745 }
746 else if (TaskManagerSettings.SortColumn == COLUMN_PID)
747 {
748 l1 = Param1->ProcessId;
749 l2 = Param2->ProcessId;
750 ret = CMP(l1, l2);
751 }
752 else if (TaskManagerSettings.SortColumn == COLUMN_USERNAME)
753 {
754 PerfDataGetUserName(IndexParam1, text1, sizeof (text1) / sizeof (*text1));
755 PerfDataGetUserName(IndexParam2, text2, sizeof (text2) / sizeof (*text2));
756 ret = _wcsicmp(text1, text2);
757 }
758 else if (TaskManagerSettings.SortColumn == COLUMN_SESSIONID)
759 {
760 l1 = PerfDataGetSessionId(IndexParam1);
761 l2 = PerfDataGetSessionId(IndexParam2);
762 ret = CMP(l1, l2);
763 }
764 else if (TaskManagerSettings.SortColumn == COLUMN_CPUUSAGE)
765 {
766 l1 = PerfDataGetCPUUsage(IndexParam1);
767 l2 = PerfDataGetCPUUsage(IndexParam2);
768 ret = CMP(l1, l2);
769 }
770 else if (TaskManagerSettings.SortColumn == COLUMN_CPUTIME)
771 {
772 time1 = PerfDataGetCPUTime(IndexParam1);
773 time2 = PerfDataGetCPUTime(IndexParam2);
774 ret = largeintcmp(time1, time2);
775 }
776 else if (TaskManagerSettings.SortColumn == COLUMN_MEMORYUSAGE)
777 {
778 l1 = PerfDataGetWorkingSetSizeBytes(IndexParam1);
779 l2 = PerfDataGetWorkingSetSizeBytes(IndexParam2);
780 ret = CMP(l1, l2);
781 }
782 else if (TaskManagerSettings.SortColumn == COLUMN_PEAKMEMORYUSAGE)
783 {
784 l1 = PerfDataGetPeakWorkingSetSizeBytes(IndexParam1);
785 l2 = PerfDataGetPeakWorkingSetSizeBytes(IndexParam2);
786 ret = CMP(l1, l2);
787 }
788 else if (TaskManagerSettings.SortColumn == COLUMN_MEMORYUSAGEDELTA)
789 {
790 l1 = PerfDataGetWorkingSetSizeDelta(IndexParam1);
791 l2 = PerfDataGetWorkingSetSizeDelta(IndexParam2);
792 ret = CMP(l1, l2);
793 }
794 else if (TaskManagerSettings.SortColumn == COLUMN_PAGEFAULTS)
795 {
796 l1 = PerfDataGetPageFaultCount(IndexParam1);
797 l2 = PerfDataGetPageFaultCount(IndexParam2);
798 ret = CMP(l1, l2);
799 }
800 else if (TaskManagerSettings.SortColumn == COLUMN_PAGEFAULTSDELTA)
801 {
802 l1 = PerfDataGetPageFaultCountDelta(IndexParam1);
803 l2 = PerfDataGetPageFaultCountDelta(IndexParam2);
804 ret = CMP(l1, l2);
805 }
806 else if (TaskManagerSettings.SortColumn == COLUMN_VIRTUALMEMORYSIZE)
807 {
808 l1 = PerfDataGetVirtualMemorySizeBytes(IndexParam1);
809 l2 = PerfDataGetVirtualMemorySizeBytes(IndexParam2);
810 ret = CMP(l1, l2);
811 }
812 else if (TaskManagerSettings.SortColumn == COLUMN_PAGEDPOOL)
813 {
814 l1 = PerfDataGetPagedPoolUsagePages(IndexParam1);
815 l2 = PerfDataGetPagedPoolUsagePages(IndexParam2);
816 ret = CMP(l1, l2);
817 }
818 else if (TaskManagerSettings.SortColumn == COLUMN_NONPAGEDPOOL)
819 {
820 l1 = PerfDataGetNonPagedPoolUsagePages(IndexParam1);
821 l2 = PerfDataGetNonPagedPoolUsagePages(IndexParam2);
822 ret = CMP(l1, l2);
823 }
824 else if (TaskManagerSettings.SortColumn == COLUMN_BASEPRIORITY)
825 {
826 l1 = PerfDataGetBasePriority(IndexParam1);
827 l2 = PerfDataGetBasePriority(IndexParam2);
828 ret = CMP(l1, l2);
829 }
830 else if (TaskManagerSettings.SortColumn == COLUMN_HANDLECOUNT)
831 {
832 l1 = PerfDataGetHandleCount(IndexParam1);
833 l2 = PerfDataGetHandleCount(IndexParam2);
834 ret = CMP(l1, l2);
835 }
836 else if (TaskManagerSettings.SortColumn == COLUMN_THREADCOUNT)
837 {
838 l1 = PerfDataGetThreadCount(IndexParam1);
839 l2 = PerfDataGetThreadCount(IndexParam2);
840 ret = CMP(l1, l2);
841 }
842 else if (TaskManagerSettings.SortColumn == COLUMN_USEROBJECTS)
843 {
844 l1 = PerfDataGetUSERObjectCount(IndexParam1);
845 l2 = PerfDataGetUSERObjectCount(IndexParam2);
846 ret = CMP(l1, l2);
847 }
848 else if (TaskManagerSettings.SortColumn == COLUMN_GDIOBJECTS)
849 {
850 l1 = PerfDataGetGDIObjectCount(IndexParam1);
851 l2 = PerfDataGetGDIObjectCount(IndexParam2);
852 ret = CMP(l1, l2);
853 }
854 else if (TaskManagerSettings.SortColumn == COLUMN_IOREADS)
855 {
856 PerfDataGetIOCounters(IndexParam1, &iocounters1);
857 PerfDataGetIOCounters(IndexParam2, &iocounters2);
858 ull1 = iocounters1.ReadOperationCount;
859 ull2 = iocounters2.ReadOperationCount;
860 ret = CMP(ull1, ull2);
861 }
862 else if (TaskManagerSettings.SortColumn == COLUMN_IOWRITES)
863 {
864 PerfDataGetIOCounters(IndexParam1, &iocounters1);
865 PerfDataGetIOCounters(IndexParam2, &iocounters2);
866 ull1 = iocounters1.WriteOperationCount;
867 ull2 = iocounters2.WriteOperationCount;
868 ret = CMP(ull1, ull2);
869 }
870 else if (TaskManagerSettings.SortColumn == COLUMN_IOOTHER)
871 {
872 PerfDataGetIOCounters(IndexParam1, &iocounters1);
873 PerfDataGetIOCounters(IndexParam2, &iocounters2);
874 ull1 = iocounters1.OtherOperationCount;
875 ull2 = iocounters2.OtherOperationCount;
876 ret = CMP(ull1, ull2);
877 }
878 else if (TaskManagerSettings.SortColumn == COLUMN_IOREADBYTES)
879 {
880 PerfDataGetIOCounters(IndexParam1, &iocounters1);
881 PerfDataGetIOCounters(IndexParam2, &iocounters2);
882 ull1 = iocounters1.ReadTransferCount;
883 ull2 = iocounters2.ReadTransferCount;
884 ret = CMP(ull1, ull2);
885 }
886 else if (TaskManagerSettings.SortColumn == COLUMN_IOWRITEBYTES)
887 {
888 PerfDataGetIOCounters(IndexParam1, &iocounters1);
889 PerfDataGetIOCounters(IndexParam2, &iocounters2);
890 ull1 = iocounters1.WriteTransferCount;
891 ull2 = iocounters2.WriteTransferCount;
892 ret = CMP(ull1, ull2);
893 }
894 else if (TaskManagerSettings.SortColumn == COLUMN_IOOTHERBYTES)
895 {
896 PerfDataGetIOCounters(IndexParam1, &iocounters1);
897 PerfDataGetIOCounters(IndexParam2, &iocounters2);
898 ull1 = iocounters1.OtherTransferCount;
899 ull2 = iocounters2.OtherTransferCount;
900 ret = CMP(ull1, ull2);
901 }
902 return ret;
903 }