⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pelistdlg.cpp

📁 基于visual c++ 可以实现列出当前运行的所有进程
💻 CPP
字号:
// pelistDlg.cpp : implementation file
//

#include "stdafx.h"
#include "pelist.h"
#include "pelistDlg.h"
#include   <conio.h>   
#include   <tlhelp32.h>
#include   "psapi.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()

/////////////////////////////////////////////////////////////////////////////
// CPelistDlg dialog

CPelistDlg::CPelistDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CPelistDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CPelistDlg)
	m_num = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CPelistDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPelistDlg)
	DDX_Control(pDX, IDC_LIST1, m_showlist);
	DDX_Text(pDX, IDC_EDIT1, m_num);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CPelistDlg, CDialog)
	//{{AFX_MSG_MAP(CPelistDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPelistDlg message handlers

BOOL CPelistDlg::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
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CPelistDlg::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 CPelistDlg::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 CPelistDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CPelistDlg::OnButton1() 
{

  HANDLE   Snapshot; 
  HANDLE   hExeFile   =   NULL;
  char   path[255]   ;   
  memset(path,   0,   sizeof(path))   ;  
  int m_ProcessIndex[256];
  Snapshot=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);   
  //获得某一时刻系统的进程、堆(heap)、模块(module)或线程的快照信息   
  PROCESSENTRY32   processListStr;   
  processListStr.dwSize=sizeof(PROCESSENTRY32);   
  BOOL   return_value;   
  return_value=Process32First(Snapshot,&processListStr);   
  //获得系统进程链表中第一个进程的信息   
    m_showlist.DeleteAllItems(); 
    m_showlist.InsertColumn(0, "进程名称", LVCFMT_LEFT, 110);
	m_showlist.InsertColumn(1, "此进程开启的线程计数", LVCFMT_LEFT, 130);
	m_showlist.InsertColumn(2, "父进程ID", LVCFMT_LEFT, 130);
	m_showlist.InsertColumn(3, "线程优先权", LVCFMT_LEFT, 130);
	m_showlist.InsertColumn(4, "本线程ID", LVCFMT_LEFT, 130);
	m_showlist.InsertColumn(5, "5", LVCFMT_LEFT, 130);
	// 设置列表控件扩展样式为整行高亮
	m_showlist.SetExtendedStyle(LVS_EX_FULLROWSELECT);
  int   i=0;//item   index   
  while(return_value)   
  {   
  m_showlist.InsertItem(i,processListStr.szExeFile,0);   
  CString   s;   
  s.Format("%d",processListStr.cntThreads);   
  m_showlist.SetItemText(i,1,s);   
  s.Format("%d",processListStr.th32ParentProcessID);   
  m_showlist.SetItemText(i,2,s);   
  s.Format("%d",processListStr.pcPriClassBase);   
  m_showlist.SetItemText(i,3,s);   
  s.Format("%d",processListStr.th32ProcessID);   
  m_showlist.SetItemText(i,4,s); 
  hExeFile   =   OpenProcess(PROCESS_ALL_ACCESS, FALSE, processListStr.th32ProcessID);   
  GetModuleFileNameEx(hExeFile,   (HMODULE)(processListStr.th32ModuleID), path, 255)   ; 
  m_showlist.SetItemText(i,5,path); 
  int   memory=processListStr.cntThreads;   
  m_ProcessIndex[i]=processListStr.th32ProcessID;//save   ID   into   array   to   teminate     
  return_value=Process32Next(Snapshot,&processListStr);   
  //获得系统进程链表中下一个进程的信息   
  i++;   
  }   

  
  
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -