通过exe文件,获取程序版本号,公司名称等信息

//获取 文件版本号的
//    pName    _T("CompanyName"),_T("FileDescription"),_T("FileVersion"),
//    _T("InternalName"),_T("LegalCopyright"),_T("OriginalFilename"),
//    _T("ProductName"),_T("ProductVersion"),

BOOL CTrayMgrDlg::GetFileVersionString(CHAR * pFileName, _bstr_t * pRecvStr, TCHAR * pName /* = NULL */)
{
    DWORD    dwDummyHandle; // will always be set to zero
    DWORD    dwLen;
    BYTE    * pVersionInfo;
    BOOL    bRetVal;

    VS_FIXEDFILEINFO    FileVersion;
    *pRecvStr = "";

    struct TRANSLATION {
        WORD langID;            // language ID
        WORD charset;            // character set (code page)
    } Translation;

    HMODULE        hVerDll;
    hVerDll = LoadLibrary(_T("VERSION.dll"));
    if(hVerDll == NULL)
        return FALSE;

    typedef DWORD (WINAPI *Fun_GetFileVersionInfoSizeA)(CHAR *, DWORD *);
    typedef BOOL  (WINAPI *Fun_GetFileVersionInfoA)(CHAR *, DWORD, DWORD, LPVOID);
    typedef BOOL  (WINAPI *Fun_VerQueryValueA)(LPVOID, CHAR *, LPVOID, PUINT);


    Fun_GetFileVersionInfoSizeA        pGetFileVersionInfoSizeA;
    Fun_GetFileVersionInfoA            pGetFileVersionInfoA;
    Fun_VerQueryValueA                pVerQueryValueA;

    pGetFileVersionInfoSizeA = (Fun_GetFileVersionInfoSizeA)::GetProcAddress(hVerDll, "GetFileVersionInfoSizeA");
    pGetFileVersionInfoA     = (Fun_GetFileVersionInfoA)::GetProcAddress(hVerDll, "GetFileVersionInfoA");
    pVerQueryValueA             = (Fun_VerQueryValueA)::GetProcAddress(hVerDll, "VerQueryValueA");

    Translation.langID = 0x0409;    //
    Translation.charset = 1252;        // default = ANSI code page

    dwLen = pGetFileVersionInfoSizeA(pFileName, &dwDummyHandle);
    if (dwLen == 0)
        return FALSE;

    pVersionInfo = new BYTE[dwLen]; // allocate version info
    bRetVal = pGetFileVersionInfoA(pFileName, 0, dwLen, pVersionInfo);
    if(bRetVal == FALSE)
    {
        delete [] pVersionInfo;
        return FALSE;
    }

    VOID    * pVI;
    UINT    uLen;

    bRetVal = pVerQueryValueA(pVersionInfo, "\\", &pVI, &uLen);
    if(bRetVal == FALSE)
    {
        delete [] pVersionInfo;
        return FALSE;
    }
	
    memcpy(&FileVersion, pVI, sizeof(VS_FIXEDFILEINFO));
	
    bRetVal = pVerQueryValueA(pVersionInfo, "\\VarFileInfo\\Translation", 
        &pVI, &uLen);
    if(bRetVal && uLen >= 4)
    {
        memcpy(&Translation, pVI, sizeof(TRANSLATION));
    }
	
  //  BREAKIF(FileVersion.dwSignature != VS_FFI_SIGNATURE);
	if (FileVersion.dwSignature != VS_FFI_SIGNATURE)
	{
		return FALSE;
	}
	
    CHAR        szQuery[1024];
    CHAR        * pVal;
    UINT        iLenVal;
	
    if(pName == NULL)
    {
        sprintf(szQuery, "%d.%d.%d.%d",
            HIWORD(FileVersion.dwFileVersionMS), LOWORD(FileVersion.dwFileVersionMS),
            HIWORD(FileVersion.dwFileVersionLS), LOWORD(FileVersion.dwFileVersionLS));
        *pRecvStr = szQuery;
    }
    else
    {
        _bstr_t bStrNtyLine1 = pName;
        sprintf(szQuery, "\\StringFileInfo\\%04X%04X\\%s",
            Translation.langID, Translation.charset, (CHAR *)bStrNtyLine1);
		
        bRetVal = pVerQueryValueA(pVersionInfo, szQuery, (LPVOID*)&pVal, &iLenVal);
        if(bRetVal)
            *pRecvStr = pVal;
        else
            *pRecvStr = "";
    }
	
    delete [] pVersionInfo;
    return TRUE;
}


发布了1 篇原创文章 · 获赞 1 · 访问量 1万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览