windows获取.exe文件版本号

时间:2022-08-19 18:38:33

先贴代码,直接可用,windows QT刚刚接触,研究了接近一个下午==:

QString GetEXEVersion()

{
#pragma comment(lib, "Version")


TCHAR   szFullPath[MAX_PATH];
DWORD   dwVerInfoSize = 0;
DWORD   dwVerHnd;
VS_FIXEDFILEINFO *pFileInfo;


GetModuleFileName(NULL, szFullPath, sizeof(szFullPath));//这一步很关键,可以自动获取.exe文件路径
dwVerInfoSize = GetFileVersionInfoSize(szFullPath, &dwVerHnd);//这一步更关键,直接就是获取到数据
if (dwVerInfoSize > 0)
{
HANDLE     hMem;
LPVOID     lpvMem;
unsigned   int   uInfoSize = 0;


hMem = GlobalAlloc(GMEM_MOVEABLE, dwVerInfoSize);
lpvMem = GlobalLock(hMem);
GetFileVersionInfo(szFullPath, dwVerHnd, dwVerInfoSize, lpvMem);// 用来获得指定文件的版本信息:


if (!::VerQueryValue(lpvMem, (LPTSTR)_T("\\"), (LPVOID*)&pFileInfo, &uInfoSize))//这边的话主要是 用于从指定的版本信息源获取版本信息
{
if (hMem)
  {
GlobalUnlock(hMem);
GlobalFree(hMem);
}
return  QString::fromStdWString(L"");
}


WORD   m_nProdVersion[4];



m_nProdVersion[0] = HIWORD(pFileInfo->dwProductVersionMS);
m_nProdVersion[1] = LOWORD(pFileInfo->dwProductVersionMS);
m_nProdVersion[2] = HIWORD(pFileInfo->dwProductVersionLS);
m_nProdVersion[3] = LOWORD(pFileInfo->dwProductVersionLS);


char vVersion[256] = { 0 };
sprintf(vVersion, "%d.%d.%d.%d", m_nProdVersion[0], m_nProdVersion[1], m_nProdVersion[2], m_nProdVersion[3]);
std::string vVerStr = vVersion;


GlobalUnlock(hMem);
GlobalFree(hMem);


return QString::fromStdString(vVerStr);
}
return QString::fromStdWString(L"");

}


这边有个文档的链接,推荐一看:http://blog.csdn.net/asce1885/article/details/5732024