www.gusucode.com > 获取Windows进程:VC++进程管理器-源码程序 > 获取Windows进程:VC++进程管理器-源码程序\code\showallprocess\showallprocessDlg.cpp
// showallprocessDlg.cpp : implementation file // Download by http://www.NewXing.com #include "stdafx.h" #include "showallprocess.h" #include "showallprocessDlg.h" //包含获取系统进程函数头文件 #include "TLHELP32.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() ///////////////////////////////////////////////////////////////////////////// // CShowallprocessDlg dialog CShowallprocessDlg::CShowallprocessDlg(CWnd* pParent /*=NULL*/) : CDialog(CShowallprocessDlg::IDD, pParent) { //{{AFX_DATA_INIT(CShowallprocessDlg) // 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 CShowallprocessDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CShowallprocessDlg) DDX_Control(pDX, IDC_STOP, m_stop); DDX_Control(pDX, IDCANCEL, m_exit); DDX_Control(pDX, IDOK, m_show); DDX_Control(pDX, IDC_LIST_SHOW, m_showlist); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CShowallprocessDlg, CDialog) //{{AFX_MSG_MAP(CShowallprocessDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDOK, OnGetProcess) ON_BN_CLICKED(IDC_STOP, OnStopProcess) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CShowallprocessDlg message handlers BOOL CShowallprocessDlg::OnInitDialog() { CDialog::OnInitDialog(); //设置List控件的表格扩展属性 m_showlist.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES); m_showlist.InsertColumn(0,"进程映像名称"); m_showlist.InsertColumn(1,"进程开启的线程数"); m_showlist.InsertColumn(2,"PID"); m_showlist.InsertColumn(3,"优先级"); for(int i=0;i<4;i++) m_showlist.SetColumnWidth(i,120); // 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 CShowallprocessDlg::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 CShowallprocessDlg::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 HCURSOR CShowallprocessDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } void CShowallprocessDlg::OnGetProcess() { // TODO: Add your control notification handler code here HANDLE Snapshot; Snapshot=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0); //获得某一时刻系统的进程、堆(heap)、模块(module)或线程的快照信息 PROCESSENTRY32 processListStr; processListStr.dwSize=sizeof(PROCESSENTRY32); BOOL return_value; return_value=Process32First(Snapshot,&processListStr); //获得系统进程链表中第一个进程的信息 m_showlist.DeleteAllItems(); int i=0; 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); int memory=processListStr.cntThreads; //把代表进程的ID号,存入数组中,便于中止 m_ProcessIndex[i]=processListStr.th32ProcessID; //获得系统进程链表中下一个进程的信息 return_value=Process32Next(Snapshot,&processListStr); i++; } } void CShowallprocessDlg::OnStopProcess() { // TODO: Add your control notification handler code here POSITION pos = m_showlist.GetFirstSelectedItemPosition(); int currentSelect=m_showlist.GetNextSelectedItem(pos); if(currentSelect>0) { //选择某项 CString FileBuffer; //获得进程的可执行文件的文件名 FileBuffer=CString(TEXT("确实要终止进程")+m_showlist.GetItemText(currentSelect,0)+TEXT("?")); if(AfxMessageBox(FileBuffer,MB_OKCANCEL,0)==IDOK) { //先获得该进程句柄,通过进程标识 HANDLE ProcessHandle; ProcessHandle=OpenProcess(PROCESS_ALL_ACCESS,FALSE,m_ProcessIndex[currentSelect]); //利用数组ID指定的获得存在的一个进程的句柄 if(ProcessHandle) { //中止进程 TerminateProcess(ProcessHandle,0); AfxMessageBox("指定进程已终止!"); //刷新显示 OnGetProcess(); } else {AfxMessageBox("系统不允许终止指定进程!");} } } else {AfxMessageBox("请先选择一个需要终止的进程!",MB_OK); } }