Last active
April 3, 2018 11:59
-
-
Save mazhar-ansari-ardeh/068f0ede7533f7848f2c273f9da2edb8 to your computer and use it in GitHub Desktop.
A small function that retrieves file version numbers on Windows platform.
This file contains 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
#include <windows.h> | |
#include <memory> | |
#pragma comment(lib, "version.lib") | |
#define ERR_INVALID_DLL_PATH 0x81000001 | |
#define ERR_INVALID_POINTER 0x81000002 | |
#define ERR_NAME_NOT_FOUND_OR_INVALID_RESOURCE 0x81000003 | |
#define ERR_NO_VALUE_AVAILABLE 0x81000004 | |
#define SUCCESS 0 | |
DWORD GetFileVersion(TCHAR* path, DWORD* fileVersionMS, DWORD* fileVersionLS) | |
{ | |
using namespace std; | |
if (nullptr == path) | |
return ERR_INVALID_DLL_PATH; | |
if (nullptr == fileVersionMS || nullptr == fileVersionLS) | |
return ERR_INVALID_POINTER; | |
DWORD wHandle; | |
// If the function succeeds, the return value is the size, in bytes, of the file's version information. | |
// If the function fails, the return value is zero.To get extended error information, call GetLastError. | |
DWORD versionSize = GetFileVersionInfoSize(path, &wHandle); | |
if (!versionSize) | |
return GetLastError(); | |
unique_ptr<BYTE> versionData(new BYTE[versionSize]()); | |
if (!GetFileVersionInfo(path, wHandle, versionSize, versionData.get())) | |
return GetLastError(); | |
// If the specified version-information structure exists, and version information is available, the return value is | |
// nonzero. If the address of the length buffer is zero, no value is available for the specified version-information | |
// name. | |
// If the specified name does not exist or the specified resource is not valid, the return value is zero. | |
BYTE *dataBuffer = NULL; // The value of this pointer is freed automatically when 'versionData' is freed. | |
UINT dataBufferLen = 0; | |
if (!VerQueryValue(versionData.get(), "\\", (LPVOID*)&dataBuffer, &dataBufferLen)) | |
return ERR_NAME_NOT_FOUND_OR_INVALID_RESOURCE; | |
if (0 == dataBufferLen) | |
return ERR_NO_VALUE_AVAILABLE; | |
VS_FIXEDFILEINFO fileInfo = {}; | |
memcpy_s(&fileInfo, sizeof(fileInfo), dataBuffer, dataBufferLen); | |
*fileVersionMS = fileInfo.dwFileVersionMS; | |
*fileVersionLS = fileInfo.dwFileVersionLS; | |
return SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The function simply shows how to use the
GetFileVersionInfo
andVerQueryValue
functions and can be altered to return other version information of files. Detailed information about these functions are available respectively at:https://msdn.microsoft.com/en-us/library/ms647003.aspx
and
https://msdn.microsoft.com/en-us/library/ms647464.aspx
MSDN pages.