summaryrefslogtreecommitdiffstats
path: root/third_party/rust/whatsys/src/windows.rs
blob: 7e5a22c04871274541143d705b6c2e8813eee1d1 (plain)
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
use libc::{c_char, c_int};

extern "C" {
    fn get_os_release(outbuf: *const c_char, outlen: usize) -> c_int;
    fn get_build_number() -> c_int;
}

/// Get the version of the currently running kernel.
///
/// **Note**: On Windows 8 and later this will report the Windows 8 OS version value `6.2`,
/// unless the final application is build to explicitly target Windows 10.
/// See [`GetVersionEx`] for details.
///
/// [`GetVersionEx`]: https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getversionexa
///
///
/// Returns `None` if an error occured.
pub fn kernel_version() -> Option<String> {
    unsafe {
        // Windows 10 should report "10.0", which is 4 bytes,
        // "unknown" is 7 bytes.
        // We need to account for the null byte.
        let size = 8;
        let mut buf = vec![0; size];
        let written = get_os_release(buf.as_mut_ptr() as _, size) as usize;
        match written {
            0 => None,
            _ => Some(String::from_utf8_lossy(&buf[0..written]).into_owned()),
        }
    }
}

/// Get the build number from Windows.
///
/// **Note**: On Windows 8 and later this may report a Windows 8 build number,
/// for example 9200, unless the final application is build to explicitly
/// target Windows 10.
/// See [`GetVersionEx`] for details.
///
/// [`GetVersionEx`]: https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getversionexa
///
///
/// Returns `None` if an error occured.
pub fn windows_build_number() -> Option<i32> {
    unsafe {
        // Get windows build number
        let build_number = get_build_number();
        match build_number {
            0 => None,
            _ => Some(build_number),
        }
    }
}