Created
April 4, 2016 02:25
C++ Get file version and return formatted string(Windows)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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