-
Notifications
You must be signed in to change notification settings - Fork 9
/
FileVersion.cpp
163 lines (134 loc) · 4.04 KB
/
FileVersion.cpp
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// FileVersion.cpp: implementation of the CFileVersion class.
//
// This code comes from www.codeguru.com
// by Manuel Laflamme (mailto:[email protected])
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "FileVersion.h"
#pragma comment(lib, "version")
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
CFileVersion::CFileVersion()
{
m_lpVersionData = NULL;
m_dwLangCharset = 0;
}
CFileVersion::~CFileVersion()
{
Close();
}
void CFileVersion::Close()
{
delete[] m_lpVersionData;
m_lpVersionData = NULL;
m_dwLangCharset = 0;
}
BOOL CFileVersion::Open(LPCTSTR lpszModuleName)
{
ASSERT(_tcslen(lpszModuleName) > 0);
ASSERT(m_lpVersionData == NULL);
// Get the version information size for allocate the buffer
DWORD dwHandle;
DWORD dwDataSize = ::GetFileVersionInfoSize((LPTSTR)lpszModuleName, &dwHandle);
if ( dwDataSize == 0 )
return FALSE;
// Allocate buffer and retrieve version information
m_lpVersionData = new BYTE[dwDataSize];
if (!::GetFileVersionInfo((LPTSTR)lpszModuleName, dwHandle, dwDataSize,
(void**)m_lpVersionData) )
{
Close();
return FALSE;
}
// Retrieve the first language and character-set identifier
UINT nQuerySize;
DWORD* pTransTable;
if (!::VerQueryValue(m_lpVersionData, _T( "\\VarFileInfo\\Translation"),
(void **)&pTransTable, &nQuerySize) )
{
Close();
return FALSE;
}
// Swap the words to have lang-charset in the correct format
m_dwLangCharset = MAKELONG(HIWORD(pTransTable[0]), LOWORD(pTransTable[0]));
return TRUE;
}
CString CFileVersion::QueryValue(LPCTSTR lpszValueName,
DWORD dwLangCharset /* = 0*/)
{
// Must call Open() first
ASSERT(m_lpVersionData != NULL);
if ( m_lpVersionData == NULL )
return (CString)_T( "");
// If no lang-charset specified use default
if ( dwLangCharset == 0 )
dwLangCharset = m_dwLangCharset;
// Query version information value
UINT nQuerySize;
LPVOID lpData;
CString strValue, strBlockName;
strBlockName.Format(_T( "\\StringFileInfo\\%08lx\\%s"),
dwLangCharset, lpszValueName);
if ( ::VerQueryValue((void **)m_lpVersionData, strBlockName.GetBuffer(0),
&lpData, &nQuerySize) )
strValue = (LPCTSTR)lpData;
strBlockName.ReleaseBuffer();
return strValue;
}
BOOL CFileVersion::GetFixedInfo(VS_FIXEDFILEINFO& vsffi)
{
// Must call Open() first
ASSERT(m_lpVersionData != NULL);
if ( m_lpVersionData == NULL )
return FALSE;
UINT nQuerySize;
VS_FIXEDFILEINFO* pVsffi;
if ( ::VerQueryValue((void **)m_lpVersionData, _T( "\\"),
(void**)&pVsffi, &nQuerySize) )
{
vsffi = *pVsffi;
return TRUE;
}
return FALSE;
}
CString CFileVersion::GetFixedFileVersion()
{
CString strVersion;
VS_FIXEDFILEINFO vsffi;
if ( GetFixedInfo(vsffi) )
{
strVersion.Format( _T( "%u.%u.%u.%u"),HIWORD(vsffi.dwFileVersionMS),
LOWORD(vsffi.dwFileVersionMS),
HIWORD(vsffi.dwFileVersionLS),
LOWORD(vsffi.dwFileVersionLS));
}
return strVersion;
}
CString CFileVersion::GetFixedProductVersion()
{
CString strVersion;
VS_FIXEDFILEINFO vsffi;
if ( GetFixedInfo(vsffi) )
{
strVersion.Format( _T( "%u.%u.%u.%u"), HIWORD(vsffi.dwProductVersionMS),
LOWORD(vsffi.dwProductVersionMS),
HIWORD(vsffi.dwProductVersionLS),
LOWORD(vsffi.dwProductVersionLS));
}
return strVersion;
}
CString CFileVersion::GetProductLanguage()
{
CString strLanguage;
DWORD nSize = 256;
TCHAR lpData[256];
DWORD nResult;
nResult = VerLanguageName(m_dwLangCharset, lpData, nSize);
if ((nResult != 0) && (nResult < nSize - 1))
strLanguage = (LPCTSTR)lpData;
return strLanguage;
}