Sync with trunk (r48545)
[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
206 return 0;
207 }
208
209 void ProcessPageOnNotify(WPARAM wParam, LPARAM lParam)
210 {
211 int idctrl;
212 LPNMHDR pnmh;
213 LPNMLISTVIEW pnmv;
214 NMLVDISPINFO* pnmdi;
215 LPNMHEADER pnmhdr;
216 ULONG Index;
217 ULONG ColumnIndex;
218 LPPROCESS_PAGE_LIST_ITEM pData;
219
220 idctrl = (int) wParam;
221 pnmh = (LPNMHDR) lParam;
222 pnmv = (LPNMLISTVIEW) lParam;
223 pnmdi = (NMLVDISPINFO*) lParam;
224 pnmhdr = (LPNMHEADER) lParam;
225
226 if (pnmh->hwndFrom == hProcessPageListCtrl)
227 {
228 switch (pnmh->code)
229 {
230 #if 0
231 case LVN_ITEMCHANGED:
232 ProcessPageUpdate();
233 break;
234 #endif
235
236 case LVN_GETDISPINFO:
237
238 if (!(pnmdi->item.mask & LVIF_TEXT))
239 break;
240
241 pData = (LPPROCESS_PAGE_LIST_ITEM)pnmdi->item.lParam;
242 Index = PerfDataGetProcessIndex(pData->ProcessId);
243 ColumnIndex = pnmdi->item.iSubItem;
244
245 PerfDataGetText(Index, ColumnIndex, pnmdi->item.pszText, pnmdi->item.cchTextMax);
246
247 break;
248
249 case NM_RCLICK:
250
251 ProcessPageShowContextMenu(GetSelectedProcessId());
252 break;
253
254 }
255 }
256 else if (pnmh->hwndFrom == hProcessPageHeaderCtrl)
257 {
258 switch (pnmh->code)
259 {
260 case HDN_ITEMCLICK:
261
262 TaskManagerSettings.SortColumn = ColumnDataHints[pnmhdr->iItem];
263 TaskManagerSettings.SortAscending = !TaskManagerSettings.SortAscending;
264 (void)ListView_SortItems(hProcessPageListCtrl, ProcessPageCompareFunc, NULL);
265
266 break;
267
268 case HDN_ITEMCHANGED:
269
270 UpdateColumnDataHints();
271
272 break;
273
274 case HDN_ENDDRAG:
275
276 UpdateColumnDataHints();
277
278 break;
279
280 }
281 }
282 }
283
284 void CommaSeparateNumberString(LPWSTR strNumber, int nMaxCount)
285 {
286 WCHAR temp[260];
287 UINT i, j, k;
288
289 for (i=0,j=0; i<(wcslen(strNumber) % 3); i++, j++)
290 temp[j] = strNumber[i];
291 for (k=0; i<wcslen(strNumber); i++,j++,k++) {
292 if ((k % 3 == 0) && (j > 0))
293 temp[j++] = L',';
294 temp[j] = strNumber[i];
295 }
296 temp[j] = L'\0';
297 wcsncpy(strNumber, temp, nMaxCount);
298 }
299
300 void ProcessPageShowContextMenu(DWORD dwProcessId)
301 {
302 HMENU hMenu;
303 HMENU hSubMenu;
304 HMENU hPriorityMenu;
305 POINT pt;
306 SYSTEM_INFO si;
307 HANDLE hProcess;
308 DWORD dwProcessPriorityClass;
309 WCHAR strDebugger[260];
310 DWORD dwDebuggerSize;
311 HKEY hKey;
312
313 memset(&si, 0, sizeof(SYSTEM_INFO));
314
315 GetCursorPos(&pt);
316 GetSystemInfo(&si);
317
318 hMenu = LoadMenuW(hInst, MAKEINTRESOURCEW(IDR_PROCESS_PAGE_CONTEXT));
319 hSubMenu = GetSubMenu(hMenu, 0);
320 hPriorityMenu = GetSubMenu(hSubMenu, 4);
321
322 hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, dwProcessId);
323 dwProcessPriorityClass = GetPriorityClass(hProcess);
324 CloseHandle(hProcess);
325
326 if (si.dwNumberOfProcessors < 2)
327 RemoveMenu(hSubMenu, ID_PROCESS_PAGE_SETAFFINITY, MF_BYCOMMAND);
328
329 if (!DebugChannelsAreSupported())
330 RemoveMenu(hSubMenu, ID_PROCESS_PAGE_DEBUGCHANNELS, MF_BYCOMMAND);
331
332 switch (dwProcessPriorityClass) {
333 case REALTIME_PRIORITY_CLASS:
334 CheckMenuRadioItem(hPriorityMenu, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, ID_PROCESS_PAGE_SETPRIORITY_LOW, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, MF_BYCOMMAND);
335 break;
336 case HIGH_PRIORITY_CLASS:
337 CheckMenuRadioItem(hPriorityMenu, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, ID_PROCESS_PAGE_SETPRIORITY_LOW, ID_PROCESS_PAGE_SETPRIORITY_HIGH, MF_BYCOMMAND);
338 break;
339 case ABOVE_NORMAL_PRIORITY_CLASS:
340 CheckMenuRadioItem(hPriorityMenu, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, ID_PROCESS_PAGE_SETPRIORITY_LOW, ID_PROCESS_PAGE_SETPRIORITY_ABOVENORMAL, MF_BYCOMMAND);
341 break;
342 case NORMAL_PRIORITY_CLASS:
343 CheckMenuRadioItem(hPriorityMenu, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, ID_PROCESS_PAGE_SETPRIORITY_LOW, ID_PROCESS_PAGE_SETPRIORITY_NORMAL, MF_BYCOMMAND);
344 break;
345 case BELOW_NORMAL_PRIORITY_CLASS:
346 CheckMenuRadioItem(hPriorityMenu, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, ID_PROCESS_PAGE_SETPRIORITY_LOW, ID_PROCESS_PAGE_SETPRIORITY_BELOWNORMAL, MF_BYCOMMAND);
347 break;
348 case IDLE_PRIORITY_CLASS:
349 CheckMenuRadioItem(hPriorityMenu, ID_PROCESS_PAGE_SETPRIORITY_REALTIME, ID_PROCESS_PAGE_SETPRIORITY_LOW, ID_PROCESS_PAGE_SETPRIORITY_LOW, MF_BYCOMMAND);
350 break;
351 }
352
353 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\Windows NT\\CurrentVersion\\AeDebug", 0, KEY_READ, &hKey) == ERROR_SUCCESS)
354 {
355 dwDebuggerSize = 260;
356 if (RegQueryValueExW(hKey, L"Debugger", NULL, NULL, (LPBYTE)strDebugger, &dwDebuggerSize) == ERROR_SUCCESS)
357 {
358 CharUpper(strDebugger);
359 if (wcsstr(strDebugger, L"DRWTSN32"))
360 EnableMenuItem(hSubMenu, ID_PROCESS_PAGE_DEBUG, MF_BYCOMMAND|MF_DISABLED|MF_GRAYED);
361 }
362 else
363 EnableMenuItem(hSubMenu, ID_PROCESS_PAGE_DEBUG, MF_BYCOMMAND|MF_DISABLED|MF_GRAYED);
364
365 RegCloseKey(hKey);
366 } else {
367 EnableMenuItem(hSubMenu, ID_PROCESS_PAGE_DEBUG, MF_BYCOMMAND|MF_DISABLED|MF_GRAYED);
368 }
369 TrackPopupMenu(hSubMenu, TPM_LEFTALIGN|TPM_TOPALIGN|TPM_LEFTBUTTON, pt.x, pt.y, 0, hMainWnd, NULL);
370 DestroyMenu(hMenu);
371 }
372
373 void RefreshProcessPage(void)
374 {
375 #ifdef RUN_PROC_PAGE
376 /* Signal the event so that our refresh thread */
377 /* will wake up and refresh the process page */
378 PostThreadMessage(dwProcessThread, WM_TIMER, 0, 0);
379 #endif
380 }
381
382 DWORD WINAPI ProcessPageRefreshThread(void *lpParameter)
383 {
384 ULONG OldProcessorUsage = 0;
385 ULONG OldProcessCount = 0;
386 WCHAR szCpuUsage[256], szProcesses[256];
387 MSG msg;
388
389 LoadStringW(hInst, IDS_STATUS_CPUUSAGE, szCpuUsage, 256);
390 LoadStringW(hInst, IDS_STATUS_PROCESSES, szProcesses, 256);
391
392 while (1) {
393 /* Wait for an the event or application close */
394 if (GetMessage(&msg, NULL, 0, 0) <= 0)
395 return 0;
396
397 if (msg.message == WM_TIMER) {
398 WCHAR text[260];
399
400 UpdateProcesses();
401
402 if (IsWindowVisible(hProcessPage))
403 InvalidateRect(hProcessPageListCtrl, NULL, FALSE);
404
405 if (OldProcessorUsage != PerfDataGetProcessorUsage()) {
406 OldProcessorUsage = PerfDataGetProcessorUsage();
407 wsprintfW(text, szCpuUsage, OldProcessorUsage);
408 SendMessageW(hStatusWnd, SB_SETTEXT, 1, (LPARAM)text);
409 }
410 if (OldProcessCount != PerfDataGetProcessCount()) {
411 OldProcessCount = PerfDataGetProcessCount();
412 wsprintfW(text, szProcesses, OldProcessCount);
413 SendMessageW(hStatusWnd, SB_SETTEXT, 0, (LPARAM)text);
414 }
415 }
416 }
417 return 0;
418 }
419
420 void UpdateProcesses()
421 {
422 int i;
423 ULONG l;
424 LV_ITEM item;
425 LPPROCESS_PAGE_LIST_ITEM pData;
426
427 /* Remove old processes */
428 for (i = 0; i < ListView_GetItemCount(hProcessPageListCtrl); i++)
429 {
430 memset(&item, 0, sizeof (LV_ITEM));
431 item.mask = LVIF_PARAM;
432 item.iItem = i;
433 (void)ListView_GetItem(hProcessPageListCtrl, &item);
434 pData = (LPPROCESS_PAGE_LIST_ITEM)item.lParam;
435 if (!ProcessRunning(pData->ProcessId))
436 {
437 (void)ListView_DeleteItem(hProcessPageListCtrl, i);
438 HeapFree(GetProcessHeap(), 0, pData);
439 }
440 }
441
442 /* Check for difference in listview process and performance process counts */
443 if (ListView_GetItemCount(hProcessPageListCtrl) != PerfDataGetProcessCount())
444 {
445 /* Add new processes by checking against the current items */
446 for (l = 0; l < PerfDataGetProcessCount(); l++)
447 {
448 AddProcess(l);
449 }
450 }
451
452 if (TaskManagerSettings.SortColumn != -1)
453 {
454 (void)ListView_SortItems(hProcessPageListCtrl, ProcessPageCompareFunc, NULL);
455 }
456 }
457
458 BOOL ProcessRunning(ULONG ProcessId)
459 {
460 HANDLE hProcess;
461 DWORD exitCode;
462
463 if (ProcessId == 0) {
464 return TRUE;
465 }
466
467 hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, ProcessId);
468 if (hProcess == NULL) {
469 return FALSE;
470 }
471
472 if (GetExitCodeProcess(hProcess, &exitCode)) {
473 CloseHandle(hProcess);
474 return (exitCode == STILL_ACTIVE);
475 }
476
477 CloseHandle(hProcess);
478 return FALSE;
479 }
480
481 void AddProcess(ULONG Index)
482 {
483 LPPROCESS_PAGE_LIST_ITEM pData;
484 int i;
485 LV_ITEM item;
486 BOOL bAlreadyInList = FALSE;
487 ULONG pid;
488
489 pid = PerfDataGetProcessId(Index);
490
491 /* Check to see if it's already in our list */
492 for (i=0; i<ListView_GetItemCount(hProcessPageListCtrl); i++)
493 {
494 memset(&item, 0, sizeof(LV_ITEM));
495 item.mask = LVIF_PARAM;
496 item.iItem = i;
497 (void)ListView_GetItem(hProcessPageListCtrl, &item);
498 pData = (LPPROCESS_PAGE_LIST_ITEM)item.lParam;
499 if (pData->ProcessId == pid)
500 {
501 bAlreadyInList = TRUE;
502 break;
503 }
504 }
505 if (!bAlreadyInList) /* Add */
506 {
507 pData = (LPPROCESS_PAGE_LIST_ITEM)HeapAlloc(GetProcessHeap(), 0, sizeof(PROCESS_PAGE_LIST_ITEM));
508 pData->ProcessId = pid;
509
510 /* Add the item to the list */
511 memset(&item, 0, sizeof(LV_ITEM));
512 item.mask = LVIF_TEXT|LVIF_PARAM;
513 item.pszText = LPSTR_TEXTCALLBACK;
514 item.iItem = ListView_GetItemCount(hProcessPageListCtrl);
515 item.lParam = (LPARAM)pData;
516 (void)ListView_InsertItem(hProcessPageListCtrl, &item);
517 }
518 }
519
520 BOOL PerfDataGetText(ULONG Index, ULONG ColumnIndex, LPTSTR lpText, int nMaxCount)
521 {
522 IO_COUNTERS iocounters;
523 LARGE_INTEGER time;
524
525 if (ColumnDataHints[ColumnIndex] == COLUMN_IMAGENAME)
526 PerfDataGetImageName(Index, lpText, nMaxCount);
527 if (ColumnDataHints[ColumnIndex] == COLUMN_PID)
528 wsprintfW(lpText, L"%d", PerfDataGetProcessId(Index));
529 if (ColumnDataHints[ColumnIndex] == COLUMN_USERNAME)
530 PerfDataGetUserName(Index, lpText, nMaxCount);
531 if (ColumnDataHints[ColumnIndex] == COLUMN_SESSIONID)
532 wsprintfW(lpText, L"%d", PerfDataGetSessionId(Index));
533 if (ColumnDataHints[ColumnIndex] == COLUMN_CPUUSAGE)
534 wsprintfW(lpText, L"%02d", PerfDataGetCPUUsage(Index));
535 if (ColumnDataHints[ColumnIndex] == COLUMN_CPUTIME)
536 {
537 DWORD dwHours;
538 DWORD dwMinutes;
539 DWORD dwSeconds;
540
541 time = PerfDataGetCPUTime(Index);
542 gethmsfromlargeint(time, &dwHours, &dwMinutes, &dwSeconds);
543 wsprintfW(lpText, L"%d:%02d:%02d", dwHours, dwMinutes, dwSeconds);
544 }
545 if (ColumnDataHints[ColumnIndex] == COLUMN_MEMORYUSAGE)
546 {
547 wsprintfW(lpText, L"%d", PerfDataGetWorkingSetSizeBytes(Index) / 1024);
548 CommaSeparateNumberString(lpText, nMaxCount);
549 wcscat(lpText, L" K");
550 }
551 if (ColumnDataHints[ColumnIndex] == COLUMN_PEAKMEMORYUSAGE)
552 {
553 wsprintfW(lpText, L"%d", PerfDataGetPeakWorkingSetSizeBytes(Index) / 1024);
554 CommaSeparateNumberString(lpText, nMaxCount);
555 wcscat(lpText, L" K");
556 }
557 if (ColumnDataHints[ColumnIndex] == COLUMN_MEMORYUSAGEDELTA)
558 {
559 wsprintfW(lpText, L"%d", PerfDataGetWorkingSetSizeDelta(Index) / 1024);
560 CommaSeparateNumberString(lpText, nMaxCount);
561 wcscat(lpText, L" K");
562 }
563 if (ColumnDataHints[ColumnIndex] == COLUMN_PAGEFAULTS)
564 {
565 wsprintfW(lpText, L"%d", PerfDataGetPageFaultCount(Index));
566 CommaSeparateNumberString(lpText, nMaxCount);
567 }
568 if (ColumnDataHints[ColumnIndex] == COLUMN_PAGEFAULTSDELTA)
569 {
570 wsprintfW(lpText, L"%d", PerfDataGetPageFaultCountDelta(Index));
571 CommaSeparateNumberString(lpText, nMaxCount);
572 }
573 if (ColumnDataHints[ColumnIndex] == COLUMN_VIRTUALMEMORYSIZE)
574 {
575 wsprintfW(lpText, L"%d", PerfDataGetVirtualMemorySizeBytes(Index) / 1024);
576 CommaSeparateNumberString(lpText, nMaxCount);
577 wcscat(lpText, L" K");
578 }
579 if (ColumnDataHints[ColumnIndex] == COLUMN_PAGEDPOOL)
580 {
581 wsprintfW(lpText, L"%d", PerfDataGetPagedPoolUsagePages(Index) / 1024);
582 CommaSeparateNumberString(lpText, nMaxCount);
583 wcscat(lpText, L" K");
584 }
585 if (ColumnDataHints[ColumnIndex] == COLUMN_NONPAGEDPOOL)
586 {
587 wsprintfW(lpText, L"%d", PerfDataGetNonPagedPoolUsagePages(Index) / 1024);
588 CommaSeparateNumberString(lpText, nMaxCount);
589 wcscat(lpText, L" K");
590 }
591 if (ColumnDataHints[ColumnIndex] == COLUMN_BASEPRIORITY)
592 wsprintfW(lpText, L"%d", PerfDataGetBasePriority(Index));
593 if (ColumnDataHints[ColumnIndex] == COLUMN_HANDLECOUNT)
594 {
595 wsprintfW(lpText, L"%d", PerfDataGetHandleCount(Index));
596 CommaSeparateNumberString(lpText, nMaxCount);
597 }
598 if (ColumnDataHints[ColumnIndex] == COLUMN_THREADCOUNT)
599 {
600 wsprintfW(lpText, L"%d", PerfDataGetThreadCount(Index));
601 CommaSeparateNumberString(lpText, nMaxCount);
602 }
603 if (ColumnDataHints[ColumnIndex] == COLUMN_USEROBJECTS)
604 {
605 wsprintfW(lpText, L"%d", PerfDataGetUSERObjectCount(Index));
606 CommaSeparateNumberString(lpText, nMaxCount);
607 }
608 if (ColumnDataHints[ColumnIndex] == COLUMN_GDIOBJECTS)
609 {
610 wsprintfW(lpText, L"%d", PerfDataGetGDIObjectCount(Index));
611 CommaSeparateNumberString(lpText, nMaxCount);
612 }
613 if (ColumnDataHints[ColumnIndex] == COLUMN_IOREADS)
614 {
615 PerfDataGetIOCounters(Index, &iocounters);
616 /* wsprintfW(pnmdi->item.pszText, L"%d", iocounters.ReadOperationCount); */
617 _ui64tow(iocounters.ReadOperationCount, lpText, 10);
618 CommaSeparateNumberString(lpText, nMaxCount);
619 }
620 if (ColumnDataHints[ColumnIndex] == COLUMN_IOWRITES)
621 {
622 PerfDataGetIOCounters(Index, &iocounters);
623 /* wsprintfW(pnmdi->item.pszText, L"%d", iocounters.WriteOperationCount); */
624 _ui64tow(iocounters.WriteOperationCount, lpText, 10);
625 CommaSeparateNumberString(lpText, nMaxCount);
626 }
627 if (ColumnDataHints[ColumnIndex] == COLUMN_IOOTHER)
628 {
629 PerfDataGetIOCounters(Index, &iocounters);
630 /* wsprintfW(pnmdi->item.pszText, L"%d", iocounters.OtherOperationCount); */
631 _ui64tow(iocounters.OtherOperationCount, lpText, 10);
632 CommaSeparateNumberString(lpText, nMaxCount);
633 }
634 if (ColumnDataHints[ColumnIndex] == COLUMN_IOREADBYTES)
635 {
636 PerfDataGetIOCounters(Index, &iocounters);
637 /* wsprintfW(pnmdi->item.pszText, L"%d", iocounters.ReadTransferCount); */
638 _ui64tow(iocounters.ReadTransferCount, lpText, 10);
639 CommaSeparateNumberString(lpText, nMaxCount);
640 }
641 if (ColumnDataHints[ColumnIndex] == COLUMN_IOWRITEBYTES)
642 {
643 PerfDataGetIOCounters(Index, &iocounters);
644 /* wsprintfW(pnmdi->item.pszText, L"%d", iocounters.WriteTransferCount); */
645 _ui64tow(iocounters.WriteTransferCount, lpText, 10);
646 CommaSeparateNumberString(lpText, nMaxCount);
647 }
648 if (ColumnDataHints[ColumnIndex] == COLUMN_IOOTHERBYTES)
649 {
650 PerfDataGetIOCounters(Index, &iocounters);
651 /* wsprintfW(pnmdi->item.pszText, L"%d", iocounters.OtherTransferCount); */
652 _ui64tow(iocounters.OtherTransferCount, lpText, 10);
653 CommaSeparateNumberString(lpText, nMaxCount);
654 }
655
656 return FALSE;
657 }
658
659
660 void gethmsfromlargeint(LARGE_INTEGER largeint, DWORD *dwHours, DWORD *dwMinutes, DWORD *dwSeconds)
661 {
662 #ifdef _MSC_VER
663 *dwHours = (DWORD)(largeint.QuadPart / 36000000000L);
664 *dwMinutes = (DWORD)((largeint.QuadPart % 36000000000L) / 600000000L);
665 *dwSeconds = (DWORD)(((largeint.QuadPart % 36000000000L) % 600000000L) / 10000000L);
666 #else
667 *dwHours = (DWORD)(largeint.QuadPart / 36000000000LL);
668 *dwMinutes = (DWORD)((largeint.QuadPart % 36000000000LL) / 600000000LL);
669 *dwSeconds = (DWORD)(((largeint.QuadPart % 36000000000LL) % 600000000LL) / 10000000LL);
670 #endif
671 }
672
673 int largeintcmp(LARGE_INTEGER l1, LARGE_INTEGER l2)
674 {
675 int ret = 0;
676 DWORD dwHours1;
677 DWORD dwMinutes1;
678 DWORD dwSeconds1;
679 DWORD dwHours2;
680 DWORD dwMinutes2;
681 DWORD dwSeconds2;
682
683 gethmsfromlargeint(l1, &dwHours1, &dwMinutes1, &dwSeconds1);
684 gethmsfromlargeint(l2, &dwHours2, &dwMinutes2, &dwSeconds2);
685 ret = CMP(dwHours1, dwHours2);
686 if (ret == 0)
687 {
688 ret = CMP(dwMinutes1, dwMinutes2);
689 if (ret == 0)
690 {
691 ret = CMP(dwSeconds1, dwSeconds2);
692 }
693 }
694 return ret;
695 }
696
697 int CALLBACK ProcessPageCompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
698 {
699 int ret = 0;
700 LPPROCESS_PAGE_LIST_ITEM Param1;
701 LPPROCESS_PAGE_LIST_ITEM Param2;
702 ULONG IndexParam1;
703 ULONG IndexParam2;
704 WCHAR text1[260];
705 WCHAR text2[260];
706 ULONG l1;
707 ULONG l2;
708 LARGE_INTEGER time1;
709 LARGE_INTEGER time2;
710 IO_COUNTERS iocounters1;
711 IO_COUNTERS iocounters2;
712 ULONGLONG ull1;
713 ULONGLONG ull2;
714
715 if (TaskManagerSettings.SortAscending) {
716 Param1 = (LPPROCESS_PAGE_LIST_ITEM)lParam1;
717 Param2 = (LPPROCESS_PAGE_LIST_ITEM)lParam2;
718 } else {
719 Param1 = (LPPROCESS_PAGE_LIST_ITEM)lParam2;
720 Param2 = (LPPROCESS_PAGE_LIST_ITEM)lParam1;
721 }
722 IndexParam1 = PerfDataGetProcessIndex(Param1->ProcessId);
723 IndexParam2 = PerfDataGetProcessIndex(Param2->ProcessId);
724
725 if (TaskManagerSettings.SortColumn == COLUMN_IMAGENAME)
726 {
727 PerfDataGetImageName(IndexParam1, text1, sizeof (text1) / sizeof (*text1));
728 PerfDataGetImageName(IndexParam2, text2, sizeof (text2) / sizeof (*text2));
729 ret = _wcsicmp(text1, text2);
730 }
731 else if (TaskManagerSettings.SortColumn == COLUMN_PID)
732 {
733 l1 = Param1->ProcessId;
734 l2 = Param2->ProcessId;
735 ret = CMP(l1, l2);
736 }
737 else if (TaskManagerSettings.SortColumn == COLUMN_USERNAME)
738 {
739 PerfDataGetUserName(IndexParam1, text1, sizeof (text1) / sizeof (*text1));
740 PerfDataGetUserName(IndexParam2, text2, sizeof (text2) / sizeof (*text2));
741 ret = _wcsicmp(text1, text2);
742 }
743 else if (TaskManagerSettings.SortColumn == COLUMN_SESSIONID)
744 {
745 l1 = PerfDataGetSessionId(IndexParam1);
746 l2 = PerfDataGetSessionId(IndexParam2);
747 ret = CMP(l1, l2);
748 }
749 else if (TaskManagerSettings.SortColumn == COLUMN_CPUUSAGE)
750 {
751 l1 = PerfDataGetCPUUsage(IndexParam1);
752 l2 = PerfDataGetCPUUsage(IndexParam2);
753 ret = CMP(l1, l2);
754 }
755 else if (TaskManagerSettings.SortColumn == COLUMN_CPUTIME)
756 {
757 time1 = PerfDataGetCPUTime(IndexParam1);
758 time2 = PerfDataGetCPUTime(IndexParam2);
759 ret = largeintcmp(time1, time2);
760 }
761 else if (TaskManagerSettings.SortColumn == COLUMN_MEMORYUSAGE)
762 {
763 l1 = PerfDataGetWorkingSetSizeBytes(IndexParam1);
764 l2 = PerfDataGetWorkingSetSizeBytes(IndexParam2);
765 ret = CMP(l1, l2);
766 }
767 else if (TaskManagerSettings.SortColumn == COLUMN_PEAKMEMORYUSAGE)
768 {
769 l1 = PerfDataGetPeakWorkingSetSizeBytes(IndexParam1);
770 l2 = PerfDataGetPeakWorkingSetSizeBytes(IndexParam2);
771 ret = CMP(l1, l2);
772 }
773 else if (TaskManagerSettings.SortColumn == COLUMN_MEMORYUSAGEDELTA)
774 {
775 l1 = PerfDataGetWorkingSetSizeDelta(IndexParam1);
776 l2 = PerfDataGetWorkingSetSizeDelta(IndexParam2);
777 ret = CMP(l1, l2);
778 }
779 else if (TaskManagerSettings.SortColumn == COLUMN_PAGEFAULTS)
780 {
781 l1 = PerfDataGetPageFaultCount(IndexParam1);
782 l2 = PerfDataGetPageFaultCount(IndexParam2);
783 ret = CMP(l1, l2);
784 }
785 else if (TaskManagerSettings.SortColumn == COLUMN_PAGEFAULTSDELTA)
786 {
787 l1 = PerfDataGetPageFaultCountDelta(IndexParam1);
788 l2 = PerfDataGetPageFaultCountDelta(IndexParam2);
789 ret = CMP(l1, l2);
790 }
791 else if (TaskManagerSettings.SortColumn == COLUMN_VIRTUALMEMORYSIZE)
792 {
793 l1 = PerfDataGetVirtualMemorySizeBytes(IndexParam1);
794 l2 = PerfDataGetVirtualMemorySizeBytes(IndexParam2);
795 ret = CMP(l1, l2);
796 }
797 else if (TaskManagerSettings.SortColumn == COLUMN_PAGEDPOOL)
798 {
799 l1 = PerfDataGetPagedPoolUsagePages(IndexParam1);
800 l2 = PerfDataGetPagedPoolUsagePages(IndexParam2);
801 ret = CMP(l1, l2);
802 }
803 else if (TaskManagerSettings.SortColumn == COLUMN_NONPAGEDPOOL)
804 {
805 l1 = PerfDataGetNonPagedPoolUsagePages(IndexParam1);
806 l2 = PerfDataGetNonPagedPoolUsagePages(IndexParam2);
807 ret = CMP(l1, l2);
808 }
809 else if (TaskManagerSettings.SortColumn == COLUMN_BASEPRIORITY)
810 {
811 l1 = PerfDataGetBasePriority(IndexParam1);
812 l2 = PerfDataGetBasePriority(IndexParam2);
813 ret = CMP(l1, l2);
814 }
815 else if (TaskManagerSettings.SortColumn == COLUMN_HANDLECOUNT)
816 {
817 l1 = PerfDataGetHandleCount(IndexParam1);
818 l2 = PerfDataGetHandleCount(IndexParam2);
819 ret = CMP(l1, l2);
820 }
821 else if (TaskManagerSettings.SortColumn == COLUMN_THREADCOUNT)
822 {
823 l1 = PerfDataGetThreadCount(IndexParam1);
824 l2 = PerfDataGetThreadCount(IndexParam2);
825 ret = CMP(l1, l2);
826 }
827 else if (TaskManagerSettings.SortColumn == COLUMN_USEROBJECTS)
828 {
829 l1 = PerfDataGetUSERObjectCount(IndexParam1);
830 l2 = PerfDataGetUSERObjectCount(IndexParam2);
831 ret = CMP(l1, l2);
832 }
833 else if (TaskManagerSettings.SortColumn == COLUMN_GDIOBJECTS)
834 {
835 l1 = PerfDataGetGDIObjectCount(IndexParam1);
836 l2 = PerfDataGetGDIObjectCount(IndexParam2);
837 ret = CMP(l1, l2);
838 }
839 else if (TaskManagerSettings.SortColumn == COLUMN_IOREADS)
840 {
841 PerfDataGetIOCounters(IndexParam1, &iocounters1);
842 PerfDataGetIOCounters(IndexParam2, &iocounters2);
843 ull1 = iocounters1.ReadOperationCount;
844 ull2 = iocounters2.ReadOperationCount;
845 ret = CMP(ull1, ull2);
846 }
847 else if (TaskManagerSettings.SortColumn == COLUMN_IOWRITES)
848 {
849 PerfDataGetIOCounters(IndexParam1, &iocounters1);
850 PerfDataGetIOCounters(IndexParam2, &iocounters2);
851 ull1 = iocounters1.WriteOperationCount;
852 ull2 = iocounters2.WriteOperationCount;
853 ret = CMP(ull1, ull2);
854 }
855 else if (TaskManagerSettings.SortColumn == COLUMN_IOOTHER)
856 {
857 PerfDataGetIOCounters(IndexParam1, &iocounters1);
858 PerfDataGetIOCounters(IndexParam2, &iocounters2);
859 ull1 = iocounters1.OtherOperationCount;
860 ull2 = iocounters2.OtherOperationCount;
861 ret = CMP(ull1, ull2);
862 }
863 else if (TaskManagerSettings.SortColumn == COLUMN_IOREADBYTES)
864 {
865 PerfDataGetIOCounters(IndexParam1, &iocounters1);
866 PerfDataGetIOCounters(IndexParam2, &iocounters2);
867 ull1 = iocounters1.ReadTransferCount;
868 ull2 = iocounters2.ReadTransferCount;
869 ret = CMP(ull1, ull2);
870 }
871 else if (TaskManagerSettings.SortColumn == COLUMN_IOWRITEBYTES)
872 {
873 PerfDataGetIOCounters(IndexParam1, &iocounters1);
874 PerfDataGetIOCounters(IndexParam2, &iocounters2);
875 ull1 = iocounters1.WriteTransferCount;
876 ull2 = iocounters2.WriteTransferCount;
877 ret = CMP(ull1, ull2);
878 }
879 else if (TaskManagerSettings.SortColumn == COLUMN_IOOTHERBYTES)
880 {
881 PerfDataGetIOCounters(IndexParam1, &iocounters1);
882 PerfDataGetIOCounters(IndexParam2, &iocounters2);
883 ull1 = iocounters1.OtherTransferCount;
884 ull2 = iocounters2.OtherTransferCount;
885 ret = CMP(ull1, ull2);
886 }
887 return ret;
888 }