-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathwinapi.rs
162 lines (139 loc) · 5.42 KB
/
winapi.rs
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// spell-checker:ignore dword, minwindef, ntdef, ntdll, ntstatus, osversioninfoex, osversioninfoexa
// spell-checker:ignore osversioninfoexw, serverr, sysinfoapi, winnt, winuser
#![allow(unsafe_code)]
use kernel32::GetSystemInfo;
use user32::GetSystemMetrics;
#[cfg(target_arch = "x86")]
use winapi::winnt::OSVERSIONINFOEXA;
#[cfg(not(target_arch = "x86"))]
use winapi::winnt::OSVERSIONINFOEXW;
use winapi::{minwindef::DWORD, ntdef::NTSTATUS, ntstatus::STATUS_SUCCESS, sysinfoapi::SYSTEM_INFO,
winuser::SM_SERVERR2};
use std::mem;
use {Info, Type, Version};
#[cfg(target_arch = "x86")]
type OSVERSIONINFOEX = OSVERSIONINFOEXA;
#[cfg(not(target_arch = "x86"))]
type OSVERSIONINFOEX = OSVERSIONINFOEXW;
/// Win32 Flag: VER_NT_WORKSTATION
/// https://msdn.microsoft.com/en-us/library/windows/desktop/ms724833(v=vs.85).aspx
const VER_NT_WORKSTATION: u8 = 0x0000001;
/// Win32 Flag: VER_SUITE_WH_SERVER
/// https://msdn.microsoft.com/en-us/library/windows/desktop/ms724833(v=vs.85).aspx
const VER_SUITE_WH_SERVER: u16 = 0x00008000;
/// Win32 Flag: PROCESSOR_ARCHITECTURE_AMD64
/// https://msdn.microsoft.com/en-us/library/windows/desktop/ms724958(v=vs.85).aspx
const PROCESSOR_ARCHITECTURE_AMD64: u16 = 9;
#[link(name = "ntdll")]
extern "C" {
pub fn RtlGetVersion(lpVersionInformation: &mut OSVERSIONINFOEX) -> NTSTATUS;
}
pub fn get() -> Info {
let mut info = Info::new(Type::Windows, Version::unknown());
let version_info = match get_version_info() {
None => {
return info;
}
Some(val) => val,
};
info.version = Version::semantic(
version_info.dwMajorVersion as u64,
version_info.dwMinorVersion as u64,
version_info.dwBuildNumber as u64,
get_edition(&version_info),
);
info
}
// Calls the Win32 API function RtlGetVersion to get the OS version information:
// https://msdn.microsoft.com/en-us/library/mt723418(v=vs.85).aspx
fn get_version_info() -> Option<OSVERSIONINFOEX> {
let mut info: OSVERSIONINFOEX = unsafe { mem::zeroed() };
info.dwOSVersionInfoSize = mem::size_of::<OSVERSIONINFOEX>() as DWORD;
if unsafe { RtlGetVersion(&mut info) } == STATUS_SUCCESS {
Some(info)
} else {
None
}
}
// Examines data in the OSVERSIONINFOEX structure to determine the Windows edition:
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms724833(v=vs.85).aspx
fn get_edition(version_info: &OSVERSIONINFOEX) -> Option<String> {
match (
version_info.dwMajorVersion,
version_info.dwMinorVersion,
version_info.wProductType,
) {
// Windows 10.
(10, 0, VER_NT_WORKSTATION) => Some("Windows 10"),
(10, 0, _) => Some("Windows Server 2016"),
// Windows Vista, 7, 8 and 8.1.
(6, 3, VER_NT_WORKSTATION) => Some("Windows 8.1"),
(6, 3, _) => Some("Windows Server 2012 R2"),
(6, 2, VER_NT_WORKSTATION) => Some("Windows 8"),
(6, 2, _) => Some("Windows Server 2012"),
(6, 1, VER_NT_WORKSTATION) => Some("Windows 7"),
(6, 1, _) => Some("Windows Server 2008 R2"),
(6, 0, VER_NT_WORKSTATION) => Some("Windows Vista"),
(6, 0, _) => Some("Windows Server 2008"),
// Windows 2000, Home Server, 2003 Server, 2003 R2 Server, XP and XP Professional x64.
(5, 1, _) => Some("Windows XP"),
(5, 0, _) => Some("Windows 2000"),
(5, 2, _) if unsafe { GetSystemMetrics(SM_SERVERR2) } == 0 => {
let mut info: SYSTEM_INFO = unsafe { mem::zeroed() };
unsafe { GetSystemInfo(&mut info) };
if version_info.wSuiteMask & VER_SUITE_WH_SERVER == VER_SUITE_WH_SERVER {
Some("Windows Home Server")
} else if version_info.wProductType == VER_NT_WORKSTATION
&& info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64
{
Some("Windows XP Professional x64 Edition")
} else {
Some("Windows Server 2003")
}
}
_ => None,
}.map(str::to_string)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn version() {
let info = get();
assert_eq!(Type::Windows, info.os_type());
}
#[test]
fn version_info() {
let version = get_version_info();
assert!(version.is_some());
}
#[test]
fn edition() {
let test_data = [
(10, 0, VER_NT_WORKSTATION, "Windows 10"),
(10, 0, 0, "Windows Server 2016"),
(6, 3, VER_NT_WORKSTATION, "Windows 8.1"),
(6, 3, 0, "Windows Server 2012 R2"),
(6, 2, VER_NT_WORKSTATION, "Windows 8"),
(6, 2, 0, "Windows Server 2012"),
(6, 1, VER_NT_WORKSTATION, "Windows 7"),
(6, 1, 0, "Windows Server 2008 R2"),
(6, 0, VER_NT_WORKSTATION, "Windows Vista"),
(6, 0, 0, "Windows Server 2008"),
(5, 1, 0, "Windows XP"),
(5, 1, 1, "Windows XP"),
(5, 1, 100, "Windows XP"),
(5, 0, 0, "Windows 2000"),
(5, 0, 1, "Windows 2000"),
(5, 0, 100, "Windows 2000"),
];
let mut info = get_version_info().unwrap();
for &(major, minor, product_type, expected_edition) in &test_data {
info.dwMajorVersion = major;
info.dwMinorVersion = minor;
info.wProductType = product_type;
let edition = get_edition(&info).unwrap();
assert_eq!(edition, expected_edition);
}
}
}