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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
|
#include "envwindows.h"
#include "env.h"
#include "envmodule.h"
#include <log.h>
#include <utility.h>
namespace env
{
using namespace MOBase;
WindowsInfo::WindowsInfo()
{
// loading ntdll.dll, the functions will be found with GetProcAddress()
LibraryPtr ntdll(LoadLibraryW(L"ntdll.dll"));
if (!ntdll) {
log::error("failed to load ntdll.dll while getting version");
return;
} else {
m_reported = getReportedVersion(ntdll.get());
m_real = getRealVersion(ntdll.get());
}
m_release = getRelease();
m_elevated = getElevated();
}
bool WindowsInfo::compatibilityMode() const
{
if (m_real == Version()) {
// don't know the real version, can't guess compatibility mode
return false;
}
return (m_real != m_reported);
}
const WindowsInfo::Version& WindowsInfo::reportedVersion() const
{
return m_reported;
}
const WindowsInfo::Version& WindowsInfo::realVersion() const
{
return m_real;
}
const WindowsInfo::Release& WindowsInfo::release() const
{
return m_release;
}
std::optional<bool> WindowsInfo::isElevated() const
{
return m_elevated;
}
QString WindowsInfo::toString() const
{
QStringList sl;
const QString reported = m_reported.toString();
const QString real = m_real.toString();
// version
sl.push_back("version " + reported);
// real version if different
if (compatibilityMode()) {
sl.push_back("real version " + real);
}
// build.UBR, such as 17763.557
if (m_release.UBR != 0) {
DWORD build = 0;
if (compatibilityMode()) {
build = m_real.build;
} else {
build = m_reported.build;
}
sl.push_back(QString("%1.%2").arg(build).arg(m_release.UBR));
}
// release ID
if (!m_release.ID.isEmpty()) {
sl.push_back("release " + m_release.ID);
}
// buildlab string
if (!m_release.buildLab.isEmpty()) {
sl.push_back(m_release.buildLab);
}
// elevated
QString elevated = "?";
if (m_elevated.has_value()) {
elevated = (*m_elevated ? "yes" : "no");
}
sl.push_back("elevated: " + elevated);
return sl.join(", ");
}
WindowsInfo::Version WindowsInfo::getReportedVersion(HINSTANCE ntdll) const
{
// windows has been deprecating pretty much all the functions having to do
// with getting version information because apparently, people keep misusing
// them for feature detection
//
// there's still RtlGetVersion() though
using RtlGetVersionType = NTSTATUS(NTAPI)(PRTL_OSVERSIONINFOW);
auto* RtlGetVersion =
reinterpret_cast<RtlGetVersionType*>(GetProcAddress(ntdll, "RtlGetVersion"));
if (!RtlGetVersion) {
log::error("RtlGetVersion() not found in ntdll.dll");
return {};
}
OSVERSIONINFOEX vi = {};
vi.dwOSVersionInfoSize = sizeof(vi);
// this apparently never fails
RtlGetVersion((RTL_OSVERSIONINFOW*)&vi);
return {vi.dwMajorVersion, vi.dwMinorVersion, vi.dwBuildNumber};
}
WindowsInfo::Version WindowsInfo::getRealVersion(HINSTANCE ntdll) const
{
// getting the actual windows version is more difficult because all the
// functions are lying when running in compatibility mode
//
// RtlGetNtVersionNumbers() is an undocumented function that seems to work
// fine, but it might not in the future
using RtlGetNtVersionNumbersType = void(NTAPI)(DWORD*, DWORD*, DWORD*);
auto* RtlGetNtVersionNumbers = reinterpret_cast<RtlGetNtVersionNumbersType*>(
GetProcAddress(ntdll, "RtlGetNtVersionNumbers"));
if (!RtlGetNtVersionNumbers) {
log::error("RtlGetNtVersionNumbers not found in ntdll.dll");
return {};
}
DWORD major = 0, minor = 0, build = 0;
RtlGetNtVersionNumbers(&major, &minor, &build);
// for whatever reason, the build number has 0xf0000000 set
build = 0x0fffffff & build;
return {major, minor, build};
}
WindowsInfo::Release WindowsInfo::getRelease() const
{
// there are several interesting items in the registry, but most of them
// are undocumented, not always available, and localizable
//
// most of them are used to provide as much information as possible in case
// any of the other versions fail to work
QSettings settings(
R"(HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion)",
QSettings::NativeFormat);
Release r;
// buildlab seems to be an internal name from the build system
r.buildLab = settings.value("BuildLabEx", "").toString();
if (r.buildLab.isEmpty()) {
r.buildLab = settings.value("BuildLab", "").toString();
if (r.buildLab.isEmpty()) {
r.buildLab = settings.value("BuildBranch", "").toString();
}
}
// release ID, such as 1803
r.ID = settings.value("DisplayVersion", "").toString();
if (r.ID.isEmpty()) {
r.ID = settings.value("ReleaseId", "").toString();
}
// some other build number, shown in winver.exe
r.UBR = settings.value("UBR", 0).toUInt();
return r;
}
std::optional<bool> WindowsInfo::getElevated() const
{
HandlePtr token;
{
HANDLE rawToken = 0;
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &rawToken)) {
const auto e = GetLastError();
log::error("while trying to check if process is elevated, "
"OpenProcessToken() failed: {}",
formatSystemMessage(e));
return {};
}
token.reset(rawToken);
}
TOKEN_ELEVATION e = {};
DWORD size = sizeof(TOKEN_ELEVATION);
if (!GetTokenInformation(token.get(), TokenElevation, &e, sizeof(e), &size)) {
const auto e = GetLastError();
log::error("while trying to check if process is elevated, "
"GetTokenInformation() failed: {}",
formatSystemMessage(e));
return {};
}
return (e.TokenIsElevated != 0);
}
} // namespace env
|