| 1 |
#include <assert.h> |
| 2 |
#include <stdio.h> |
| 3 |
#include <string.h> |
| 4 |
#include <windows.h> |
| 5 |
|
| 6 |
/** |
| 7 |
* Based on code from sys-info: https://crates.io/crates/sys-info |
| 8 |
* Original licenses: MIT |
| 9 |
* Original author: Siyu Wang |
| 10 |
* License file: https://github.com/FillZpp/sys-info-rs/blob/master/LICENSE |
| 11 |
*/ |
| 12 |
|
| 13 |
/** |
| 14 |
* Get the OS release version. |
| 15 |
* |
| 16 |
* This uses [`GetVersionEx`] internally. |
| 17 |
* Depending on how the application is compiled |
| 18 |
* it might return different values for the same operating system. |
| 19 |
* |
| 20 |
* On Windows 10 the example will return the Windows 8 OS version value `6.2`. |
| 21 |
* If compiled into an application which explicitly targets Windows 10, it will |
| 22 |
* return `10.0`. |
| 23 |
* |
| 24 |
* [`GetVersionEx`]: https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getversionexa |
| 25 |
* |
| 26 |
* ## Arguments |
| 27 |
* |
| 28 |
* * `outbuf` - Pointer to an allocated buffer of size `outlen`. |
| 29 |
* The OS version will be written here, null-terminated. |
| 30 |
* If the version is longer than the buffer it will be truncated. |
| 31 |
* * `outlen` - Size of the buffer. |
| 32 |
* |
| 33 |
* ## Return value |
| 34 |
* |
| 35 |
* Returns the number of bytes written. |
| 36 |
*/ |
| 37 |
int get_os_release(char *outbuf, size_t outlen) { |
| 38 |
assert(outlen > 1); |
| 39 |
|
| 40 |
OSVERSIONINFO osvi; |
| 41 |
|
| 42 |
ZeroMemory(&osvi, sizeof(osvi)); |
| 43 |
osvi.dwOSVersionInfoSize = sizeof(osvi); |
| 44 |
|
| 45 |
int written = 0; |
| 46 |
if (GetVersionEx(&osvi)) { |
| 47 |
written = snprintf(outbuf, outlen, "%ld.%ld", osvi.dwMajorVersion, |
| 48 |
osvi.dwMinorVersion); |
| 49 |
} else { |
| 50 |
int res = strncpy_s(outbuf, outlen, "unknown", strlen("unknown")); |
| 51 |
if (res != 0) { |
| 52 |
return 0; |
| 53 |
} |
| 54 |
written = strlen("unknown"); |
| 55 |
} |
| 56 |
|
| 57 |
// If the output buffer is smaller than the version (or "unknown"), |
| 58 |
// we only wrote until the buffer was full. |
| 59 |
return min(written, (int)outlen - 1); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Get the windows build version. |
| 64 |
* |
| 65 |
* Works similarly to `get_os_release`, but returns the `dwBuildNumber` field from the |
| 66 |
* struct returned by `GetVersionEx`. |
| 67 |
* |
| 68 |
* [`GetVersionEx`]: https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getversionexa |
| 69 |
* |
| 70 |
* ## Return value |
| 71 |
* |
| 72 |
* Returns the build number as an int. |
| 73 |
*/ |
| 74 |
int get_build_number() { |
| 75 |
OSVERSIONINFO osvi; |
| 76 |
|
| 77 |
ZeroMemory(&osvi, sizeof(osvi)); |
| 78 |
osvi.dwOSVersionInfoSize = sizeof(osvi); |
| 79 |
|
| 80 |
if (GetVersionEx(&osvi)) { |
| 81 |
return osvi.dwBuildNumber; |
| 82 |
} |
| 83 |
|
| 84 |
return 0; |
| 85 |
} |
| 86 |
|