📄 processlist.cpp
字号:
/************************************************************************
* 文件名称:ProcessList.cpp
* 作 者:李骥
*************************************************************************/
#include "stdafx.h"
#include "processlist.h"
HWND CreateListView (HWND hWndParent,int nCmdShow)
{
HWND hWndListView;
RECT rcl;
INITCOMMONCONTROLSEX icex;
// Ensure that the common control DLL is loaded.
icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
icex.dwICC = ICC_LISTVIEW_CLASSES;
InitCommonControlsEx(&icex);
// Create the list-view window in report view with label
// editing enabled.
GetClientRect (hWndParent, &rcl);
hWndListView = CreateWindow (WC_LISTVIEW, L"",
WS_CHILD | LVS_REPORT | LVS_EDITLABELS,
0, 0, rcl.right - rcl.left, rcl.bottom -
rcl.top,
hWndParent,NULL /*(HMENU) ID_LISTVIEW*/, NULL, NULL);
if (hWndListView == NULL)
return NULL;
ShowWindow(hWndListView, nCmdShow);
return hWndListView;
}
// InitListViewColumns_Process - adds columns to a list-view
// control.
// Returns TRUE if successful, or FALSE otherwise.
// hWndListView - handle to the list-view control.
BOOL InitListViewColumns_Process(HWND hWndListView)
{
WCHAR szText[9][256]={L"Priority Class",L"Priority Base",L"PARENT PROCESS ID",L"THREAD COUNT",L"PROCESS ID",L"PROCESS NAME"}; // temporary buffer
LVCOLUMN lvc;
memset(&lvc,0,sizeof(lvc));
int iCol;
// Initialize the LVCOLUMN structure.
// The mask specifies that the format, width, text, and
// subitem members of the structure are valid.
lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT |
LVCF_SUBITEM;
// Add the columns.
for (iCol = 0; iCol < 6/*C_COLUMNS*/; iCol++)
{
lvc.iSubItem = iCol;
lvc.pszText = szText[iCol];
lvc.cx = 100; // width of column in pixels
/*LoadString(hInst, IDS_FIRSTCOLUMN + iCol,
szText, sizeof(szText)/sizeof(szText[0]));*/
SendMessage(hWndListView,LVM_INSERTCOLUMN,0,(LPARAM)&lvc);
}
return TRUE;
}
BOOL InitListViewColumns_Thread(HWND hWndListView)
{
WCHAR szText[9][256]={L"DeltaPri",L"BasePri",L"CPU AFFINITY MASK",L"CPU RATE",L"OWNER PROCESS ID",L"THREAD ID"}; // temporary buffer
LVCOLUMN lvc;
memset(&lvc,0,sizeof(lvc));
int iCol;
// Initialize the LVCOLUMN structure.
// The mask specifies that the format, width, text, and
// subitem members of the structure are valid.
lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT |
LVCF_SUBITEM;
// Add the columns.
for (iCol = 0; iCol < 6/*C_COLUMNS*/; iCol++)
{
lvc.iSubItem = iCol;
lvc.pszText = szText[iCol];
lvc.cx = 120; // width of column in pixels
/*LoadString(hInst, IDS_FIRSTCOLUMN + iCol,
szText, sizeof(szText)/sizeof(szText[0]));*/
SendMessage(hWndListView,LVM_INSERTCOLUMN,0,(LPARAM)&lvc);
}
return TRUE;
}
BOOL Install_Process_To_Item(PROCESSENTRY32 pe32,LVITEM *LvItem,HWND hList,int iSubItem)
{
LvItem->mask=LVIF_TEXT; // Text Style
LvItem->cchTextMax = 256; // Max size of test
LvItem->iItem=iSubItem; // choose item
LvItem->iSubItem=0; // Put in first coluom
LvItem->pszText=pe32.szExeFile; // Text to display (can be from a char variable) (Items)
SendMessage(hList,LVM_INSERTITEM,0,(LPARAM)LvItem); // Send to the Listview
WCHAR Temp[256];
LvItem->iSubItem=1;
swprintf_s(Temp,L"0x%08X",pe32.th32ProcessID);
LvItem->pszText=Temp;
SendMessage(hList,LVM_SETITEM,0,(LPARAM)LvItem);
LvItem->iSubItem=2;
swprintf_s(Temp,L"%d", pe32.cntThreads);
LvItem->pszText=Temp;
SendMessage(hList,LVM_SETITEM,0,(LPARAM)LvItem);
LvItem->iSubItem=3;
swprintf_s(Temp,L"0x%08X", pe32.th32ParentProcessID);
LvItem->pszText=Temp;
SendMessage(hList,LVM_SETITEM,0,(LPARAM)LvItem);
LvItem->iSubItem=4;
swprintf_s(Temp,L"%d", pe32.pcPriClassBase);
LvItem->pszText=Temp;
SendMessage(hList,LVM_SETITEM,0,(LPARAM)LvItem);
// Retrieve the priority class.
DWORD dwPriorityClass = 0;
HANDLE hProcess = OpenProcess( PROCESS_ALL_ACCESS, FALSE, pe32.th32ProcessID );
if( hProcess == NULL )
{
return FALSE;
}
else
{
dwPriorityClass = GetPriorityClass( hProcess );
if( !dwPriorityClass )
{
return FALSE;
}
CloseHandle( hProcess );
}
LvItem->iSubItem=5;
swprintf_s(Temp,L"%d", dwPriorityClass);
LvItem->pszText=Temp;
SendMessage(hList,LVM_SETITEM,0,(LPARAM)LvItem);
// List the modules and threads associated with this process
//ListProcessModules( pe32.th32ProcessID );
//ListProcessThreads( pe32.th32ProcessID );
return TRUE;
}
BOOL GetThread_FromProcess(DWORD dwOwnerPID,HWND hList)
{
LVITEM LvItem;
memset(&LvItem,0,sizeof(LvItem)); // Reset Item Struct
HANDLE hThreadSnap = INVALID_HANDLE_VALUE;
THREADENTRY32 th32;
// Take a snapshot of all running threads
hThreadSnap = CreateToolhelp32Snapshot( TH32CS_SNAPTHREAD, 0 );
if( hThreadSnap == INVALID_HANDLE_VALUE )
return( FALSE );
// Fill in the size of the structure before using it.
th32.dwSize = sizeof(THREADENTRY32 );
// Retrieve information about the first thread,
// and exit if unsuccessful
if( !Thread32First( hThreadSnap, &th32 ) )
{
CloseHandle( hThreadSnap ); // Must clean up the snapshot object!
return( FALSE );
}
// Now walk the thread list of the system,
// and display information about each thread
// associated with the specified process
int count=0;
do
{
if( th32.th32OwnerProcessID == dwOwnerPID )
{
LvItem.mask=LVIF_TEXT; // Text Style
LvItem.cchTextMax = 256; // Max size of test
LvItem.iItem=count++; // choose item
LvItem.iSubItem=0; // Put in first coluom
WCHAR Temp[256];
swprintf_s(Temp,L"0x%08X",th32.th32ThreadID);
LvItem.pszText=Temp; // Text to display (can be from a char variable) (Items)
SendMessage(hList,LVM_INSERTITEM,0,(LPARAM)&LvItem); // Send to the Listview
LvItem.iSubItem=1;
swprintf_s(Temp,L"0x%08X",th32.th32OwnerProcessID);
LvItem.pszText=Temp;
SendMessage(hList,LVM_SETITEM,0,(LPARAM)&LvItem);
LvItem.iSubItem=2;
swprintf_s(Temp,L"%d/100", 20);
LvItem.pszText=Temp;
SendMessage(hList,LVM_SETITEM,0,(LPARAM)&LvItem);
LvItem.iSubItem=3;
swprintf_s(Temp,L"0x%08X", 3);
LvItem.pszText=Temp;
SendMessage(hList,LVM_SETITEM,0,(LPARAM)&LvItem);
LvItem.iSubItem=4;
swprintf_s(Temp,L"0x%08X", th32.tpBasePri);
LvItem.pszText=Temp;
SendMessage(hList,LVM_SETITEM,0,(LPARAM)&LvItem);
LvItem.iSubItem=5;
swprintf_s(Temp,L"0x%08X", th32.tpDeltaPri);
LvItem.pszText=Temp;
SendMessage(hList,LVM_SETITEM,0,(LPARAM)&LvItem);
HANDLE hThread=OpenThread(THREAD_ALL_ACCESS,TRUE,th32.th32ThreadID);
/* DWORD dwMask=0x0002;
SetThreadAffinityMask(hThread,dwMask);*/
}
} while( Thread32Next(hThreadSnap, &th32 ) );
CloseHandle( hThreadSnap );
return( TRUE );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -