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