// EnumProcessTestDlg.cpp : implementation file
//
#include "stdafx.h"
#include "EnumProcessTest.h"
#include "EnumProcessTestDlg.h"
#include "EnumProcess.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CEnumProcessTestDlg dialog
CEnumProcessTestDlg::CEnumProcessTestDlg(CWnd* pParent /*=NULL*/)
: CDialog(CEnumProcessTestDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CEnumProcessTestDlg)
// 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 CEnumProcessTestDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CEnumProcessTestDlg)
DDX_Control(pDX, IDC_ENUM16, m_enum16);
DDX_Control(pDX, IDC_MODULE, m_moduleButton);
DDX_Control(pDX, IDC_MAINLIST, m_mainlist);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CEnumProcessTestDlg, CDialog)
//{{AFX_MSG_MAP(CEnumProcessTestDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_ENUMERATEPROCESSES, OnEnumerateprocesses)
ON_BN_CLICKED(IDC_PSAPI, OnPsapi)
ON_BN_CLICKED(IDC_TOOLHELP, OnToolhelp)
ON_BN_CLICKED(IDC_MODULE, OnModule)
ON_NOTIFY(NM_CUSTOMDRAW, IDC_MAINLIST, OnCustomDrawList)
ON_BN_CLICKED(IDC_ENUM16, OnEnum16)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CEnumProcessTestDlg message handlers
BOOL CEnumProcessTestDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 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
CButton* th = static_cast(GetDlgItem(IDC_TOOLHELP));
CButton* ps = static_cast(GetDlgItem(IDC_PSAPI));
CEnumProcess tmp;
int method = tmp.GetAvailableMethods();
if (method != (method|ENUM_METHOD::PSAPI)) ps->EnableWindow(FALSE);
if (method != (method|ENUM_METHOD::TOOLHELP)) th->EnableWindow(FALSE);
if (method != (method|ENUM_METHOD::PROC16)) m_enum16.EnableWindow(FALSE);
m_enumMethod = tmp.GetSuggestedMethod();
TRACE("EnumMethod: %ld, %ld", m_enumMethod, method);
if ((ENUM_METHOD::PSAPI|m_enumMethod) == m_enumMethod)
ps->SetCheck(1);
if ((ENUM_METHOD::TOOLHELP|m_enumMethod) == m_enumMethod)
th->SetCheck(1);
if ((ENUM_METHOD::PROC16|m_enumMethod) == m_enumMethod)
m_enum16.SetCheck(1);
m_moduleButton.EnableWindow(FALSE);
CRect listRect;
m_mainlist.GetWindowRect(&listRect);
m_mainlist.InsertColumn(0, "", LVCFMT_LEFT, listRect.Width()/2);
m_mainlist.InsertColumn(1, "", LVCFMT_LEFT, listRect.Width()/2);
m_mainlist.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
m_imageList.Create(16, 16, 0, 8, 8); // 32, 32 for large icons
m_imageList.Add(AfxGetApp()->LoadIcon(IDI_PROCESS));
m_imageList.Add(AfxGetApp()->LoadIcon(IDI_PROCESS16));
m_imageList.Add(AfxGetApp()->LoadIcon(IDI_MODULE));
m_imageList.Add(AfxGetApp()->LoadIcon(IDI_WMODULE));
m_mainlist.SetImageList(&m_imageList, LVSIL_SMALL);
return TRUE; // return TRUE unless you set the focus to a control
}
void CEnumProcessTestDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// 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 CEnumProcessTestDlg::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();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CEnumProcessTestDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CEnumProcessTestDlg::OnEnumerateprocesses()
{ m_moduleButton.EnableWindow(TRUE);
m_mainlist.DeleteAllItems();
CEnumProcess enumeration;
enumeration.SetMethod(m_enumMethod);
CEnumProcess::CProcessEntry entry;
CString s;
for (BOOL OK = enumeration.GetProcessFirst(&entry); OK; OK = enumeration.GetProcessNext(&entry) )
{int image = 0;
if (entry.hTask16)
{s.Format("%08X (%ld)", entry.dwPID, entry.hTask16);
image = 1;
}
else
s.Format("%08X", entry.dwPID);
TRACE(s + " %s\n", entry.lpFilename);
int index = m_mainlist.InsertItem(LVIF_TEXT | LVIF_PARAM | LVIF_IMAGE, 0, s, NULL, LVIS_FOCUSED , image, entry.dwPID);
m_mainlist.SetItem(index,1,LVIF_TEXT, entry.lpFilename, NULL, NULL, NULL, NULL);
}
}
void CEnumProcessTestDlg::OnPsapi()
{
m_enumMethod = ENUM_METHOD::PSAPI +m_enum16.GetCheck()*ENUM_METHOD::PROC16;
}
void CEnumProcessTestDlg::OnToolhelp()
{
m_enumMethod = ENUM_METHOD::TOOLHELP +m_enum16.GetCheck()*ENUM_METHOD::PROC16;
}
void CEnumProcessTestDlg::OnEnum16()
{ CButton* th = static_cast(GetDlgItem(IDC_TOOLHELP));
CButton* ps = static_cast(GetDlgItem(IDC_PSAPI));
m_enumMethod = th->GetCheck()*ENUM_METHOD::TOOLHELP +
ps->GetCheck()*ENUM_METHOD::PSAPI
+ m_enum16.GetCheck()*ENUM_METHOD::PROC16;
}
void CEnumProcessTestDlg::OnModule()
{
int pos = m_mainlist.GetSelectionMark();
if (-1 == pos) return;
m_moduleButton.EnableWindow(FALSE);
DWORD dwPID = m_mainlist.GetItemData(pos);
m_mainlist.DeleteAllItems();
CEnumProcess enumeration;
enumeration.SetMethod(m_enumMethod);
CEnumProcess::CModuleEntry entry;
CString s;
BOOL OK = enumeration.GetModuleFirst(dwPID, &entry);
if (!OK)
{m_mainlist.InsertItem(LVIF_TEXT | LVIF_PARAM | LVIF_IMAGE, 0, "Module listing not available", NULL, LVIS_FOCUSED , 3, 0);
return;
}
for (OK = enumeration.GetModuleFirst(dwPID, &entry); OK; OK = enumeration.GetModuleNext(dwPID, &entry) )
{int image = entry.pLoadBase==entry.pPreferredBase?2:3;
s.Format("0x%08X (0x%08X)", entry.pLoadBase, entry.pPreferredBase);
TRACE(s + " %s\n", entry.lpFilename);
int index = m_mainlist.InsertItem(LVIF_TEXT | LVIF_PARAM | LVIF_IMAGE, 0, s, NULL, LVIS_FOCUSED , image, 0);
m_mainlist.SetItem(index,1,LVIF_TEXT, entry.lpFilename, NULL, NULL, NULL, NULL);
}
}
void CEnumProcessTestDlg::OnCustomDrawList(NMHDR *pNMHDR, LRESULT *pResult)
{NMLVCUSTOMDRAW* pLVCD = reinterpret_cast( pNMHDR );
// Take the default processing unless we set this to something else below.
*pResult = CDRF_DODEFAULT;
// First thing - check the draw stage. If it's the control's prepaint
// stage, then tell Windows we want messages for every item.
if ( CDDS_PREPAINT == pLVCD->nmcd.dwDrawStage )
{
*pResult = CDRF_NOTIFYITEMDRAW;
}
else if ( CDDS_ITEMPREPAINT == pLVCD->nmcd.dwDrawStage )
{
COLORREF crBkgnd;
if ( (pLVCD->nmcd.dwItemSpec % 2) == 0 )
crBkgnd = RGB(255,255,255);
else
crBkgnd = RGB(220,220,220);
// Store the color back in the NMLVCUSTOMDRAW struct.
pLVCD->clrTextBk = crBkgnd;
// Tell Windows to paint the control itself.
*pResult = CDRF_DODEFAULT;
}
}