Skip to content

Commit

Permalink
Improve error handling for verify_system_version
Browse files Browse the repository at this point in the history
  • Loading branch information
DrChat committed Jun 23, 2024
1 parent 65e9250 commit 305c36f
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/native/version_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//!
//! At the moment the only option available is to check if the actual System Version is greater than
//! Win8, is the only check we need for the crate to work as expected
use windows::core::HRESULT;
use windows::Win32::Foundation::GetLastError;
use windows::Win32::Foundation::ERROR_OLD_WIN_VERSION;
use windows::Win32::System::SystemInformation::{VerSetConditionMask, VerifyVersionInfoA};
Expand Down Expand Up @@ -50,13 +51,15 @@ fn verify_system_version(major: u8, minor: u8, sp_major: u16) -> VersionHelperRe
)
};

let error = unsafe { GetLastError() }.ok();

// See https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-verifyversioninfoa#return-value
match (res.is_ok(), error) {
(true, _) => Ok(true),
(false, Err(err)) if err.code() == ERROR_OLD_WIN_VERSION.to_hresult() => Ok(false),
(false, _err) => Err(VersionHelperError::IoError(std::io::Error::last_os_error())),
match res {
Ok(_) => Ok(true),
Err(e) => match e.code() {
e if e == HRESULT::from_win32(ERROR_OLD_WIN_VERSION.0) => Ok(false),
_ => Err(VersionHelperError::IoError(
std::io::Error::from_raw_os_error(unsafe { GetLastError() }.0 as i32),
)),
},
}
}

Expand Down

0 comments on commit 305c36f

Please sign in to comment.