📄 sampledlg.cpp
字号:
// SampleDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Sample.h"
#include "SampleDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// PSAPI wrapper
//
TProcessStatusModule::TProcessStatusModule()
:TDllModule(_T("PSAPI.DLL"))
{
initAll();
}
TProcessStatusModule::~TProcessStatusModule()
{
}
void TProcessStatusModule::initAll(void)
{
FEnumProcesses = NULL;
FEnumProcessModules = NULL;
FGetModuleInformation = NULL;
FGetModuleBaseName = NULL;
FGetModuleFileNameEx = NULL;
}
BOOL TProcessStatusModule::Create(void)
{
if (TDllModule::Create()) {
// DLL handle is valid (DLL is loaded)
FEnumProcesses = (TFEnumProcesses)::GetProcAddress(m_hHandle,_T("EnumProcesses"));
FEnumProcessModules = (TFEnumProcessModules)::GetProcAddress(m_hHandle,_T("EnumProcessModules"));
FGetModuleInformation = (TFGetModuleInformation)::GetProcAddress(m_hHandle,_T("GetModuleInformation"));
#ifdef UNICODE
FGetModuleBaseName = (TFGetModuleBaseName)::GetProcAddress(m_hHandle,_T("GetModuleBaseNameW"));
FGetModuleFileNameEx = (TFGetModuleFileNameEx)::GetProcAddress(m_hHandle,_T("GetModuleFileNameExW"));
#else
FGetModuleBaseName = (TFGetModuleBaseName)::GetProcAddress(m_hHandle,_T("GetModuleBaseNameA"));
FGetModuleFileNameEx = (TFGetModuleFileNameEx)::GetProcAddress(m_hHandle,_T("GetModuleFileNameExA"));
#endif
// Can also check if all pointers to functions are different from NULL.
return TRUE;
}
return FALSE;
}
void TProcessStatusModule::Destroy(void)
{
TDllModule::Destroy();
initAll();
}
BOOL TProcessStatusModule::EnumProcesses(
DWORD *lpidProcess, DWORD cb, DWORD *cbNeeded)
{
ASSERT(FEnumProcesses != NULL);
if (FEnumProcesses)
return FEnumProcesses(lpidProcess,cb,cbNeeded);
return FALSE;
}
BOOL TProcessStatusModule::EnumProcessModules(
HANDLE hProcess,HMODULE *lphModule,DWORD cb,LPDWORD lpcbNeeded)
{
ASSERT(FEnumProcessModules != NULL);
if (FEnumProcessModules)
return FEnumProcessModules(hProcess, lphModule, cb, lpcbNeeded);
return FALSE;
}
DWORD TProcessStatusModule::GetModuleBaseName(
HANDLE hProcess,HMODULE hModule,LPSTR lpBaseName,DWORD nSize)
{
ASSERT(FGetModuleBaseName != NULL);
if (FGetModuleBaseName)
return FGetModuleBaseName(hProcess, hModule, lpBaseName, nSize);
return FALSE;
}
DWORD TProcessStatusModule::GetModuleFileNameEx(
HANDLE hProcess,HMODULE hModule,LPSTR lpFilename,DWORD nSize)
{
ASSERT(FGetModuleFileNameEx != NULL);
if (FGetModuleFileNameEx)
return FGetModuleFileNameEx(hProcess, hModule, lpFilename, nSize);
return FALSE;
}
BOOL TProcessStatusModule::GetModuleInformation(
HANDLE hProcess,HMODULE hModule,LPMODULEINFO lpmodinfo,DWORD cb)
{
ASSERT(FGetModuleInformation != NULL);
if (FGetModuleInformation)
return FGetModuleInformation(hProcess, hModule, lpmodinfo, cb);
return FALSE;
}
/////////////////////////////////////////////////////////////////////////////
// CSampleDlg dialog
CSampleDlg::CSampleDlg(CWnd* pParent /*=NULL*/)
: CDialog(CSampleDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CSampleDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CSampleDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSampleDlg)
DDX_Control(pDX, IDC_LIST, m_List);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSampleDlg, CDialog)
//{{AFX_MSG_MAP(CSampleDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_ENUM, OnEnum)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSampleDlg message handlers
BOOL CSampleDlg::OnInitDialog()
{
CDialog::OnInitDialog();
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
m_List.InsertColumn(0, _T("Process ID"), LVCFMT_LEFT, 60);
m_List.InsertColumn(1, _T("Process name"), LVCFMT_LEFT, 400);
return TRUE; // return TRUE unless you set the focus to a control
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CSampleDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
HCURSOR CSampleDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CSampleDlg::OnEnum()
{
m_List.DeleteAllItems();
TProcessStatusModule module;
if (module.Create()) {
DWORD pids[1024];
DWORD dwNeeded = 0;
if (module.EnumProcesses(pids, 1024, &dwNeeded)) {
CString str;
for (int nIndex = 0; nIndex < (int)(dwNeeded/sizeof(DWORD)); nIndex ++) {
str.Format("%d", pids[nIndex]);
m_List.InsertItem(nIndex, str);
HANDLE hProcess = ::OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
FALSE, pids[nIndex]);
if (hProcess) {
char szProcessName[MAX_PATH] = "Unknown";
HMODULE hModule;
DWORD cbNeeded;
if (module.EnumProcessModules(hProcess, &hModule, sizeof(hModule), &cbNeeded)) {
module.GetModuleFileNameEx(hProcess, hModule, szProcessName, sizeof(szProcessName));
strupr(szProcessName);
}
m_List.SetItemText(nIndex, 1, szProcessName);
::CloseHandle(hProcess);
} else {
m_List.SetItemText(nIndex, 1, "Cannot open process");
}
}
}
module.Destroy();
} else {
AfxMessageBox("DLL PSAPI.DLL is not found");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -