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

📄 aboutdlg.cpp

📁 VC网络程序设计实例导航配套代码
💻 CPP
字号:
#include "stdafx.h"
#include "AboutDlg.h"
#include <Tlhelp32.h>

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
    m_pszLoadedModules         = NULL;
    m_pszLoadedModulesFullPath = NULL;
    m_nNumberOfLoadedModules   = 0;
}


CAboutDlg::~CAboutDlg()
{
    // Delete individual strings
    for (int i = 0; i < m_nNumberOfLoadedModules; ++i)
    {
        if (m_pszLoadedModules[i] != NULL)
        {
            delete m_pszLoadedModules[i];
            delete m_pszLoadedModulesFullPath[i];

            m_pszLoadedModulesFullPath[i] = NULL;
            m_pszLoadedModules[i]         = NULL;
        }
    }

    // Delete the array of strings
    if (m_pszLoadedModules != NULL)
    {
        delete m_pszLoadedModules;
        delete m_pszLoadedModulesFullPath;

        m_pszLoadedModules         = NULL;
        m_pszLoadedModulesFullPath = NULL;
    }
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BOOL CAboutDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
    	
	CListCtrl* pListCtrl = static_cast<CListCtrl*>(GetDlgItem(IDC_LIST_LOADED_MODULES));
    CStatic* pStaticBuildInfo = static_cast<CStatic*>(GetDlgItem(IDC_STATIC_BUILD_INFO));

    // Get the build date and time
    CString strBuildDateTime = GetBuildDateTime();
    pStaticBuildInfo->SetWindowText(strBuildDateTime);

    // Get the number of loaded modules that will be shown
    GetLoadedModules(&m_pszLoadedModules, &m_pszLoadedModulesFullPath, &m_nNumberOfLoadedModules);

    pListCtrl->InsertColumn(0, _T("Name"), LVCFMT_LEFT, 100);
    pListCtrl->InsertColumn(1, _T("Path"), LVCFMT_LEFT, 245);
    

    // We start from 1 because the first name is always the path of the exe itself.
    // Hence, no point in showing it again.
    for (int i = 1; i < m_nNumberOfLoadedModules; ++i)
    {
        pListCtrl->InsertItem(i - 1, m_pszLoadedModules[i]);
        pListCtrl->SetItemText(i - 1, 1, m_pszLoadedModulesFullPath[i]);
    }
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}


void CAboutDlg::GetLoadedModules(TCHAR*** pszModuleList, TCHAR*** pszModuleListFullPath, int* pnNumberOfModules)
{
    *pnNumberOfModules = 0;

    DWORD dwCurrentProcessId = ::GetCurrentProcessId();
    
    // Take this process's snapshot
    HANDLE hModulesSnapShot = ::CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, dwCurrentProcessId);
    
    MODULEENTRY32 me32;

    // << Start counting the number of modules loaded
    BOOL bSuccessInModuleFind = ::Module32First(hModulesSnapShot, &me32);
    
    if (bSuccessInModuleFind)
    {
        ++(*pnNumberOfModules);
        do 
        {
            bSuccessInModuleFind = ::Module32Next(hModulesSnapShot, &me32);
            if (bSuccessInModuleFind)
            {
                ++(*pnNumberOfModules);
            }
        }
        while (bSuccessInModuleFind);
    }
    else
    {
        // We didn't forget to clean up the snapshot object.
        ::CloseHandle(hModulesSnapShot);

        return; // no modules loaded
    }

    // Stop counting the number of modules loaded >>


    // Now do the same things to get the loaded module names and paths, as done above,
    // but allocate memory for the module name and path name strings.
    // Again, walk through the list of modules in this process

    *pszModuleList         = new TCHAR*[(*pnNumberOfModules)];
    *pszModuleListFullPath = new TCHAR*[(*pnNumberOfModules)];

    ::Module32First(hModulesSnapShot, &me32);
    
    // Allocate memory for the first module name and its path
    unsigned int nStrLenModuleName     = ::strlen(me32.szModule);
    unsigned int nStrLenModuleFullPath = ::strlen(me32.szExePath);

    (*pszModuleList)[0]         = new TCHAR[nStrLenModuleName + 1];
    (*pszModuleListFullPath)[0] = new TCHAR[nStrLenModuleFullPath + 1];

    // Copy the first module name and its path in the buffer
    ::strcpy((*pszModuleList)[0], me32.szModule);
    ::strcpy((*pszModuleListFullPath)[0], me32.szExePath);

    for (int i = 1; i < (*pnNumberOfModules); ++i)
    {
        ::Module32Next(hModulesSnapShot, &me32);

        // Allocate memory for modules name and their path
        nStrLenModuleName     = ::strlen(me32.szModule);
        nStrLenModuleFullPath = ::strlen(me32.szExePath);

        (*pszModuleList)[i]         = new TCHAR[nStrLenModuleName + 1];
        (*pszModuleListFullPath)[i] = new TCHAR[nStrLenModuleFullPath + 1];

        // Copy the module name and their path in the buffer
        ::strcpy((*pszModuleList)[i], me32.szModule);
        ::strcpy((*pszModuleListFullPath)[i], me32.szExePath);
    }

    // We didn't forget to clean up the snapshot object.
    ::CloseHandle(hModulesSnapShot);
}


CString CAboutDlg::GetBuildDateTime()
{
    CString strDateTime;
    
    strDateTime.Format("Built on %s at %s", __DATE__, __TIME__);

    return strDateTime;
}

⌨️ 快捷键说明

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