-
Notifications
You must be signed in to change notification settings - Fork 15
/
versioninfo.h
101 lines (92 loc) · 3.39 KB
/
versioninfo.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#pragma once
#include <Windows.h>
#include <string>
#pragma comment(lib, "version.lib")
enum InfoType
{
InfoType_Version,
InfoType_FileDescription,
};
inline std::string get_version(const std::string& filename)
{
DWORD verHandle = 0;
UINT size = 0;
LPBYTE lpBuffer = NULL;
DWORD verSize = GetFileVersionInfoSizeA(filename.c_str(), &verHandle);
if (verSize != NULL)
{
std::string verData;
verData.resize(verSize);
if (GetFileVersionInfoA(filename.c_str(), verHandle, verSize, (LPVOID)verData.data()))
{
if (VerQueryValueA(verData.data(), "\\", (VOID FAR * FAR*)&lpBuffer, &size))
{
if (size)
{
VS_FIXEDFILEINFO* verInfo = (VS_FIXEDFILEINFO*)lpBuffer;
if (verInfo->dwSignature == 0xfeef04bd)
{
// Doesn't matter if you are on 32 bit or 64 bit,
// DWORD is always 32 bits, so first two revision numbers
// come from dwFileVersionMS, last two come from dwFileVersionLS
return std::to_string((verInfo->dwFileVersionMS >> 16) & 0xffff)
+ "." + std::to_string((verInfo->dwFileVersionMS >> 0) & 0xffff)
+ "." + std::to_string((verInfo->dwFileVersionLS >> 16) & 0xffff)
+ "." + std::to_string((verInfo->dwFileVersionLS >> 0) & 0xffff);
}
}
}
}
}
return "";
}
inline std::string get_string_info(const std::string& filename, const std::string& info)
{
DWORD verHandle = 0;
UINT size = 0;
LPBYTE lpBuffer = NULL;
DWORD verSize = GetFileVersionInfoSizeA(filename.c_str(), &verHandle);
if (verSize != NULL)
{
std::string verData;
verData.resize(verSize);
if (GetFileVersionInfoA(filename.c_str(), verHandle, verSize, (LPVOID)verData.data()))
{
if (VerQueryValue(verData.data(), TEXT("\\VarFileInfo\\Translation"), (VOID FAR * FAR*)&lpBuffer, &size))
{
if (size)
{
// Read the list of languages and code pages.
struct LANGANDCODEPAGE
{
WORD wLanguage;
WORD wCodePage;
}* lpTranslate;
lpTranslate = (struct LANGANDCODEPAGE*)lpBuffer;
// Read the file description for each language and code page.
for (UINT i = 0; i < (size / sizeof(struct LANGANDCODEPAGE)); i++)
{
char SubBlock[50];
snprintf(SubBlock, 50, ("\\StringFileInfo\\%04x%04x\\" + info).c_str(), lpTranslate[i].wLanguage, lpTranslate[i].wCodePage);
if (VerQueryValueA(verData.data(), SubBlock, (VOID FAR * FAR*)&lpBuffer, &size))
{
return (const char*)lpBuffer;
}
}
}
}
}
}
}
inline std::string get_version(const std::string& filename, InfoType it)
{
if (it == InfoType_Version)
{
return get_version(filename);
}
else if (it == InfoType_FileDescription)
{
return get_string_info(filename, "FileDescription");
}
return "";
}