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