首先添加#pragma comment(lib,"Version.lib")
CString GetProductVersion(LPCTSTR lpszFileName)
{
CString strVersion;
TCHAR szSrcfn[MAX_PATH]; // 要获取信息的文件
if(lpszFileName == NULL)
::GetModuleFileName(NULL, szSrcfn, sizeof(szSrcfn));
else
lstrcpy(szSrcfn, lpszFileName);
// 读文件信息
DWORD dwVerHnd = 0;
DWORD dwVerInfoSize = ::GetFileVersionInfoSize(szSrcfn, &dwVerHnd);
if(dwVerInfoSize)
{
HANDLE hMem;
LPVOID lpvMem;
unsigned int uInfoSize = 0;
VS_FIXEDFILEINFO * pFileInfo;
hMem = ::GlobalAlloc(GMEM_MOVEABLE, dwVerInfoSize);
lpvMem = ::GlobalLock(hMem);
::GetFileVersionInfo(szSrcfn, dwVerHnd, dwVerInfoSize, lpvMem);
::VerQueryValue(lpvMem, (LPTSTR)_T("\\"), (void**)&pFileInfo, &uInfoSize);
WORD nVer[4];
nVer[0] = HIWORD(pFileInfo->dwProductVersionMS);
nVer[1] = LOWORD(pFileInfo->dwProductVersionMS);
nVer[2] = HIWORD(pFileInfo->dwProductVersionLS);
nVer[3] = LOWORD(pFileInfo->dwProductVersionLS);
strVersion.Format(_T("%d.%d.%d.%d"), nVer[0], nVer[1],nVer[2],nVer[3]);
::GlobalUnlock(hMem);
::GlobalFree(hMem);
}
return strVersion;
}