Skip to content

Commit

Permalink
fix(win32): fake errors? maybe? fucking windows api
Browse files Browse the repository at this point in the history
  • Loading branch information
eythaann committed Dec 21, 2024
1 parent 0aef6f1 commit fe896b9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 20 deletions.
28 changes: 28 additions & 0 deletions src/background/error_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,32 @@ impl From<tauri_plugin_shell::process::Output> for AppError {
}
}

pub trait WindowsResultExt {
/// Call this when convertion a `BOOL` into a result using the win32 crate `BOOL::ok()`
///
/// For some reason `BOOL` is 0 that means failure, but the error code in the `Result` is `0`
/// and message is `succesfully completed`
///
/// Warn: Be careful when using this like win32 api documentation sometimes expect this type of behaviours...
fn filter_fake_error(self) -> core::result::Result<(), windows::core::Error>;
}

impl WindowsResultExt for core::result::Result<(), windows::core::Error> {
fn filter_fake_error(self) -> core::result::Result<(), windows::core::Error> {
match self {
Ok(_) => Ok(()),
Err(error) => {
// I really hate windows api for this types of behaviours
if error.code().is_ok() {
let app_error = AppError::from(error);
log::warn!("(maybe?) fake win32 error, was skipped: {:?}", app_error);
Ok(())
} else {
Err(error)
}
}
}
}
}

pub type Result<T = ()> = core::result::Result<T, AppError>;
31 changes: 11 additions & 20 deletions src/background/windows_api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ use windows::{
};

use crate::{
error_handler::Result,
error_handler::{Result, WindowsResultExt},
hook::HookManager,
modules::input::{domain::Point, Mouse},
utils::{is_virtual_desktop_supported, is_windows_11},
Expand Down Expand Up @@ -148,8 +148,9 @@ impl WindowsApi {
callback,
LPARAM(callback_data_address),
)
.ok()
.filter_fake_error()?;
}
.ok()?;
Ok(())
}

Expand Down Expand Up @@ -253,24 +254,18 @@ impl WindowsApi {
pub fn show_window(hwnd: HWND, command: SHOW_WINDOW_CMD) -> Result<()> {
// BOOL is returned but does not signify whether or not the operation was succesful
// https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-showwindow
let result = unsafe { ShowWindow(hwnd, command) }.ok();
if let Err(error) = result {
if !error.code().is_ok() {
return Err(error.into());
}
}
unsafe { ShowWindow(hwnd, command) }
.ok()
.filter_fake_error()?;
Ok(())
}

pub fn show_window_async(hwnd: HWND, command: SHOW_WINDOW_CMD) -> Result<()> {
// BOOL is returned but does not signify whether or not the operation was succesful
// https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-showwindowasync
let result = unsafe { ShowWindowAsync(hwnd, command) }.ok();
if let Err(error) = result {
if !error.code().is_ok() {
return Err(error.into());
}
}
unsafe { ShowWindowAsync(hwnd, command) }
.ok()
.filter_fake_error()?;
Ok(())
}

Expand All @@ -292,7 +287,7 @@ impl WindowsApi {
rect: RECT,
flags: SET_WINDOW_POS_FLAGS,
) -> Result<()> {
let result = unsafe {
unsafe {
SetWindowPos(
hwnd,
order,
Expand All @@ -302,11 +297,7 @@ impl WindowsApi {
(rect.bottom - rect.top).abs(),
flags,
)
};
if let Err(error) = result {
if !error.code().is_ok() {
return Err(error.into());
}
.filter_fake_error()?;
}
Ok(())
}
Expand Down

0 comments on commit fe896b9

Please sign in to comment.