程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 進程模塊查看器,模塊查看器

進程模塊查看器,模塊查看器

編輯:C++入門知識

進程模塊查看器,模塊查看器


主要界面如下:

主要代碼如下:

BOOL CEnumProcessDlg::OnInitDialog()
{
    CDialog::OnInitDialog();

    // 將“關於...”菜單項添加到系統菜單中。

    // IDM_ABOUTBOX 必須在系統命令范圍內。
    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);
        }
    }

    // 設置此對話框的圖標。當應用程序主窗口不是對話框時,框架將自動
    //  執行此操作
    SetIcon(m_hIcon, TRUE);            // 設置大圖標
    SetIcon(m_hIcon, FALSE);        // 設置小圖標

    // TODO: 在此添加額外的初始化代碼
    LONG lStyle;
    lStyle = GetWindowLong(m_ctrlList.m_hWnd, GWL_STYLE);
    lStyle &= ~LVS_TYPEMASK;
    lStyle |= LVS_REPORT;
    SetWindowLong(m_ctrlList.m_hWnd, GWL_STYLE, lStyle);

    DWORD dwStyle = m_ctrlList.GetExtendedStyle();
    dwStyle |= LVS_EX_FULLROWSELECT;
    dwStyle |= LVS_EX_GRIDLINES;
    m_ctrlList.SetExtendedStyle(dwStyle);
    m_ctrlList.InsertColumn(0, _T("Process ID"), LVCFMT_LEFT, 75);
    m_ctrlList.InsertColumn(1, _T("Process Name"), LVCFMT_LEFT, 120);
    m_ctrlList.InsertColumn(2, _T("Parent Process ID"), LVCFMT_LEFT, 115);
    m_ctrlList.InsertColumn(3, _T("Thread Count"), LVCFMT_LEFT, 90);
    ListProcess();

    return TRUE;  // 除非將焦點設置到控件,否則返回 TRUE
}

void CEnumProcessDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
    if ((nID & 0xFFF0) == IDM_ABOUTBOX)
    {
        CAboutDlg dlgAbout;
        dlgAbout.DoModal();
    }
    else
    {
        CDialog::OnSysCommand(nID, lParam);
    }
}

// 如果向對話框添加最小化按鈕,則需要下面的代碼
//  來繪制該圖標。對於使用文檔/視圖模型的 MFC 應用程序,
//  這將由框架自動完成。

void CEnumProcessDlg::OnPaint()
{
    if (IsIconic())
    {
        CPaintDC dc(this); // 用於繪制的設備上下文

        SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

        // 使圖標在工作矩形中居中
        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;

        // 繪制圖標
        dc.DrawIcon(x, y, m_hIcon);
    }
    else
    {
        CDialog::OnPaint();
    }
}

//當用戶拖動最小化窗口時系統調用此函數取得光標顯示。
//
HCURSOR CEnumProcessDlg::OnQueryDragIcon()
{
    return static_cast<HCURSOR>(m_hIcon);
}


void CEnumProcessDlg::ListProcess(void)
{
    HANDLE hProcessSnap;
    PROCESSENTRY32 pe32;
    
    hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    if (hProcessSnap == INVALID_HANDLE_VALUE)
    {
        CString strErr;
        strErr.Format(_T("CreateToolhelp32Snapshot failed:%d"), GetLastError());
        AfxMessageBox(strErr);
        return;
    }

    pe32.dwSize = sizeof(PROCESSENTRY32);

    int index = 0;
    if (!Process32First(hProcessSnap, &pe32))
    {
        CString strErr;
        strErr.Format(_T("Process32First failed:%d"), GetLastError());
        AfxMessageBox(strErr);
        CloseHandle(hProcessSnap);
        return;
    }

    do 
    {
        CString strID;
        strID.Format(_T("%d"), pe32.th32ProcessID);
        m_ctrlList.InsertItem(index, strID);
        m_ctrlList.SetItemText(index, 1, pe32.szExeFile);
        strID.Format(_T("%d"), pe32.th32ParentProcessID);
        m_ctrlList.SetItemText(index, 2, strID);
        strID.Format(_T("%d"), pe32.cntThreads);
        m_ctrlList.SetItemText(index++, 3, strID);
    } while (Process32Next(hProcessSnap, &pe32));

    CloseHandle(hProcessSnap);
}

void CEnumProcessDlg::OnContextMenu(CWnd* pWnd, CPoint point)
{
    // TODO: 在此處添加消息處理程序代碼
    CMenu menu;

    if (menu.CreatePopupMenu() == FALSE)
    {
        return;
    }
    
    if (pWnd == &m_ctrlList)
    {
        menu.AppendMenu(MF_STRING , ID_SCANMODULE , _T("查看依賴模塊"));
        if(m_ctrlList.GetSelectedCount() < 0)
        {
            menu.EnableMenuItem(ID_SCANMODULE , MF_DISABLED | MF_GRAYED);
        }
    }

    menu.TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, this);
}

void CEnumProcessDlg::ScanModule(UINT nID)
{
    switch (nID)
    {
    case ID_SCANMODULE:
        {
            CString strProcessID = m_ctrlList.GetItemText(m_ctrlList.GetSelectionMark(), 0);
            CProcessModule dlgProcessModule;
            dlgProcessModule.m_dwProcessID = _tcstoul(strProcessID, 0, 10);
            dlgProcessModule.DoModal();
        }
        break;
    default:
        break;
    }
}

void CEnumProcessDlg::OnNMDblclkListProcess(NMHDR *pNMHDR, LRESULT *pResult)
{
    // TODO: 在此添加控件通知處理程序代碼
    CString strProcessID = m_ctrlList.GetItemText(m_ctrlList.GetSelectionMark(), 0);
    CProcessModule dlgProcessModule;
    dlgProcessModule.m_dwProcessID = _tcstoul(strProcessID, 0, 10);
    dlgProcessModule.DoModal();
    *pResult = 0;
}

  

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved