返回顶部

收藏

在关于对话框中显示文件版本号和产品版本号

更多
#pragma comment(lib,"version.lib")
void CAboutDlg::OnShowWindow(BOOL bShow, UINT nStatus)
{
    CDialogEx::OnShowWindow(bShow, nStatus);

    // TODO: Add your message handler code here
    TCHAR fileName[MAX_PATH] = {0};
    GetModuleFileName(NULL,fileName,sizeof(fileName));
    CString szFile = fileName;
    int pos = szFile.ReverseFind(_T('\\\\'));
    szFile = szFile.Mid(pos+1,szFile.GetLength()-pos-1);

    CString szFileVer = GetFileVersion(fileName);
    if ( ! szFileVer.IsEmpty() )
        SetDlgItemText(IDC_STATIC_FILEVER,_T("文件版本:")+szFileVer);
    CString szProductVer = GetProductVersion(fileName);
    if ( ! szProductVer.IsEmpty() )
        SetDlgItemText(IDC_STATIC_PRODUCTVER,_T("产品版本:")+szProductVer);
}

// 获取文件版本号

CString CAboutDlg::GetFileVersion(TCHAR* fileName)
{
    CString szAppVer;
    DWORD dwHandle;
    DWORD dwVersize = 0;
    dwVersize = GetFileVersionInfoSize(fileName,&dwHandle);
    if (dwVersize != 0)
    {
        TCHAR* pVer = new TCHAR[dwVersize];
        memset(pVer,0,dwVersize);

        if (GetFileVersionInfo(fileName,dwHandle,dwVersize,pVer))
        {
            VS_FIXEDFILEINFO* pInfo;
            UINT pInfoLen = 0;
            if (VerQueryValue(pVer,_T("\\\\"),(LPVOID*)&pInfo,&pInfoLen))
            {
                szAppVer.Format(_T("%d.%d.%d.%d"),HIWORD(pInfo->dwFileVersionMS),LOWORD(pInfo->dwFileVersionMS),
                    HIWORD(pInfo->dwFileVersionLS),LOWORD(pInfo->dwFileVersionLS));
            }
        }
        delete []pVer;
    }
    return szAppVer;
}

// 获取产品版本号

CString CAboutDlg::GetProductVersion(TCHAR* fileName)
{
    CString szAppVer;
    DWORD dwHandle;
    DWORD dwVersize = 0;
    dwVersize = GetFileVersionInfoSize(fileName,&dwHandle);
    if (dwVersize != 0)
    {
        TCHAR* pVer = new TCHAR[dwVersize];
        memset(pVer,0,dwVersize);

        if (GetFileVersionInfo(fileName,dwHandle,dwVersize,pVer))
        {
            VS_FIXEDFILEINFO* pInfo;
            UINT pInfoLen = 0;
            if (VerQueryValue(pVer,_T("\\\\"),(LPVOID*)&pInfo,&pInfoLen))
            {
                szAppVer.Format(_T("%d.%d.%d.%d"),HIWORD(pInfo->dwProductVersionMS),LOWORD(pInfo->dwProductVersionMS),
                    HIWORD(pInfo->dwProductVersionLS),LOWORD(pInfo->dwProductVersionLS));
            }
        }
        delete []pVer;
    }
    return szAppVer;
}
//该片段来自于http://outofmemory.cn

标签:c++,系统

收藏

0人收藏

支持

0

反对

0

发表评论