-
Notifications
You must be signed in to change notification settings - Fork 0
/
OSVersion.bf
430 lines (372 loc) · 13.4 KB
/
OSVersion.bf
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
using System;
namespace beef_utils
{
public struct OSVersion
{
#if BF_PLATFORM_WINDOWS
private const String sVersionStr = "{} (Version {}.{}, Build {}, {})";
private const String sSPVersionStr = "{} Service Pack {} (Version {}.{}, Build {}, {})";
private const String sVersion32 = "32-bit Edition";
private const String sVersion64 = "64-bit Edition";
private const String sWindows = "Windows";
private const String sWindowsVista = "Windows Vista";
private const String sWindowsServer2008 = "Windows Server 2008";
private const String sWindows7 = "Windows 7";
private const String sWindowsServer2008R2 = "Windows Server 2008 R2";
private const String sWindows2000 = "Windows 2000";
private const String sWindowsXP = "Windows XP";
private const String sWindowsServer2003 = "Windows Server 2003";
private const String sWindowsServer2003R2 = "Windows Server 2003 R2";
private const String sWindowsServer2012 = "Windows Server 2012";
private const String sWindowsServer2012R2 = "Windows Server 2012 R2";
private const String sWindowsServer2016 = "Windows Server 2016";
private const String sWindows8 = "Windows 8";
private const String sWindows8Point1 = "Windows 8.1";
private const String sWindows10 = "Windows 10";
private const uint8 VER_PRODUCT_TYPE = 0x00000080;
private const uint8 VER_NT_WORKSTATION = 0x00000001;
[CRepr]
struct TOSVersionInfoA {
public uint32 DwOSVersionInfoSize;
public uint32 DwMajorVersion;
public uint32 DwMinorVersion;
public uint32 DwBuildNumber;
public uint32 DwPlatformId;
public uint8[128] SzCSDVersion; // Maintenance UnicodeString for PSS usage
}
[CRepr]
struct TOSVersionInfoW {
public uint32 DwOSVersionInfoSize;
public uint32 DwMajorVersion;
public uint32 DwMinorVersion;
public uint32 DwBuildNumber;
public uint32 DwPlatformId;
public uint16[128] SzCSDVersion; // Maintenance UnicodeString for PSS usage
}
typealias TOSVersionInfo = TOSVersionInfoW;
[CRepr]
struct TOSVersionInfoExA : TOSVersionInfoA {
public uint16 WServicePackMajor;
public uint16 WServicePackMinor;
public uint16 WSuiteMask;
public uint8 WProductType;
public uint8 WReserved;
}
[CRepr]
struct TOSVersionInfoExW : TOSVersionInfoW
{
public uint16 WServicePackMajor;
public uint16 WServicePackMinor;
public uint16 WSuiteMask;
public uint8 WProductType;
public uint8 WReserved;
}
typealias TOSVersionInfoEx = TOSVersionInfoExW;
[CRepr]
struct TSystemInfo {
public uint16 WProcessorArchitecture;
public uint16 WReserved;
public uint32 DwPageSize;
public void* LpMinimumApplicationAddress;
public void* LpMaximumApplicationAddress;
public uint32* DwActiveProcessorMask;
public uint32 DwNumberOfProcessors;
public uint32 DwProcessorType;
public uint32 DwAllocationGranularity;
public uint16 WProcessorLevel;
public uint16 WProcessorRevision;
}
[CRepr]
struct WKSTA_INFO_100
{
public uint32 wki100_platform_id;
public uint32 wki100_computername;
public uint32 wki100_langroup;
public uint32 wki100_ver_major;
public uint32 wki100_ver_minor;
}
typealias LPWKSTA_INFO_100 = WKSTA_INFO_100*;
[CRepr]
struct TVSFixedFileInfo
{
public uint32 dwSignature; // e.g. $feef04bd
public uint32 dwStrucVersion; // e.g. $00000042 = "0.42"
public uint32 dwFileVersionMS; // e.g. $00030075 = "3.75"
public uint32 dwFileVersionLS; // e.g. $00000031 = "0.31"
public uint32 dwProductVersionMS; // e.g. $00030010 = "3.10"
public uint32 dwProductVersionLS; // e.g. $00000031 = "0.31"
public uint32 dwFileFlagsMask; // = $3F for version "0.42"
public uint32 dwFileFlags; // e.g. VFF_DEBUG | VFF_PRERELEASE
public uint32 dwFileOS; // e.g. VOS_DOS_WINDOWS16
public uint32 dwFileType; // e.g. VFT_DRIVER
public uint32 dwFileSubtype; // e.g. VFT2_DRV_KEYBOARD
public uint32 dwFileDateMS; // e.g. 0
public uint32 dwFileDateLS; // e.g. 0
}
typealias PVSFixedFileInfo = TVSFixedFileInfo*;
#elif BF_PLATFORM_LINUX
outVar.AppendF(sVersionStr, fPrettyName, fName, fMajor, fMinor, fServicePackMajor);
private const String sVersionStr = "{} {} (Version {}.{}.{})";
#else // MACOS and ANDROID
outVar.AppendF(sVersionStr, fName, fMajor, fMinor, fServicePackMajor);
private const String sVersionStr = "{} (Version {}.{}.{})";
#endif
public enum TArchitecture
{
X86,
X64,
ARM32,
ARM64
}
public enum TPlatform
{
Windows,
MacOS,
iOS,
Android,
Linux
}
private static TArchitecture fArchitecture;
private static uint fBuild;
private static uint fMajor;
private static uint fMinor;
private static String fName;
private static TPlatform fPlatform;
private static uint fServicePackMajor;
private static uint fServicePackMinor;
#if BF_PLATFORM_LINUX
private static String fPrettyName;
private static uint fLibCVersionMajor;
private static uint fLibCVersionMinor;
#endif
[Import("Kernel32.lib"), CLink, StdCall]
private extern static bool GetVersionExA(TOSVersionInfoA* lpVersionInformation);
[Import("Kernel32.lib"), CLink, StdCall]
private extern static bool GetVersionExW(TOSVersionInfoW* lpVersionInformation);
[Import("Kernel32.lib"), CLink, StdCall]
private extern static bool GetVersionExA(TOSVersionInfoExA* lpVersionInformation);
[Import("Kernel32.lib"), CLink, StdCall]
private extern static bool GetVersionExW(TOSVersionInfoExW* lpVersionInformation);
[Import("Kernel32.lib"), CLink, StdCall]
private extern static bool VerifyVersionInfoA(TOSVersionInfoEx* lpVersionInformation, uint32 dwTypeMask, uint64 dwlConditionMask);
[Import("Kernel32.lib"), CLink, StdCall]
private extern static bool VerifyVersionInfoW(TOSVersionInfoEx* lpVersionInformation, uint32 dwTypeMask, uint64 dwlConditionMask);
[Import("Kernel32.lib"), CLink, StdCall]
private extern static uint64 VerSetConditionMask(uint64 dwlConditionMask, uint32 dwTypeBitMask, uint8 dwConditionMask);
[Import("Kernel32.lib"), CLink, StdCall]
private extern static void GetNativeSystemInfo(TSystemInfo* lpSystemInformation);
[Import("Kernel32.lib"), CLink, StdCall]
private extern static void GetSystemInfo(TSystemInfo* lpSystemInfo);
[Import("netapi32.lib"), CLink, StdCall]
private extern static uint32 NetWkstaGetInfo(char16* ServerName, uint32 Level, out LPWKSTA_INFO_100 BufPtr);
[Import("netapi32.lib"), CLink, StdCall]
private extern static int32 NetApiBufferFree(LPWKSTA_INFO_100 BufPtr);
[Import("Kernel32.lib"), CLink, StdCall]
private extern static uint32 GetFileVersionInfoSizeA(char8* lptstrFilename, uint32* lpdwHandle);
[Import("Kernel32.lib"), CLink, StdCall]
private extern static uint32 GetFileVersionInfoSizeW(char16* lptstrFilename, uint32* lpdwHandle);
[Import("Version.lib"), CLink, StdCall]
private extern static bool GetFileVersionInfoA(char8* lptstrFilename, out uint32 dwHandle, uint32 dwLen, void* lpData);
[Import("Version.lib"), CLink, StdCall]
private extern static bool GetFileVersionInfoW(char16* lptstrFilename, out uint32 dwHandle, uint32 dwLen, void* lpData);
[Import("Version.lib"), CLink, StdCall]
private extern static bool VerQueryValueA(void* pBlock, char8* lpSubBlock, void** lplpBuffer, out uint32 puLen);
[Import("Version.lib"), CLink, StdCall]
private extern static bool VerQueryValueW(void* pBlock, char16* lpSubBlock, void** lplpBuffer, out uint32 puLen);
[Import("User32.lib"), CLink, StdCall]
private extern static int GetSystemMetrics(int nIndex);
public static this()
{
#if BF_PLATFORM_WINDOWS
bool isWinSrv()
{
TOSVersionInfoEx osvi = .();
uint64 dwlCondMask;
osvi.WProductType = VER_NT_WORKSTATION;
dwlCondMask = VerSetConditionMask(0, VER_PRODUCT_TYPE, 1 /* VER_EQUAL */);
return VerifyVersionInfoW(&osvi, VER_PRODUCT_TYPE, dwlCondMask) == false;
}
bool GetProductVersion(String filename, out uint32 major, out uint32 minor, out uint32 build)
{
bool result = false;
major = 0;
minor = 0;
build = 0;
uint32 VerSize, Wnd;
PVSFixedFileInfo FI = &TVSFixedFileInfo();
uint32 InfoSize = GetFileVersionInfoSizeA(filename.CStr(), &Wnd);
if (InfoSize != 0) {
void* VerBuf = Internal.StdMalloc(InfoSize);
if (GetFileVersionInfoA(filename.CStr(), out Wnd, InfoSize, VerBuf)) {
if (VerQueryValueA(VerBuf, "\\", (void**)(&FI), out VerSize)) {
major = FI.dwProductVersionMS >> 16;
minor = (uint16)FI.dwProductVersionMS;
build = FI.dwProductVersionLS >> 16;
result = true;
}
}
Internal.StdFree(VerBuf);
}
return result;
}
bool GetNetWkstaMajorMinor(out uint32 major, out uint32 minor)
{
LPWKSTA_INFO_100 LBuf = ?;
bool result = NetWkstaGetInfo(null, 100, out LBuf) == 0;
if (result) {
major = LBuf.wki100_ver_major;
minor = LBuf.wki100_ver_minor;
} else {
major = 0;
minor = 0;
}
if (LBuf != null)
NetApiBufferFree(LBuf);
return result;
}
TSystemInfo SysInfo = .();
TOSVersionInfoEx VerInfo = .();
uint32 MajorNum, MinorNum, BuildNum;
VerInfo.DwOSVersionInfoSize = sizeof(TOSVersionInfoEx);
GetVersionExW(&VerInfo);
fPlatform = .Windows;
fMajor = VerInfo.DwMajorVersion;
fMinor = VerInfo.DwMinorVersion;
fBuild = VerInfo.DwBuildNumber;
fServicePackMajor = VerInfo.WServicePackMajor;
fServicePackMinor = VerInfo.WServicePackMinor;
if (Check(5, 1)) // GetNativeSystemInfo not supported on Windows 2000
GetNativeSystemInfo(&SysInfo);
fArchitecture = SysInfo.WProcessorArchitecture == 9 /* PROCESSOR_ARCHITECTURE_AMD64 */ ? .X64 : .X86;
if ((fMajor > 6) || ((fMajor == 6) && (fMinor > 1))) {
if (GetProductVersion("kernel32.dll", out MajorNum, out MinorNum, out BuildNum)) {
fMajor = MajorNum;
fMinor = MinorNum;
fBuild = BuildNum;
} else if (GetNetWkstaMajorMinor(out MajorNum, out MinorNum)) {
fMajor = MajorNum;
fMinor = MinorNum;
}
}
fName = sWindows;
switch(fMajor) {
case 10:
switch(fMinor) {
case 0: fName = !isWinSrv() ? sWindows10 : sWindowsServer2016;
// Server 2019 is also 10.0
}
break;
case 6:
switch(fMinor) {
case 0: fName = VerInfo.WProductType == VER_NT_WORKSTATION ? sWindowsVista : sWindowsServer2008;
case 1: fName = VerInfo.WProductType == VER_NT_WORKSTATION ? sWindows7 : sWindowsServer2008R2;
case 2: fName = VerInfo.WProductType == VER_NT_WORKSTATION ? sWindows8 : sWindowsServer2012;
case 3: fName = !isWinSrv() ? sWindows8Point1 : sWindowsServer2012R2;
}
break;
case 5:
switch(fMinor) {
case 0: fName = sWindows2000;
case 1: fName = sWindowsXP;
case 2:
{
if ((VerInfo.WProductType == VER_NT_WORKSTATION) &&
(SysInfo.WProcessorArchitecture == 9 /* PROCESSOR_ARCHITECTURE_AMD64 */)) {
fName = sWindowsXP;
} else {
fName = GetSystemMetrics(89 /* SM_SERVERR2 */) == 0 ? sWindowsServer2003 : sWindowsServer2003R2;
}
}
}
break;
}
#endif
}
[Inline]
public static bool Check(uint major)
{
return fMajor == major;
}
[Inline]
public static bool Check(uint major, uint minor)
{
return (fMajor > major) || ((fMajor == major) && (fMinor >= minor));
}
[Inline]
public static bool Check(uint major, uint minor, uint svcPackMajor)
{
return (fMajor > major) || ((fMajor == major) && (fMinor > minor)) ||
((fMajor == major) && (fMinor == minor) && (fServicePackMajor >= svcPackMajor));
}
[Inline]
public static bool Check(uint major, uint minor, uint svcPackMajor, uint svcPackMinor)
{
return (fMajor > major) || ((fMajor == major) && (fMinor > minor)) ||
((fMajor == major) && (fMinor == minor) && (fServicePackMajor > svcPackMajor)) ||
((fMajor == major) && (fMinor == minor) && (fServicePackMajor == svcPackMajor) && (fServicePackMinor >= svcPackMinor));
}
public override void ToString(String outVar)
{
#if BF_PLATFORM_WINDOWS
String arch = fArchitecture == .X86 ? sVersion32 : sVersion64;
if (fServicePackMajor == 0) {
outVar.AppendF(sVersionStr, fName, fMajor, fMinor, fBuild, arch);
} else {
outVar.AppendF(sSPVersionStr, fName, fServicePackMajor, fMajor, fMinor, fBuild, arch);
}
#elif BF_PLATFORM_LINUX
outVar.AppendF(sVersionStr, fPrettyName, fName, fMajor, fMinor, fServicePackMajor);
#else // MACOS and ANDROID
outVar.AppendF(sVersionStr, fName, fMajor, fMinor, fServicePackMajor);
#endif
}
public TArchitecture Architecture()
{
return fArchitecture;
}
public uint Build()
{
return fBuild;
}
public uint Major()
{
return fMajor;
}
public uint Minor()
{
return fMinor;
}
public void Name(String outVal)
{
outVal.Clear();
outVal.Append(fName);
}
public TPlatform Platform()
{
return fPlatform;
}
public uint ServicePackMajor()
{
return fServicePackMajor;
}
public uint ServicePackMinor()
{
return fServicePackMinor;
}
#if BF_PLATFORM_LINUX
public void PrettyName(String outVal)
{
outVal.Clear();
outVal.Append(fPrettyName);
}
public uint LibCVersionMajor()
{
return fLibCVersionMajor;
}
public uint LibCVersionMinor()
{
return fLibCVersionMinor;
}
#endif
}
}