From 305c36f44dad203e4c456f43d6778ad873186aed Mon Sep 17 00:00:00 2001 From: "Dr. Chat" Date: Sun, 23 Jun 2024 13:54:05 -0500 Subject: [PATCH] Improve error handling for `verify_system_version` --- src/native/version_helper.rs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/native/version_helper.rs b/src/native/version_helper.rs index 45ef70c..11474db 100644 --- a/src/native/version_helper.rs +++ b/src/native/version_helper.rs @@ -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}; @@ -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), + )), + }, } }