#include "stdafx.h"
#include "ProcessInfo.h"
#include "ProcessInfoDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CProcessInfoDlg dialog
CProcessInfoDlg::CProcessInfoDlg(CWnd* pParent /*=NULL*/)
: CDialog(CProcessInfoDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CProcessInfoDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CProcessInfoDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CProcessInfoDlg)
DDX_Control(pDX, IDC_LC_PROCESSINFO, m_lcProcessInfo);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CProcessInfoDlg, CDialog)
//{{AFX_MSG_MAP(CProcessInfoDlg)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CProcessInfoDlg message handlers
BOOL CProcessInfoDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
m_lcProcessInfo.InsertColumn(0, "PID", LVCFMT_LEFT, 40);
m_lcProcessInfo.InsertColumn(1, "Name", LVCFMT_LEFT, 130);
m_lcProcessInfo.InsertColumn(2, "Creation Time", LVCFMT_LEFT, 140);
m_lcProcessInfo.InsertColumn(3, "Running Time", LVCFMT_LEFT, 100);
m_lcProcessInfo.InsertColumn(4, "Kernel Time", LVCFMT_LEFT, 80);
m_lcProcessInfo.InsertColumn(5, "User Time", LVCFMT_LEFT, 80);
m_lcProcessInfo.SetExtendedStyle(LVS_EX_FULLROWSELECT);
//Process();
return (TRUE);
}
//====================================================================
void CProcessInfoDlg::Process( void )
{
DWORD dwProcessIDs[150],
dwSize,
dwNeeded;
HANDLE hProcess;
HMODULE hModuleHandle;
FILETIME ftCreation,
ftExit,
ftKernel,
ftUser;
SYSTEMTIME stKernel,
stUser;
CString strName,
strData;
int nIndex,
nProcessCount,
nItem;
COleDateTime timeCreation,
timeNow;
COleDateTimeSpan timeDiff;
if (EnumProcesses(dwProcessIDs, sizeof(dwProcessIDs), &dwSize) == TRUE)
{
// get the current time so we can calculate running time later
timeNow = COleDateTime::GetCurrentTime();
nProcessCount = dwSize / sizeof(DWORD);
for (nIndex = 0; nIndex < nProcessCount; nIndex++)
{
hProcess = OpenProcess(PROCESS_VM_READ | PROCESS_QUERY_INFORMATION, FALSE, dwProcessIDs[nIndex]);
if (NULL != hProcess)
{
strData.Format("%X", dwProcessIDs[nIndex]);
nItem = m_lcProcessInfo.InsertItem(nIndex, strData);
if (EnumProcessModules(hProcess, &hModuleHandle, sizeof(hModuleHandle), &dwNeeded) == TRUE)
{
if (GetModuleBaseName(hProcess, hModuleHandle, strName) > 0)
{
m_lcProcessInfo.SetItemText(nItem, 1, strName);
if (GetProcessTimes(hProcess, &ftCreation, &ftExit, &ftKernel, &ftUser) == TRUE)
{
timeCreation = ftCreation;
strData.Format("%02d/%02d/%d @ %02d:%02d:%02d",
timeCreation.GetMonth(),
timeCreation.GetDay(),
timeCreation.GetYear(),
timeCreation.GetHour(),
timeCreation.GetMinute(),
timeCreation.GetSecond());
m_lcProcessInfo.SetItemText(nItem, 2, strData);
timeDiff = timeNow - timeCreation;
strData.Format("%ud %uh %um %us", timeDiff.GetDays(), timeDiff.GetHours(), timeDiff.GetMinutes(), timeDiff.GetSeconds());
m_lcProcessInfo.SetItemText(nItem, 3, strData);
FileTimeToSystemTime(&ftKernel, &stKernel);
strData.Format("%uh %um %us", stKernel.wHour, stKernel.wMinute, stKernel.wSecond);
m_lcProcessInfo.SetItemText(nItem, 4, strData);
FileTimeToSystemTime(&ftUser, &stUser);
strData.Format("%uh %um %us", stUser.wHour, stUser.wMinute, stUser.wSecond);
m_lcProcessInfo.SetItemText(nItem, 5, strData);
}
else
TRACE("GetProcessTimes() failed: %s\n", GetErrorMessage());
}
else
TRACE("GetModuleBaseName() failed: %s\n", GetErrorMessage());
}
else
TRACE2("EnumProcessModules() failed on PID %#x: %s\n", dwProcessIDs[nIndex], GetErrorMessage());
CloseHandle(hProcess);
}
else
TRACE2("OpenProcess(%#x) failed: %s\n", dwProcessIDs[nIndex], GetErrorMessage());
}
}
else
TRACE("EnumProcesses() failed: %s\n", GetErrorMessage());
}
//====================================================================
DWORD CProcessInfoDlg::GetModuleBaseName( const HANDLE hProcess, const HMODULE hModule, CString &strName )
{
DWORD dwLength;
dwLength = ::GetModuleBaseName(hProcess, hModule, strName.GetBuffer(MAX_PATH), MAX_PATH);
strName.ReleaseBuffer();
return (dwLength);
}
//====================================================================
CString CProcessInfoDlg::GetErrorMessage( void )
{
CString strError;
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0,
NULL);
strError = (LPTSTR) lpMsgBuf;
LocalFree(lpMsgBuf);
return (strError);
}