CString GetProductVersion(LPCTSTR lpszFileName = NULL)
{
CString strVersion;
TCHAR szSrcfn[MAX_PATH]; // 要获取信息的文件
if(lpszFileName == NULL)
::GetModuleFileName(NULL, szSrcfn, sizeof(szSrcfn));
else
lstrcpy(szSrcfn, lpszFileName);
DWORD dwLen = 0;
char* lpData=NULL;
BOOL bSuccess = FALSE;
DWORD handle;
dwLen = GetFileVersionInfoSize(szSrcfn, &handle);
if (0 == dwLen)
{
delete lpData;
return L"获得版本信息失败!";
}
lpData =new char [dwLen+1];
bSuccess = GetFileVersionInfo(szSrcfn, 0, dwLen, lpData);
if (!bSuccess)
{
delete lpData;
return L"获得版本信息失败!";
}
LPVOID lpBuffer = NULL;
UINT uLen = 0;
struct LANGANDCODEPAGE {
WORD wLanguage;
WORD wCodePage;
};
LANGANDCODEPAGE Translate;
LANGANDCODEPAGE* lpTranslate = &Translate;
UINT cbTranslate;
VerQueryValue( lpData, TEXT("\\VarFileInfo\\Translation"),
(LPVOID*)&lpTranslate, &cbTranslate);
TCHAR szSubBlock[256];
wnsprintf( (LPWSTR)szSubBlock, 256, _T("\\StringFileInfo\\%04x%04x\\ProductVersion"),
lpTranslate[0].wLanguage, lpTranslate[0].wCodePage);
bSuccess = VerQueryValue(lpData, szSubBlock, &lpBuffer,&uLen);
if (!bSuccess)
{
delete lpData;
return L"获得版本信息失败!";
}
strVersion.Format(L"%s", (char*)lpBuffer);
strVersion.Replace(L",", L".");
delete [] lpData;
return strVersion;
}