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
use super::bindings::Windows::Win32::{SystemServices, WindowsProgramming};
use crate::traits::*;
#[derive(Debug)]
pub enum VersionHelperError {
IoError(std::io::Error),
}
impl LastOsError<VersionHelperError> for VersionHelperError {}
impl From<std::io::Error> for VersionHelperError {
fn from(err: std::io::Error) -> Self {
VersionHelperError::IoError(err)
}
}
pub(crate) type VersionHelperResult<T> = Result<T, VersionHelperError>;
type OsVersionInfo = WindowsProgramming::OSVERSIONINFOEXA;
const VER_GREATER_OR_EQUAL: u8 = SystemServices::VER_GREATER_EQUAL as u8;
fn verify_system_version(major: u8, minor: u8, sp_major: u16) -> VersionHelperResult<bool> {
let mut os_version = OsVersionInfo::default();
os_version.dwOSVersionInfoSize = std::mem::size_of::<OsVersionInfo>() as u32;
os_version.dwMajorVersion = major as u32;
os_version.dwMajorVersion = minor as u32;
os_version.wServicePackMajor = sp_major;
let mut condition_mask = 0;
unsafe {
condition_mask = WindowsProgramming::VerSetConditionMask(
condition_mask,
WindowsProgramming::VER_MASK::VER_MAJORVERSION,
VER_GREATER_OR_EQUAL,
);
condition_mask = WindowsProgramming::VerSetConditionMask(
condition_mask,
WindowsProgramming::VER_MASK::VER_MINORVERSION,
VER_GREATER_OR_EQUAL,
);
condition_mask = WindowsProgramming::VerSetConditionMask(
condition_mask,
WindowsProgramming::VER_MASK::VER_SERVICEPACKMAJOR,
VER_GREATER_OR_EQUAL,
);
Ok(WindowsProgramming::VerifyVersionInfoA(
&mut os_version,
WindowsProgramming::VER_MASK::VER_MAJORVERSION
| WindowsProgramming::VER_MASK::VER_MINORVERSION
| WindowsProgramming::VER_MASK::VER_SERVICEPACKMAJOR,
condition_mask,
) != false)
}
}
pub fn is_win8_or_greater() -> bool {
let res = match verify_system_version(6, 2, 0) {
Ok(res) => res,
Err(err) => {
println!("{:?}", err);
true
}
};
res
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_verify_system_version() {
match verify_system_version(5, 1, 0) {
Ok(res) => assert_eq!(true, res),
Err(err) => panic!("VersionHelper error: {:?}", err),
};
}
}