Skip to content

Instantly share code, notes, and snippets.

@superlucky8848
Created April 4, 2016 02:25
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save superlucky8848/421afc4d3436ed09a21997126759e251 to your computer and use it in GitHub Desktop.
Save superlucky8848/421afc4d3436ed09a21997126759e251 to your computer and use it in GitHub Desktop.
C++ Get file version and return formatted string(Windows)
#pragma comment(lib,"Version.lib")
#define MAX_LINE_LENGTH 4096
CString GetFileVersion(LPCTSTR filePath)
{
CString ret(_T(""));
DWORD dummy;
DWORD dwSize = GetFileVersionInfoSize(filePath, &dummy);
if (dwSize == 0) ret = _T("Error retriving version(Module Not Found)");
else
{
BYTE* data = new BYTE[dwSize];
if (!GetFileVersionInfo(filePath, NULL, dwSize, &data[0]))
ret = _T("Error retriving version(Version Data Not Retrived)");
else
{
UINT infoLen = 0;
VS_FIXEDFILEINFO * pFixedInfo = NULL;
if (!VerQueryValue(&data[0], _T("\\"), (LPVOID *)&pFixedInfo, &infoLen)) ret = _T("Error retriving version(Version Data Query Failed)");
else
{
ret.Format(_T("%u.%u.%u(%u)"),
HIWORD(pFixedInfo->dwFileVersionMS),
LOWORD(pFixedInfo->dwFileVersionMS),
HIWORD(pFixedInfo->dwFileVersionLS),
LOWORD(pFixedInfo->dwFileVersionLS));
}
}
delete[] data;
}
return ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment