BOOL GetFileVersion(LPCTSTR strFile, CString& strVersion) { TCHAR szVersionBuffer[8192] = _T(""); DWORD dwVerSize; DWORD dwHandle; dwVerSize = GetFileVersionInfoSize(strFile, &dwHandle); if (dwVerSize == 0) return FALSE; if (GetFileVersionInfo(strFile, 0, dwVerSize, szVersionBuffer)) { VS_FIXEDFILEINFO * pInfo; unsigned int nInfoLen; if (VerQueryValue(szVersionBuffer, _T("\\"), (void**)&pInfo, &nInfoLen)) { strVersion.Format(_T("%d.%d.%d.%d"), HIWORD(pInfo->dwFileVersionMS), LOWORD(pInfo->dwFileVersionMS), HIWORD(pInfo->dwFileVersionLS), LOWORD(pInfo->dwFileVersionLS)); return TRUE; } } return FALSE; }
产品版本:
BOOL GetExeVersion(LPCTSTR szFile, TCHAR* szVer, DWORD dwBufSize) { DWORD dwSize = GetFileVersionInfoSize((LPCWSTR)szFile,NULL); if (dwSize == 0){ return FALSE; } LPVOID pBlock = malloc(dwSize); GetFileVersionInfo((LPCWSTR)szFile, 0, dwSize, pBlock); TCHAR* pVerValue = NULL; UINT nSize = 0; VerQueryValue(pBlock,_T("\\VarFileInfo\\Translation"), (LPVOID*)&pVerValue, &nSize); CString strSubBlock,strTranslation,strTemp; strTemp.Format(_T("000%x"), *((unsigned short int *)pVerValue)); strTranslation = strTemp.Right(4); strTemp.Format(_T("000%x"), *((unsigned short int *)&pVerValue[2])); strTranslation += strTemp.Right(4); //080404b0为中文,040904E4为英文 strSubBlock.Format(_T("\\StringFileInfo\\080404b0\\ProductVersion")); VerQueryValue(pBlock, strSubBlock.GetBufferSetLength(256), (LPVOID*)&pVerValue, &nSize); strSubBlock.ReleaseBuffer(); StringCchCopy(szVer, dwBufSize, pVerValue); return TRUE; }