DemonMemory/源代码/ChooseProcessDlg.cpp

120 lines
2.6 KiB
C++

// ChooseProcessDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "HandleMemory.h"
#include "ChooseProcessDlg.h"
#include "afxdialogex.h"
// CChooseProcessDlg 对话框
IMPLEMENT_DYNAMIC(CChooseProcessDlg, CDialogEx)
CChooseProcessDlg::CChooseProcessDlg(CWnd* pParent /*=NULL*/)
: CDialogEx(CChooseProcessDlg::IDD, pParent)
{
}
CChooseProcessDlg::~CChooseProcessDlg()
{
}
void CChooseProcessDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_LIST_PROCESS, m_ProcessList);
}
BEGIN_MESSAGE_MAP(CChooseProcessDlg, CDialogEx)
ON_BN_CLICKED(IDOK, &CChooseProcessDlg::OnBnClickedOk)
END_MESSAGE_MAP()
// CChooseProcessDlg 消息处理程序
BOOL CChooseProcessDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// TODO: 在此添加额外的初始化
InitListControl();
return TRUE; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}
void CChooseProcessDlg::OnBnClickedOk()
{
// TODO: 在此添加控件通知处理程序代码
DWORD dwIndex = m_ProcessList.GetSelectionMark();
if(0 > dwIndex)
{
::MessageBox(m_hWnd, "Please Choose a Item!", "Warn", MB_OK | MB_ICONWARNING);
return ;
}
// 获取名称
m_ProcessList.GetItemText(dwIndex, 0, m_szProcessName, MAX_PATH);
// 获取PID
char szTemp[MAX_PATH] = {0};
m_ProcessList.GetItemText(dwIndex, 1, szTemp, MAX_PATH);
m_dwID = atoi(szTemp);
CDialogEx::OnOK();
}
DWORD CChooseProcessDlg::GetProcessID()
{
return m_dwID;
}
char *CChooseProcessDlg::GetProcessName()
{
return m_szProcessName;
}
BOOL CChooseProcessDlg::InitListControl()
{
// 初始化列表控件
m_ProcessList.InsertColumn(0, "Name", 0, 180);
m_ProcessList.InsertColumn(1, "PID", 0, 60);
m_ProcessList.SetExtendedStyle(LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT);
m_ProcessList.SetTextColor(RGB(0,100,0));
// 获取进程信息
GetProcessInfo();
return TRUE;
}
BOOL CChooseProcessDlg::GetProcessInfo()
{
m_ProcessList.DeleteAllItems();
PROCESSENTRY32 pe32 = {0};
pe32.dwSize = sizeof(PROCESSENTRY32);
HANDLE hProcessSnap = ::CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if(INVALID_HANDLE_VALUE == hProcessSnap)
{
::MessageBox(NULL, "Create Process Snapshot Error!", "Error", MB_OK);
return FALSE;
}
BOOL bMore = ::Process32First(hProcessSnap, &pe32);
DWORD dwIndex = 0;
char szTemp[MAX_PATH] = {0};
while(bMore)
{
::wsprintf(szTemp, "%s", pe32.szExeFile);
m_ProcessList.InsertItem(dwIndex, szTemp);
::wsprintf(szTemp, "%d", pe32.th32ProcessID);
m_ProcessList.SetItemText(dwIndex, 1, szTemp);
bMore = ::Process32Next(hProcessSnap, &pe32);
}
return TRUE;
}