| 1 |
use libc::{c_char, c_int}; |
| 2 |
|
| 3 |
extern "C" { |
| 4 |
fn get_os_release(outbuf: *const c_char, outlen: usize) -> c_int; |
| 5 |
fn get_build_number() -> c_int; |
| 6 |
} |
| 7 |
|
| 8 |
/// Get the version of the currently running kernel. |
| 9 |
/// |
| 10 |
/// **Note**: On Windows 8 and later this will report the Windows 8 OS version value `6.2`, |
| 11 |
/// unless the final application is build to explicitly target Windows 10. |
| 12 |
/// See [`GetVersionEx`] for details. |
| 13 |
/// |
| 14 |
/// [`GetVersionEx`]: https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getversionexa |
| 15 |
/// |
| 16 |
/// |
| 17 |
/// Returns `None` if an error occured. |
| 18 |
pub fn kernel_version() -> Option<String> { |
| 19 |
unsafe { |
| 20 |
// Windows 10 should report "10.0", which is 4 bytes, |
| 21 |
// "unknown" is 7 bytes. |
| 22 |
// We need to account for the null byte. |
| 23 |
let size = 8; |
| 24 |
let mut buf = vec![0; size]; |
| 25 |
let written = get_os_release(buf.as_mut_ptr() as _, size) as usize; |
| 26 |
match written { |
| 27 |
0 => None, |
| 28 |
_ => Some(String::from_utf8_lossy(&buf[0..written]).into_owned()), |
| 29 |
} |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
/// Get the build number from Windows. |
| 34 |
/// |
| 35 |
/// **Note**: On Windows 8 and later this may report a Windows 8 build number, |
| 36 |
/// for example 9200, unless the final application is build to explicitly |
| 37 |
/// target Windows 10. |
| 38 |
/// See [`GetVersionEx`] for details. |
| 39 |
/// |
| 40 |
/// [`GetVersionEx`]: https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getversionexa |
| 41 |
/// |
| 42 |
/// |
| 43 |
/// Returns `None` if an error occured. |
| 44 |
pub fn windows_build_number() -> Option<i32> { |
| 45 |
unsafe { |
| 46 |
// Get windows build number |
| 47 |
let build_number = get_build_number(); |
| 48 |
match build_number { |
| 49 |
0 => None, |
| 50 |
_ => Some(build_number), |
| 51 |
} |
| 52 |
} |
| 53 |
} |