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
|
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <windows.h>
/**
* Based on code from sys-info: https://crates.io/crates/sys-info
* Original licenses: MIT
* Original author: Siyu Wang
* License file: https://github.com/FillZpp/sys-info-rs/blob/master/LICENSE
*/
/**
* Get the OS release version.
*
* This uses [`GetVersionEx`] internally.
* Depending on how the application is compiled
* it might return different values for the same operating system.
*
* On Windows 10 the example will return the Windows 8 OS version value `6.2`.
* If compiled into an application which explicitly targets Windows 10, it will
* return `10.0`.
*
* [`GetVersionEx`]: https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getversionexa
*
* ## Arguments
*
* * `outbuf` - Pointer to an allocated buffer of size `outlen`.
* The OS version will be written here, null-terminated.
* If the version is longer than the buffer it will be truncated.
* * `outlen` - Size of the buffer.
*
* ## Return value
*
* Returns the number of bytes written.
*/
int get_os_release(char *outbuf, size_t outlen) {
assert(outlen > 1);
OSVERSIONINFO osvi;
ZeroMemory(&osvi, sizeof(osvi));
osvi.dwOSVersionInfoSize = sizeof(osvi);
int written = 0;
if (GetVersionEx(&osvi)) {
written = snprintf(outbuf, outlen, "%ld.%ld", osvi.dwMajorVersion,
osvi.dwMinorVersion);
} else {
int res = strncpy_s(outbuf, outlen, "unknown", strlen("unknown"));
if (res != 0) {
return 0;
}
written = strlen("unknown");
}
// If the output buffer is smaller than the version (or "unknown"),
// we only wrote until the buffer was full.
return min(written, (int)outlen - 1);
}
/**
* Get the windows build version.
*
* Works similarly to `get_os_release`, but returns the `dwBuildNumber` field from the
* struct returned by `GetVersionEx`.
*
* [`GetVersionEx`]: https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getversionexa
*
* ## Return value
*
* Returns the build number as an int.
*/
int get_build_number() {
OSVERSIONINFO osvi;
ZeroMemory(&osvi, sizeof(osvi));
osvi.dwOSVersionInfoSize = sizeof(osvi);
if (GetVersionEx(&osvi)) {
return osvi.dwBuildNumber;
}
return 0;
}
|