void ProcessPage_OnSetAffinity(void) { LV_ITEM lvitem; ULONG Index; DWORD dwProcessId; TCHAR strErrorText[260]; for (Index=0; Index<(ULONG)ListView_GetItemCount(hProcessPageListCtrl); Index++) { memset(&lvitem, 0, sizeof(LV_ITEM)); lvitem.mask = LVIF_STATE; lvitem.stateMask = LVIS_SELECTED; lvitem.iItem = Index; SendMessage(hProcessPageListCtrl, LVM_GETITEM, 0, (LPARAM) &lvitem); if (lvitem.state & LVIS_SELECTED) break; } dwProcessId = PerfDataGetProcessId(Index); if ((ListView_GetSelectedCount(hProcessPageListCtrl) != 1) || (dwProcessId == 0)) return; hProcessAffinityHandle = OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_SET_INFORMATION, FALSE, dwProcessId); if (!hProcessAffinityHandle) { GetLastErrorText(strErrorText, 260); MessageBox(hMainWnd, strErrorText, _T("Unable to Access or Set Process Affinity"), MB_OK|MB_ICONSTOP); return; } DialogBox(hInst, MAKEINTRESOURCE(IDD_AFFINITY_DIALOG), hMainWnd, AffinityDialogWndProc); if (hProcessAffinityHandle) { CloseHandle(hProcessAffinityHandle); hProcessAffinityHandle = NULL; } }
ULONG DisplayPerfGetProcessIdAndCPUMemoryUsage() { //First get the number of process ULONG iNum = PerfDataGetProcessCount(); ULONG i = 0; ULONG iProcCount = 0; char szProcessName[MAX_PATH + 10] = {0}; char szProcess[MAX_PATH + 10] = {0}; printf("\n%-25s%-15s%-15s%15s","Image Name","PID","CPU","Mem Usage"); printf("\n%-25s%-15s%-15s%15s","----------","---","---","---------"); for(i = 0; i < iNum; ++i) { memset(szProcessName,NULL,(MAX_PATH + 10)); PerfDataGetImageName(i,szProcessName,MAX_PATH); printf("\n%-25s%-15ld%-15ld%15ld K", szProcessName, PerfDataGetProcessId(i), PerfDataGetCPUUsage(i), (PerfDataGetWorkingSetSizeBytes(i)/1024)); } printf("\n\nTotal Processes %ld\n\n",iNum); return iProcCount; }
static DWORD get_selected_pid(void) { LVITEMW lvitem; ULONG Index, Count; DWORD dwProcessId; Count = SendMessageW(hProcessPageListCtrl, LVM_GETITEMCOUNT, 0, 0); for (Index = 0; Index < Count; Index++) { lvitem.mask = LVIF_STATE; lvitem.stateMask = LVIS_SELECTED; lvitem.iItem = Index; lvitem.iSubItem = 0; SendMessageW(hProcessPageListCtrl, LVM_GETITEMW, 0, (LPARAM) &lvitem); if (lvitem.state & LVIS_SELECTED) break; } Count = SendMessageW(hProcessPageListCtrl, LVM_GETSELECTEDCOUNT, 0, 0); dwProcessId = PerfDataGetProcessId(Index); if ((Count != 1) || (dwProcessId == 0)) return 0; return dwProcessId; }
static DWORD get_selected_pid(void) { LVITEM lvitem; ULONG Index; DWORD dwProcessId; for (Index = 0; Index < (ULONG)ListView_GetItemCount(hProcessPageListCtrl); Index++) { lvitem.mask = LVIF_STATE; lvitem.stateMask = LVIS_SELECTED; lvitem.iItem = Index; lvitem.iSubItem = 0; SendMessage(hProcessPageListCtrl, LVM_GETITEM, 0, (LPARAM) &lvitem); if (lvitem.state & LVIS_SELECTED) break; } dwProcessId = PerfDataGetProcessId(Index); if ((ListView_GetSelectedCount(hProcessPageListCtrl) != 1) || (dwProcessId == 0)) return 0; return dwProcessId; }
static void DoSetPriority(DWORD priority) { LVITEMW lvitem; ULONG Index, Count; DWORD dwProcessId; HANDLE hProcess; WCHAR wstrErrorText[256]; WCHAR wszWarnMsg[255]; WCHAR wszWarnTitle[255]; WCHAR wszUnable2Change[255]; LoadStringW(hInst, IDS_PRIORITY_CHANGE_MESSAGE, wszWarnMsg, sizeof(wszWarnMsg)/sizeof(WCHAR)); LoadStringW(hInst, IDS_WARNING_TITLE, wszWarnTitle, sizeof(wszWarnTitle)/sizeof(WCHAR)); LoadStringW(hInst, IDS_PRIORITY_UNABLE2CHANGE, wszUnable2Change, sizeof(wszUnable2Change)/sizeof(WCHAR)); Count = SendMessageW(hProcessPageListCtrl, LVM_GETITEMCOUNT, 0, 0); for (Index=0; Index<Count; Index++) { lvitem.mask = LVIF_STATE; lvitem.stateMask = LVIS_SELECTED; lvitem.iItem = Index; lvitem.iSubItem = 0; SendMessageW(hProcessPageListCtrl, LVM_GETITEMW, 0, (LPARAM)&lvitem); if (lvitem.state & LVIS_SELECTED) break; } Count = SendMessageW(hProcessPageListCtrl, LVM_GETSELECTEDCOUNT, 0, 0); dwProcessId = PerfDataGetProcessId(Index); if ((Count != 1) || (dwProcessId == 0)) return; if (MessageBoxW(hMainWnd, wszWarnMsg, wszWarnTitle, MB_YESNO|MB_ICONWARNING) != IDYES) return; hProcess = OpenProcess(PROCESS_SET_INFORMATION, FALSE, dwProcessId); if (!hProcess) { GetLastErrorText(wstrErrorText, sizeof(wstrErrorText)/sizeof(WCHAR)); MessageBoxW(hMainWnd, wstrErrorText, wszUnable2Change, MB_OK|MB_ICONSTOP); return; } if (!SetPriorityClass(hProcess, priority)) { GetLastErrorText(wstrErrorText, sizeof(wstrErrorText)/sizeof(WCHAR)); MessageBoxW(hMainWnd, wstrErrorText, wszUnable2Change, MB_OK|MB_ICONSTOP); } CloseHandle(hProcess); }
void ProcessPage_OnEndProcessTree(void) { LVITEMW lvitem; ULONG Index; DWORD dwProcessId; HANDLE hProcess; WCHAR wstrErrorText[256]; load_message_strings(); for (Index=0; Index<(ULONG)ListView_GetItemCount(hProcessPageListCtrl); Index++) { lvitem.mask = LVIF_STATE; lvitem.stateMask = LVIS_SELECTED; lvitem.iItem = Index; lvitem.iSubItem = 0; SendMessageW(hProcessPageListCtrl, LVM_GETITEMW, 0, (LPARAM) &lvitem); if (lvitem.state & LVIS_SELECTED) break; } dwProcessId = PerfDataGetProcessId(Index); if ((ListView_GetSelectedCount(hProcessPageListCtrl) != 1) || (dwProcessId == 0)) return; if (MessageBoxW(hMainWnd, wszWarnMsg, wszWarnTitle, MB_YESNO|MB_ICONWARNING) != IDYES) return; hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, dwProcessId); if (!hProcess) { GetLastErrorText(wstrErrorText, sizeof(wstrErrorText)/sizeof(WCHAR)); MessageBoxW(hMainWnd, wstrErrorText,wszUnable2Terminate, MB_OK|MB_ICONSTOP); return; } if (!TerminateProcess(hProcess, 0)) { GetLastErrorText(wstrErrorText, sizeof(wstrErrorText)/sizeof(WCHAR)); MessageBoxW(hMainWnd, wstrErrorText,wszUnable2Terminate, MB_OK|MB_ICONSTOP); } CloseHandle(hProcess); }
static void DoSetPriority(DWORD priority) { LVITEM lvitem; ULONG Index; DWORD dwProcessId; HANDLE hProcess; TCHAR strErrorText[260]; for (Index=0; Index<(ULONG)ListView_GetItemCount(hProcessPageListCtrl); Index++) { lvitem.mask = LVIF_STATE; lvitem.stateMask = LVIS_SELECTED; lvitem.iItem = Index; lvitem.iSubItem = 0; SendMessage(hProcessPageListCtrl, LVM_GETITEM, 0, (LPARAM)&lvitem); if (lvitem.state & LVIS_SELECTED) break; } dwProcessId = PerfDataGetProcessId(Index); if ((ListView_GetSelectedCount(hProcessPageListCtrl) != 1) || (dwProcessId == 0)) return; if (MessageBox(hMainWnd, _T("WARNING: Changing the priority class of this process may\ncause undesired results including system instability. Are you\nsure you want to change the priority class?"), _T("Task Manager Warning"), MB_YESNO|MB_ICONWARNING) != IDYES) return; hProcess = OpenProcess(PROCESS_SET_INFORMATION, FALSE, dwProcessId); if (!hProcess) { GetLastErrorText(strErrorText, 260); MessageBox(hMainWnd, strErrorText, _T("Unable to Change Priority"), MB_OK|MB_ICONSTOP); return; } if (!SetPriorityClass(hProcess, priority)) { GetLastErrorText(strErrorText, 260); MessageBox(hMainWnd, strErrorText, _T("Unable to Change Priority"), MB_OK|MB_ICONSTOP); } CloseHandle(hProcess); }
void AddProcess(ULONG Index) { LPPROCESS_PAGE_LIST_ITEM pData; int i; LV_ITEM item; BOOL bAlreadyInList = FALSE; ULONG pid; pid = PerfDataGetProcessId(Index); /* Check to see if it's already in our list */ for (i=0; i<ListView_GetItemCount(hProcessPageListCtrl); i++) { memset(&item, 0, sizeof(LV_ITEM)); item.mask = LVIF_PARAM; item.iItem = i; (void)ListView_GetItem(hProcessPageListCtrl, &item); pData = (LPPROCESS_PAGE_LIST_ITEM)item.lParam; if (pData->ProcessId == pid) { bAlreadyInList = TRUE; break; } } if (!bAlreadyInList) /* Add */ { pData = (LPPROCESS_PAGE_LIST_ITEM)HeapAlloc(GetProcessHeap(), 0, sizeof(PROCESS_PAGE_LIST_ITEM)); pData->ProcessId = pid; /* Add the item to the list */ memset(&item, 0, sizeof(LV_ITEM)); item.mask = LVIF_TEXT|LVIF_PARAM; item.pszText = LPSTR_TEXTCALLBACK; item.iItem = ListView_GetItemCount(hProcessPageListCtrl); item.lParam = (LPARAM)pData; (void)ListView_InsertItem(hProcessPageListCtrl, &item); } }
BOOL PerfDataGetText(ULONG Index, ULONG ColumnIndex, LPTSTR lpText, int nMaxCount) { IO_COUNTERS iocounters; LARGE_INTEGER time; if (ColumnDataHints[ColumnIndex] == COLUMN_IMAGENAME) PerfDataGetImageName(Index, lpText, nMaxCount); if (ColumnDataHints[ColumnIndex] == COLUMN_PID) wsprintfW(lpText, L"%lu", PerfDataGetProcessId(Index)); if (ColumnDataHints[ColumnIndex] == COLUMN_USERNAME) PerfDataGetUserName(Index, lpText, nMaxCount); if (ColumnDataHints[ColumnIndex] == COLUMN_SESSIONID) wsprintfW(lpText, L"%lu", PerfDataGetSessionId(Index)); if (ColumnDataHints[ColumnIndex] == COLUMN_CPUUSAGE) wsprintfW(lpText, L"%02lu", PerfDataGetCPUUsage(Index)); if (ColumnDataHints[ColumnIndex] == COLUMN_CPUTIME) { DWORD dwHours; DWORD dwMinutes; DWORD dwSeconds; time = PerfDataGetCPUTime(Index); gethmsfromlargeint(time, &dwHours, &dwMinutes, &dwSeconds); wsprintfW(lpText, L"%lu:%02lu:%02lu", dwHours, dwMinutes, dwSeconds); } if (ColumnDataHints[ColumnIndex] == COLUMN_MEMORYUSAGE) { wsprintfW(lpText, L"%lu", PerfDataGetWorkingSetSizeBytes(Index) / 1024); CommaSeparateNumberString(lpText, nMaxCount); wcscat(lpText, L" K"); } if (ColumnDataHints[ColumnIndex] == COLUMN_PEAKMEMORYUSAGE) { wsprintfW(lpText, L"%lu", PerfDataGetPeakWorkingSetSizeBytes(Index) / 1024); CommaSeparateNumberString(lpText, nMaxCount); wcscat(lpText, L" K"); } if (ColumnDataHints[ColumnIndex] == COLUMN_MEMORYUSAGEDELTA) { wsprintfW(lpText, L"%lu", PerfDataGetWorkingSetSizeDelta(Index) / 1024); CommaSeparateNumberString(lpText, nMaxCount); wcscat(lpText, L" K"); } if (ColumnDataHints[ColumnIndex] == COLUMN_PAGEFAULTS) { wsprintfW(lpText, L"%lu", PerfDataGetPageFaultCount(Index)); CommaSeparateNumberString(lpText, nMaxCount); } if (ColumnDataHints[ColumnIndex] == COLUMN_PAGEFAULTSDELTA) { wsprintfW(lpText, L"%lu", PerfDataGetPageFaultCountDelta(Index)); CommaSeparateNumberString(lpText, nMaxCount); } if (ColumnDataHints[ColumnIndex] == COLUMN_VIRTUALMEMORYSIZE) { wsprintfW(lpText, L"%lu", PerfDataGetVirtualMemorySizeBytes(Index) / 1024); CommaSeparateNumberString(lpText, nMaxCount); wcscat(lpText, L" K"); } if (ColumnDataHints[ColumnIndex] == COLUMN_PAGEDPOOL) { wsprintfW(lpText, L"%lu", PerfDataGetPagedPoolUsagePages(Index) / 1024); CommaSeparateNumberString(lpText, nMaxCount); wcscat(lpText, L" K"); } if (ColumnDataHints[ColumnIndex] == COLUMN_NONPAGEDPOOL) { wsprintfW(lpText, L"%lu", PerfDataGetNonPagedPoolUsagePages(Index) / 1024); CommaSeparateNumberString(lpText, nMaxCount); wcscat(lpText, L" K"); } if (ColumnDataHints[ColumnIndex] == COLUMN_BASEPRIORITY) wsprintfW(lpText, L"%lu", PerfDataGetBasePriority(Index)); if (ColumnDataHints[ColumnIndex] == COLUMN_HANDLECOUNT) { wsprintfW(lpText, L"%lu", PerfDataGetHandleCount(Index)); CommaSeparateNumberString(lpText, nMaxCount); } if (ColumnDataHints[ColumnIndex] == COLUMN_THREADCOUNT) { wsprintfW(lpText, L"%lu", PerfDataGetThreadCount(Index)); CommaSeparateNumberString(lpText, nMaxCount); } if (ColumnDataHints[ColumnIndex] == COLUMN_USEROBJECTS) { wsprintfW(lpText, L"%lu", PerfDataGetUSERObjectCount(Index)); CommaSeparateNumberString(lpText, nMaxCount); } if (ColumnDataHints[ColumnIndex] == COLUMN_GDIOBJECTS) { wsprintfW(lpText, L"%lu", PerfDataGetGDIObjectCount(Index)); CommaSeparateNumberString(lpText, nMaxCount); } if (ColumnDataHints[ColumnIndex] == COLUMN_IOREADS) { PerfDataGetIOCounters(Index, &iocounters); /* wsprintfW(pnmdi->item.pszText, L"%d", iocounters.ReadOperationCount); */ _ui64tow(iocounters.ReadOperationCount, lpText, 10); CommaSeparateNumberString(lpText, nMaxCount); } if (ColumnDataHints[ColumnIndex] == COLUMN_IOWRITES) { PerfDataGetIOCounters(Index, &iocounters); /* wsprintfW(pnmdi->item.pszText, L"%d", iocounters.WriteOperationCount); */ _ui64tow(iocounters.WriteOperationCount, lpText, 10); CommaSeparateNumberString(lpText, nMaxCount); } if (ColumnDataHints[ColumnIndex] == COLUMN_IOOTHER) { PerfDataGetIOCounters(Index, &iocounters); /* wsprintfW(pnmdi->item.pszText, L"%d", iocounters.OtherOperationCount); */ _ui64tow(iocounters.OtherOperationCount, lpText, 10); CommaSeparateNumberString(lpText, nMaxCount); } if (ColumnDataHints[ColumnIndex] == COLUMN_IOREADBYTES) { PerfDataGetIOCounters(Index, &iocounters); /* wsprintfW(pnmdi->item.pszText, L"%d", iocounters.ReadTransferCount); */ _ui64tow(iocounters.ReadTransferCount, lpText, 10); CommaSeparateNumberString(lpText, nMaxCount); } if (ColumnDataHints[ColumnIndex] == COLUMN_IOWRITEBYTES) { PerfDataGetIOCounters(Index, &iocounters); /* wsprintfW(pnmdi->item.pszText, L"%d", iocounters.WriteTransferCount); */ _ui64tow(iocounters.WriteTransferCount, lpText, 10); CommaSeparateNumberString(lpText, nMaxCount); } if (ColumnDataHints[ColumnIndex] == COLUMN_IOOTHERBYTES) { PerfDataGetIOCounters(Index, &iocounters); /* wsprintfW(pnmdi->item.pszText, L"%d", iocounters.OtherTransferCount); */ _ui64tow(iocounters.OtherTransferCount, lpText, 10); CommaSeparateNumberString(lpText, nMaxCount); } return FALSE; }
void ProcessPage_OnDebug(void) { LVITEMW lvitem; ULONG Index; DWORD dwProcessId; WCHAR wstrErrorText[256]; HKEY hKey; WCHAR wstrDebugPath[256]; WCHAR wstrDebugger[256]; DWORD dwDebuggerSize; PROCESS_INFORMATION pi; STARTUPINFOW si; HANDLE hDebugEvent; WCHAR wszWarnTitle[255]; WCHAR wszUnable2Debug[255]; WCHAR wszWarnMsg[255]; static const WCHAR wszSubKey[] = {'S','o','f','t','w','a','r','e','\\', 'M','i','c','r','o','s','o','f','t','\\', 'W','i','n','d','o','w','s',' ','N','T','\\', 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\', 'A','e','D','e','b','u','g',0}; static const WCHAR wszDebugger[] = {'D','e','b','u','g','g','e','r',0}; LoadStringW(hInst, IDS_WARNING_TITLE, wszWarnTitle, sizeof(wszWarnTitle)/sizeof(WCHAR)); LoadStringW(hInst, IDS_DEBUG_UNABLE2DEBUG, wszUnable2Debug, sizeof(wszUnable2Debug)/sizeof(WCHAR)); LoadStringW(hInst, IDS_DEBUG_MESSAGE, wszWarnMsg, sizeof(wszWarnMsg)/sizeof(WCHAR)); for (Index=0; Index<(ULONG)ListView_GetItemCount(hProcessPageListCtrl); Index++) { lvitem.mask = LVIF_STATE; lvitem.stateMask = LVIS_SELECTED; lvitem.iItem = Index; lvitem.iSubItem = 0; SendMessageW(hProcessPageListCtrl, LVM_GETITEMW, 0, (LPARAM) &lvitem); if (lvitem.state & LVIS_SELECTED) break; } dwProcessId = PerfDataGetProcessId(Index); if ((ListView_GetSelectedCount(hProcessPageListCtrl) != 1) || (dwProcessId == 0)) return; if (MessageBoxW(hMainWnd, wszWarnMsg, wszWarnTitle, MB_YESNO|MB_ICONWARNING) != IDYES) { GetLastErrorText(wstrErrorText, sizeof(wstrErrorText)/sizeof(WCHAR)); MessageBoxW(hMainWnd, wstrErrorText, wszUnable2Debug, MB_OK|MB_ICONSTOP); return; } if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, wszSubKey, 0, KEY_READ, &hKey) != ERROR_SUCCESS) { GetLastErrorText(wstrErrorText, sizeof(wstrErrorText)/sizeof(WCHAR)); MessageBoxW(hMainWnd, wstrErrorText, wszUnable2Debug, MB_OK|MB_ICONSTOP); return; } dwDebuggerSize = 260; if (RegQueryValueExW(hKey, wszDebugger, NULL, NULL, (LPBYTE)wstrDebugger, &dwDebuggerSize) != ERROR_SUCCESS) { GetLastErrorText(wstrErrorText, sizeof(wstrErrorText)/sizeof(WCHAR)); MessageBoxW(hMainWnd, wstrErrorText, wszUnable2Debug, MB_OK|MB_ICONSTOP); RegCloseKey(hKey); return; } RegCloseKey(hKey); hDebugEvent = CreateEvent(NULL, FALSE, FALSE, NULL); if (!hDebugEvent) { GetLastErrorText(wstrErrorText, sizeof(wstrErrorText)/sizeof(WCHAR)); MessageBoxW(hMainWnd, wstrErrorText, wszUnable2Debug, MB_OK|MB_ICONSTOP); return; } wsprintfW(wstrDebugPath, wstrDebugger, dwProcessId, hDebugEvent); memset(&pi, 0, sizeof(PROCESS_INFORMATION)); memset(&si, 0, sizeof(STARTUPINFOW)); si.cb = sizeof(STARTUPINFOW); if (!CreateProcessW(NULL, wstrDebugPath, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) { GetLastErrorText(wstrErrorText, sizeof(wstrErrorText)/sizeof(WCHAR)); MessageBoxW(hMainWnd, wstrErrorText, wszUnable2Debug, MB_OK|MB_ICONSTOP); } CloseHandle(hDebugEvent); }
ULONG PerfGetProcessIdAndCPUMemoryUsage(LPTSTR lpImageName, ULONG* plProcId, ULONG* plCPUUsage, ULONG* plMemoryUsage) { ULONG iProcCount = 0; try { //First get the number of process ULONG iNum = PerfDataGetProcessCount(); ULONG i = 0; char szProcessName[MAX_PATH + 10] = {0}; char szProcess[MAX_PATH + 10] = {0}; char* pszFindChar = NULL; pszFindChar = strchr(lpImageName,'.'); if(NULL != pszFindChar) { *pszFindChar = NULL; lstrcpy(szProcess,lpImageName); *pszFindChar = '.'; } for(i = 0; i < iNum; ++i) { memset(szProcessName,NULL,(MAX_PATH + 10)); PerfDataGetImageName(i,szProcessName,MAX_PATH); if(0 == lstrcmpi(lpImageName,szProcessName)) { plProcId[iProcCount] = PerfDataGetProcessId(i); plCPUUsage[iProcCount] = PerfDataGetCPUUsage(i); plMemoryUsage[iProcCount] = PerfDataGetWorkingSetSizeBytes(i); ++iProcCount; } else { if(NULL != szProcess[0]) { if(0 == lstrcmpi(szProcess,szProcessName)) { plProcId[iProcCount] = PerfDataGetProcessId(i); plCPUUsage[iProcCount] = PerfDataGetCPUUsage(i); plMemoryUsage[iProcCount] = PerfDataGetWorkingSetSizeBytes(i); ++iProcCount; } else { pszFindChar = strchr(szProcessName,'.'); if(NULL != pszFindChar) { *pszFindChar = NULL; if(0 == lstrcmpi(szProcess,szProcessName)) { plProcId[iProcCount] = PerfDataGetProcessId(i); plCPUUsage[iProcCount] = PerfDataGetCPUUsage(i); plMemoryUsage[iProcCount] = PerfDataGetWorkingSetSizeBytes(i); ++iProcCount; } } } } } } } catch(...) { } return iProcCount; }
static void ProcessPageOnNotify(WPARAM wParam, LPARAM lParam) { int idctrl; LPNMHDR pnmh; LPNMLISTVIEW pnmv; NMLVDISPINFO* pnmdi; LPNMHEADER pnmhdr; LVITEM lvitem; ULONG Index; ULONG ColumnIndex; IO_COUNTERS iocounters; TIME time; idctrl = (int) wParam; pnmh = (LPNMHDR) lParam; pnmv = (LPNMLISTVIEW) lParam; pnmdi = (NMLVDISPINFO*) lParam; pnmhdr = (LPNMHEADER) lParam; if (pnmh->hwndFrom == hProcessPageListCtrl) { switch (pnmh->code) { #if 0 case LVN_ITEMCHANGED: ProcessPageUpdate(); break; #endif case LVN_GETDISPINFO: if (!(pnmdi->item.mask & LVIF_TEXT)) break; ColumnIndex = pnmdi->item.iSubItem; Index = pnmdi->item.iItem; if (ColumnDataHints[ColumnIndex] == COLUMN_IMAGENAME) PerfDataGetImageName(Index, pnmdi->item.pszText, pnmdi->item.cchTextMax); if (ColumnDataHints[ColumnIndex] == COLUMN_PID) wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetProcessId(Index)); if (ColumnDataHints[ColumnIndex] == COLUMN_USERNAME) PerfDataGetUserName(Index, pnmdi->item.pszText, pnmdi->item.cchTextMax); if (ColumnDataHints[ColumnIndex] == COLUMN_SESSIONID) wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetSessionId(Index)); if (ColumnDataHints[ColumnIndex] == COLUMN_CPUUSAGE) wsprintf(pnmdi->item.pszText, _T("%02d"), PerfDataGetCPUUsage(Index)); if (ColumnDataHints[ColumnIndex] == COLUMN_CPUTIME) { DWORD dwHours; DWORD dwMinutes; DWORD dwSeconds; ULONGLONG secs; time = PerfDataGetCPUTime(Index); secs = time.QuadPart / 10000000; dwHours = secs / 3600; dwMinutes = (secs % 3600) / 60; dwSeconds = (secs % 3600) % 60; wsprintf(pnmdi->item.pszText, _T("%d:%02d:%02d"), dwHours, dwMinutes, dwSeconds); } if (ColumnDataHints[ColumnIndex] == COLUMN_MEMORYUSAGE) { wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetWorkingSetSizeBytes(Index) / 1024); CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax); _tcscat(pnmdi->item.pszText, _T(" K")); } if (ColumnDataHints[ColumnIndex] == COLUMN_PEAKMEMORYUSAGE) { wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetPeakWorkingSetSizeBytes(Index) / 1024); CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax); _tcscat(pnmdi->item.pszText, _T(" K")); } if (ColumnDataHints[ColumnIndex] == COLUMN_MEMORYUSAGEDELTA) { wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetWorkingSetSizeDelta(Index) / 1024); CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax); _tcscat(pnmdi->item.pszText, _T(" K")); } if (ColumnDataHints[ColumnIndex] == COLUMN_PAGEFAULTS) { wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetPageFaultCount(Index)); CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax); } if (ColumnDataHints[ColumnIndex] == COLUMN_PAGEFAULTSDELTA) { wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetPageFaultCountDelta(Index)); CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax); } if (ColumnDataHints[ColumnIndex] == COLUMN_VIRTUALMEMORYSIZE) { wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetVirtualMemorySizeBytes(Index) / 1024); CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax); _tcscat(pnmdi->item.pszText, _T(" K")); } if (ColumnDataHints[ColumnIndex] == COLUMN_PAGEDPOOL) { wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetPagedPoolUsagePages(Index) / 1024); CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax); _tcscat(pnmdi->item.pszText, _T(" K")); } if (ColumnDataHints[ColumnIndex] == COLUMN_NONPAGEDPOOL) { wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetNonPagedPoolUsagePages(Index) / 1024); CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax); _tcscat(pnmdi->item.pszText, _T(" K")); } if (ColumnDataHints[ColumnIndex] == COLUMN_BASEPRIORITY) wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetBasePriority(Index)); if (ColumnDataHints[ColumnIndex] == COLUMN_HANDLECOUNT) { wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetHandleCount(Index)); CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax); } if (ColumnDataHints[ColumnIndex] == COLUMN_THREADCOUNT) { wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetThreadCount(Index)); CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax); } if (ColumnDataHints[ColumnIndex] == COLUMN_USEROBJECTS) { wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetUSERObjectCount(Index)); CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax); } if (ColumnDataHints[ColumnIndex] == COLUMN_GDIOBJECTS) { wsprintf(pnmdi->item.pszText, _T("%d"), PerfDataGetGDIObjectCount(Index)); CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax); } if (ColumnDataHints[ColumnIndex] == COLUMN_IOREADS) { PerfDataGetIOCounters(Index, &iocounters); /* wsprintf(pnmdi->item.pszText, _T("%d"), iocounters.ReadOperationCount); */ #ifdef UNICODE #define _ui64toa _ui64tow #else #endif _ui64toa(iocounters.ReadOperationCount, pnmdi->item.pszText, 10); CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax); } if (ColumnDataHints[ColumnIndex] == COLUMN_IOWRITES) { PerfDataGetIOCounters(Index, &iocounters); /* wsprintf(pnmdi->item.pszText, _T("%d"), iocounters.WriteOperationCount); */ _ui64toa(iocounters.WriteOperationCount, pnmdi->item.pszText, 10); CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax); } if (ColumnDataHints[ColumnIndex] == COLUMN_IOOTHER) { PerfDataGetIOCounters(Index, &iocounters); /* wsprintf(pnmdi->item.pszText, _T("%d"), iocounters.OtherOperationCount); */ _ui64toa(iocounters.OtherOperationCount, pnmdi->item.pszText, 10); CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax); } if (ColumnDataHints[ColumnIndex] == COLUMN_IOREADBYTES) { PerfDataGetIOCounters(Index, &iocounters); /* wsprintf(pnmdi->item.pszText, _T("%d"), iocounters.ReadTransferCount); */ _ui64toa(iocounters.ReadTransferCount, pnmdi->item.pszText, 10); CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax); } if (ColumnDataHints[ColumnIndex] == COLUMN_IOWRITEBYTES) { PerfDataGetIOCounters(Index, &iocounters); /* wsprintf(pnmdi->item.pszText, _T("%d"), iocounters.WriteTransferCount); */ _ui64toa(iocounters.WriteTransferCount, pnmdi->item.pszText, 10); CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax); } if (ColumnDataHints[ColumnIndex] == COLUMN_IOOTHERBYTES) { PerfDataGetIOCounters(Index, &iocounters); /* wsprintf(pnmdi->item.pszText, _T("%d"), iocounters.OtherTransferCount); */ _ui64toa(iocounters.OtherTransferCount, pnmdi->item.pszText, 10); CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax); } break; case NM_RCLICK: for (Index=0; Index<(ULONG)ListView_GetItemCount(hProcessPageListCtrl); Index++) { memset(&lvitem, 0, sizeof(LVITEM)); lvitem.mask = LVIF_STATE; lvitem.stateMask = LVIS_SELECTED; lvitem.iItem = Index; SendMessage(hProcessPageListCtrl, LVM_GETITEM, 0, (LPARAM) &lvitem); if (lvitem.state & LVIS_SELECTED) break; } if ((ListView_GetSelectedCount(hProcessPageListCtrl) == 1) && (PerfDataGetProcessId(Index) != 0)) { ProcessPageShowContextMenu(PerfDataGetProcessId(Index)); } break; } } else if (pnmh->hwndFrom == hProcessPageHeaderCtrl) { switch (pnmh->code) { case HDN_ITEMCLICK: /* * FIXME: Fix the column sorting * *ListView_SortItems(hApplicationPageListCtrl, ApplicationPageCompareFunc, NULL); *bSortAscending = !bSortAscending; */ break; case HDN_ITEMCHANGED: UpdateColumnDataHints(); break; case HDN_ENDDRAG: UpdateColumnDataHints(); break; } } }
static void ProcessPageOnNotify(LPARAM lParam) { LPNMHDR pnmh; NMLVDISPINFOW* pnmdi; LVITEMW lvitem; ULONG Index, Count; ULONG ColumnIndex; IO_COUNTERS iocounters; TIME time; static const WCHAR wszFmtD[] = {'%','d',0}; static const WCHAR wszFmt02D[] = {'%','0','2','d',0}; static const WCHAR wszUnitK[] = {' ','K',0}; pnmh = (LPNMHDR) lParam; pnmdi = (NMLVDISPINFOW*) lParam; if (pnmh->hwndFrom == hProcessPageListCtrl) { switch (pnmh->code) { #if 0 case LVN_ITEMCHANGED: ProcessPageUpdate(); break; #endif case LVN_GETDISPINFOW: if (!(pnmdi->item.mask & LVIF_TEXT)) break; ColumnIndex = pnmdi->item.iSubItem; Index = pnmdi->item.iItem; if (ColumnDataHints[ColumnIndex] == COLUMN_IMAGENAME) PerfDataGetImageName(Index, pnmdi->item.pszText, pnmdi->item.cchTextMax); if (ColumnDataHints[ColumnIndex] == COLUMN_PID) wsprintfW(pnmdi->item.pszText, wszFmtD, PerfDataGetProcessId(Index)); if (ColumnDataHints[ColumnIndex] == COLUMN_USERNAME) PerfDataGetUserName(Index, pnmdi->item.pszText, pnmdi->item.cchTextMax); if (ColumnDataHints[ColumnIndex] == COLUMN_SESSIONID) wsprintfW(pnmdi->item.pszText, wszFmtD, PerfDataGetSessionId(Index)); if (ColumnDataHints[ColumnIndex] == COLUMN_CPUUSAGE) wsprintfW(pnmdi->item.pszText, wszFmt02D, PerfDataGetCPUUsage(Index)); if (ColumnDataHints[ColumnIndex] == COLUMN_CPUTIME) { DWORD dwHours; DWORD dwMinutes; DWORD dwSeconds; ULONGLONG secs; static const WCHAR timefmt[] = {'%','d',':','%','0','2','d',':','%','0','2','d',0}; time = PerfDataGetCPUTime(Index); secs = time.QuadPart / 10000000; dwHours = secs / 3600; dwMinutes = (secs % 3600) / 60; dwSeconds = (secs % 3600) % 60; wsprintfW(pnmdi->item.pszText, timefmt, dwHours, dwMinutes, dwSeconds); } if (ColumnDataHints[ColumnIndex] == COLUMN_MEMORYUSAGE) { wsprintfW(pnmdi->item.pszText, wszFmtD, PerfDataGetWorkingSetSizeBytes(Index) / 1024); CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax); wcscat(pnmdi->item.pszText, wszUnitK); } if (ColumnDataHints[ColumnIndex] == COLUMN_PEAKMEMORYUSAGE) { wsprintfW(pnmdi->item.pszText, wszFmtD, PerfDataGetPeakWorkingSetSizeBytes(Index) / 1024); CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax); wcscat(pnmdi->item.pszText, wszUnitK); } if (ColumnDataHints[ColumnIndex] == COLUMN_MEMORYUSAGEDELTA) { wsprintfW(pnmdi->item.pszText, wszFmtD, PerfDataGetWorkingSetSizeDelta(Index) / 1024); CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax); wcscat(pnmdi->item.pszText, wszUnitK); } if (ColumnDataHints[ColumnIndex] == COLUMN_PAGEFAULTS) { wsprintfW(pnmdi->item.pszText, wszFmtD, PerfDataGetPageFaultCount(Index)); CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax); } if (ColumnDataHints[ColumnIndex] == COLUMN_PAGEFAULTSDELTA) { wsprintfW(pnmdi->item.pszText, wszFmtD, PerfDataGetPageFaultCountDelta(Index)); CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax); } if (ColumnDataHints[ColumnIndex] == COLUMN_VIRTUALMEMORYSIZE) { wsprintfW(pnmdi->item.pszText, wszFmtD, PerfDataGetVirtualMemorySizeBytes(Index) / 1024); CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax); wcscat(pnmdi->item.pszText, wszUnitK); } if (ColumnDataHints[ColumnIndex] == COLUMN_PAGEDPOOL) { wsprintfW(pnmdi->item.pszText, wszFmtD, PerfDataGetPagedPoolUsagePages(Index) / 1024); CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax); wcscat(pnmdi->item.pszText, wszUnitK); } if (ColumnDataHints[ColumnIndex] == COLUMN_NONPAGEDPOOL) { wsprintfW(pnmdi->item.pszText, wszFmtD, PerfDataGetNonPagedPoolUsagePages(Index) / 1024); CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax); wcscat(pnmdi->item.pszText, wszUnitK); } if (ColumnDataHints[ColumnIndex] == COLUMN_BASEPRIORITY) wsprintfW(pnmdi->item.pszText, wszFmtD, PerfDataGetBasePriority(Index)); if (ColumnDataHints[ColumnIndex] == COLUMN_HANDLECOUNT) { wsprintfW(pnmdi->item.pszText, wszFmtD, PerfDataGetHandleCount(Index)); CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax); } if (ColumnDataHints[ColumnIndex] == COLUMN_THREADCOUNT) { wsprintfW(pnmdi->item.pszText, wszFmtD, PerfDataGetThreadCount(Index)); CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax); } if (ColumnDataHints[ColumnIndex] == COLUMN_USEROBJECTS) { wsprintfW(pnmdi->item.pszText, wszFmtD, PerfDataGetUSERObjectCount(Index)); CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax); } if (ColumnDataHints[ColumnIndex] == COLUMN_GDIOBJECTS) { wsprintfW(pnmdi->item.pszText, wszFmtD, PerfDataGetGDIObjectCount(Index)); CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax); } if (ColumnDataHints[ColumnIndex] == COLUMN_IOREADS) { PerfDataGetIOCounters(Index, &iocounters); /* wsprintfW(pnmdi->item.pszText, wszFmtD, iocounters.ReadOperationCount); */ _ui64tow(iocounters.ReadOperationCount, pnmdi->item.pszText, 10); CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax); } if (ColumnDataHints[ColumnIndex] == COLUMN_IOWRITES) { PerfDataGetIOCounters(Index, &iocounters); /* wsprintfW(pnmdi->item.pszText, wszFmtD, iocounters.WriteOperationCount); */ _ui64tow(iocounters.WriteOperationCount, pnmdi->item.pszText, 10); CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax); } if (ColumnDataHints[ColumnIndex] == COLUMN_IOOTHER) { PerfDataGetIOCounters(Index, &iocounters); /* wsprintfW(pnmdi->item.pszText, wszFmtD, iocounters.OtherOperationCount); */ _ui64tow(iocounters.OtherOperationCount, pnmdi->item.pszText, 10); CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax); } if (ColumnDataHints[ColumnIndex] == COLUMN_IOREADBYTES) { PerfDataGetIOCounters(Index, &iocounters); /* wsprintfW(pnmdi->item.pszText, wszFmtD, iocounters.ReadTransferCount); */ _ui64tow(iocounters.ReadTransferCount, pnmdi->item.pszText, 10); CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax); } if (ColumnDataHints[ColumnIndex] == COLUMN_IOWRITEBYTES) { PerfDataGetIOCounters(Index, &iocounters); /* wsprintfW(pnmdi->item.pszText, wszFmtD, iocounters.WriteTransferCount); */ _ui64tow(iocounters.WriteTransferCount, pnmdi->item.pszText, 10); CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax); } if (ColumnDataHints[ColumnIndex] == COLUMN_IOOTHERBYTES) { PerfDataGetIOCounters(Index, &iocounters); /* wsprintfW(pnmdi->item.pszText, wszFmtD, iocounters.OtherTransferCount); */ _ui64tow(iocounters.OtherTransferCount, pnmdi->item.pszText, 10); CommaSeparateNumberString(pnmdi->item.pszText, pnmdi->item.cchTextMax); } break; case NM_RCLICK: Count = SendMessageW(hProcessPageListCtrl, LVM_GETITEMCOUNT, 0, 0); for (Index=0; Index<Count; Index++) { lvitem.mask = LVIF_STATE; lvitem.stateMask = LVIS_SELECTED; lvitem.iItem = Index; lvitem.iSubItem = 0; SendMessageW(hProcessPageListCtrl, LVM_GETITEMW, 0, (LPARAM) &lvitem); if (lvitem.state & LVIS_SELECTED) break; } if ((SendMessageW(hProcessPageListCtrl, LVM_GETSELECTEDCOUNT, 0, 0) == 1) && (PerfDataGetProcessId(Index) != 0)) { ProcessPageShowContextMenu(PerfDataGetProcessId(Index)); } break; } } else if (pnmh->hwndFrom == hProcessPageHeaderCtrl) { switch (pnmh->code) { case HDN_ITEMCLICKW: /* * FIXME: Fix the column sorting * *ListView_SortItems(hApplicationPageListCtrl, ApplicationPageCompareFunc, NULL); *bSortAscending = !bSortAscending; */ break; case HDN_ITEMCHANGEDW: UpdateColumnDataHints(); break; case HDN_ENDDRAG: UpdateColumnDataHints(); break; } } }
BOOL PerfDataGetCommandLine(ULONG Index, LPWSTR lpCommandLine, ULONG nMaxCount) { static const LPWSTR ellipsis = L"..."; PROCESS_BASIC_INFORMATION pbi = {0}; UNICODE_STRING CommandLineStr = {0}; PVOID ProcessParams = NULL; HANDLE hProcess; ULONG ProcessId; NTSTATUS Status; BOOL result; PCMD_LINE_CACHE new_entry; LPWSTR new_string; PCMD_LINE_CACHE cache = global_cache; /* [A] Search for a string already in cache? If so, use it */ while (cache && cache->pnext != NULL) { if (cache->idx == Index && cache->str != NULL) { /* Found it. Use it, and add some ellipsis at the very end to make it cute */ wcsncpy(lpCommandLine, cache->str, CMD_LINE_MIN(nMaxCount, cache->len)); wcscpy(lpCommandLine + CMD_LINE_MIN(nMaxCount, cache->len) - sizeof(ellipsis)/sizeof(WCHAR), ellipsis); return TRUE; } cache = cache->pnext; } /* [B] We don't; let's allocate and load a value from the process mem... and cache it */ ProcessId = PerfDataGetProcessId(Index); /* Default blank command line in case things don't work out */ wcsncpy(lpCommandLine, L"", nMaxCount); /* Ask for a handle to the target process so that we can read its memory and query stuff */ hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, ProcessId); if (!hProcess) goto cleanup; /* First off, get the ProcessEnvironmentBlock location in that process' address space */ Status = NtQueryInformationProcess(hProcess, 0, &pbi, sizeof(pbi), NULL); if (!NT_SUCCESS(Status)) goto cleanup; /* Then get the PEB.ProcessParameters member pointer */ result = ReadProcessMemory(hProcess, (PVOID)((ULONG_PTR)pbi.PebBaseAddress + FIELD_OFFSET(PEB, ProcessParameters)), &ProcessParams, sizeof(ProcessParams), NULL); if (!result) goto cleanup; /* Then copy the PEB->ProcessParameters.CommandLine member to get the pointer to the string buffer and its size */ result = ReadProcessMemory(hProcess, (PVOID)((ULONG_PTR)ProcessParams + FIELD_OFFSET(RTL_USER_PROCESS_PARAMETERS, CommandLine)), &CommandLineStr, sizeof(CommandLineStr), NULL); if (!result) goto cleanup; /* Allocate the next cache entry and its accompanying string in one go */ new_entry = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(CMD_LINE_CACHE) + CommandLineStr.Length + sizeof(UNICODE_NULL)); if (!new_entry) goto cleanup; new_string = (LPWSTR)((ULONG_PTR)new_entry + sizeof(CMD_LINE_CACHE)); /* Bingo, the command line should be stored there, copy the string from the other process */ result = ReadProcessMemory(hProcess, CommandLineStr.Buffer, new_string, CommandLineStr.Length, NULL); if (!result) { /* Weird, after sucessfully reading the mem of that process various times it fails now, forget it and bail out */ HeapFree(GetProcessHeap(), 0, new_entry); goto cleanup; } /* Add our pointer to the cache... */ new_entry->idx = Index; new_entry->str = new_string; new_entry->len = CommandLineStr.Length; if (!global_cache) global_cache = new_entry; else cache->pnext = new_entry; /* ... and print the buffer for the first time */ wcsncpy(lpCommandLine, new_string, CMD_LINE_MIN(nMaxCount, CommandLineStr.Length)); cleanup: if (hProcess) CloseHandle(hProcess); return TRUE; }