Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Windows 11 support #322

Merged
merged 1 commit into from Aug 28, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 27 additions & 11 deletions os_info/src/windows/winapi.rs
Expand Up @@ -59,7 +59,7 @@ fn version() -> (Version, Option<String>) {
v.dwMinorVersion as u64,
v.dwBuildNumber as u64,
),
product_name().or_else(|| edition(&v)),
product_name(&v).or_else(|| edition(&v)),
),
}
}
Expand Down Expand Up @@ -125,7 +125,7 @@ fn version_info() -> Option<OSVERSIONINFOEX> {
}
}

fn product_name() -> Option<String> {
fn product_name(info: &OSVERSIONINFOEX) -> Option<String> {
const REG_SUCCESS: LSTATUS = ERROR_SUCCESS as LSTATUS;

let sub_key = to_wide("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion");
Expand All @@ -138,8 +138,14 @@ fn product_name() -> Option<String> {
return None;
}

let is_win_11 = info.dwMajorVersion == 10 && info.dwBuildNumber >= 22000;

// Get size of the data.
let name = to_wide("ProductName");
let name = to_wide(if is_win_11 {
"EditionID"
} else {
"ProductName"
});
let mut data_type: DWORD = 0;
let mut data_size: DWORD = 0;
if unsafe {
Expand Down Expand Up @@ -186,11 +192,15 @@ fn product_name() -> Option<String> {
_ => {}
}

Some(
OsString::from_wide(data.as_slice())
.to_string_lossy()
.into_owned(),
)
let value = OsString::from_wide(data.as_slice())
.to_string_lossy()
.into_owned();

if is_win_11 {
Some(format!("Windows 11 {}", value))
} else {
Some(value)
}
}

fn to_wide(value: &str) -> Vec<WCHAR> {
Expand All @@ -206,7 +216,13 @@ fn edition(version_info: &OSVERSIONINFOEX) -> Option<String> {
version_info.wProductType,
) {
// Windows 10.
(10, 0, VER_NT_WORKSTATION) => Some("Windows 10"),
(10, 0, VER_NT_WORKSTATION) => {
if version_info.dwBuildNumber >= 22000 {
Some("Windows 11")
} else {
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"),
Expand Down Expand Up @@ -283,7 +299,6 @@ mod tests {
#[test]
fn get_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"),
Expand Down Expand Up @@ -351,7 +366,8 @@ mod tests {

#[test]
fn get_product_name() {
let edition = product_name().expect("edition() failed");
let version = version_info().expect("version_info() failed");
let edition = product_name(&version).expect("edition() failed");
assert!(!edition.is_empty());
}

Expand Down