Remove get_error() in windows.rs

Signed-off-by: fufesou <shuanglongchen@yeah.net>
This commit is contained in:
fufesou 2023-11-20 17:38:53 +08:00
parent c6d587f0c7
commit 2aaca0c54a
5 changed files with 39 additions and 80 deletions

View File

@ -139,7 +139,7 @@ impl MagInterface {
return Err(Error::new(
ErrorKind::Other,
format!(
"Failed to LoadLibraryExA {}, error: {:?}",
"Failed to LoadLibraryExA {}, error: {}",
lib_file_name,
Error::last_os_error()
),
@ -173,10 +173,7 @@ impl MagInterface {
if FALSE == init_func() {
return Err(Error::new(
ErrorKind::Other,
format!(
"Failed to MagInitialize, error: {:?}",
Error::last_os_error()
),
format!("Failed to MagInitialize, error: {}", Error::last_os_error()),
));
} else {
s.init_succeeded = true;
@ -198,7 +195,7 @@ impl MagInterface {
return Err(Error::new(
ErrorKind::Other,
format!(
"Failed to GetProcAddress {}, error {:?}",
"Failed to GetProcAddress {}, error {}",
func_name,
Error::last_os_error()
),
@ -212,14 +209,14 @@ impl MagInterface {
if let Some(uninit_func) = self.mag_uninitialize_func {
unsafe {
if FALSE == uninit_func() {
println!("Failed MagUninitialize, error {:?}", Error::last_os_error())
println!("Failed MagUninitialize, error {}", Error::last_os_error())
}
}
}
if !self.lib_handle.is_null() {
unsafe {
if FALSE == FreeLibrary(self.lib_handle) {
println!("Failed FreeLibrary, error {:?}", Error::last_os_error())
println!("Failed FreeLibrary, error {}", Error::last_os_error())
}
}
self.lib_handle = NULL as _;
@ -319,7 +316,7 @@ impl CapturerMag {
return Err(Error::new(
ErrorKind::Other,
format!(
"Failed to GetModuleHandleExA, error {:?}",
"Failed to GetModuleHandleExA, error {}",
Error::last_os_error()
),
));
@ -375,7 +372,7 @@ impl CapturerMag {
return Err(Error::new(
ErrorKind::Other,
format!(
"Failed to CreateWindowExA host_window, error {:?}",
"Failed to CreateWindowExA host_window, error {}",
Error::last_os_error()
),
));
@ -400,7 +397,7 @@ impl CapturerMag {
return Err(Error::new(
ErrorKind::Other,
format!(
"Failed CreateWindowA magnifier_window, error {:?}",
"Failed CreateWindowA magnifier_window, error {}",
Error::last_os_error()
),
));
@ -420,7 +417,7 @@ impl CapturerMag {
return Err(Error::new(
ErrorKind::Other,
format!(
"Failed to MagSetImageScalingCallback, error {:?}",
"Failed to MagSetImageScalingCallback, error {}",
Error::last_os_error()
),
));
@ -464,7 +461,7 @@ impl CapturerMag {
return Err(Error::new(
ErrorKind::Other,
format!(
"Failed MagSetWindowFilterList for cls {} name {}, error {:?}",
"Failed MagSetWindowFilterList for cls {} name {}, error {}",
cls,
name,
Error::last_os_error()
@ -539,7 +536,7 @@ impl CapturerMag {
return Err(Error::new(
ErrorKind::Other,
format!(
"Failed SetWindowPos (x, y, w , h) - ({}, {}, {}, {}), error {:?}",
"Failed SetWindowPos (x, y, w , h) - ({}, {}, {}, {}), error {}",
self.rect.left,
self.rect.top,
self.rect.right - self.rect.left,
@ -556,7 +553,7 @@ impl CapturerMag {
return Err(Error::new(
ErrorKind::Other,
format!(
"Failed to MagSetWindowSource, error {:?}",
"Failed to MagSetWindowSource, error {}",
Error::last_os_error()
),
));
@ -591,7 +588,7 @@ impl CapturerMag {
if FALSE == DestroyWindow(self.magnifier_window) {
//
println!(
"Failed DestroyWindow magnifier window, error {:?}",
"Failed DestroyWindow magnifier window, error {}",
Error::last_os_error()
)
}
@ -604,7 +601,7 @@ impl CapturerMag {
if FALSE == DestroyWindow(self.host_window) {
//
println!(
"Failed DestroyWindow host window, error {:?}",
"Failed DestroyWindow host window, error {}",
Error::last_os_error()
)
}

View File

@ -319,7 +319,7 @@ fn get_rich_cursor_data(
let dc = DC::new()?;
let bitmap_dc = BitmapDC::new(dc.0, hbm_color)?;
if get_di_bits(out.as_mut_ptr(), bitmap_dc.dc(), hbm_color, width, height) > 0 {
bail!("Failed to get di bits: {}", get_error());
bail!("Failed to get di bits: {}", io::Error::last_os_error());
}
}
Ok(())
@ -603,7 +603,7 @@ async fn launch_server(session_id: DWORD, close_first: bool) -> ResultType<HANDL
let wstr = wstr.as_ptr();
let h = unsafe { LaunchProcessWin(wstr, session_id, FALSE) };
if h.is_null() {
log::error!("Failed to launch server: {}", get_error());
log::error!("Failed to launch server: {}", io::Error::last_os_error());
}
Ok(h)
}
@ -627,7 +627,7 @@ pub fn run_as_user(arg: Vec<&str>) -> ResultType<Option<std::process::Child>> {
"Failed to launch {:?} with session id {}: {}",
arg,
session_id,
get_error()
io::Error::last_os_error()
);
}
Ok(None)
@ -676,7 +676,7 @@ pub fn try_change_desktop() -> bool {
if !res {
let mut s = SUPPRESS.lock().unwrap();
if s.elapsed() > std::time::Duration::from_secs(3) {
log::error!("Failed to switch desktop: {}", get_error());
log::error!("Failed to switch desktop: {}", io::Error::last_os_error());
*s = Instant::now();
}
} else {
@ -688,41 +688,6 @@ pub fn try_change_desktop() -> bool {
return false;
}
fn get_error() -> String {
unsafe {
let buff_size = 256;
let mut buff: Vec<u16> = Vec::with_capacity(buff_size);
buff.resize(buff_size, 0);
let errno = GetLastError();
let chars_copied = FormatMessageW(
FORMAT_MESSAGE_IGNORE_INSERTS
| FORMAT_MESSAGE_FROM_SYSTEM
| FORMAT_MESSAGE_ARGUMENT_ARRAY,
std::ptr::null(),
errno,
0,
buff.as_mut_ptr(),
(buff_size + 1) as u32,
std::ptr::null_mut(),
);
if chars_copied == 0 {
return "".to_owned();
}
let mut curr_char: usize = chars_copied as usize;
while curr_char > 0 {
let ch = buff[curr_char];
if ch >= ' ' as u16 {
break;
}
curr_char -= 1;
}
let sl = std::slice::from_raw_parts(buff.as_ptr(), curr_char);
let err_msg = String::from_utf16(sl);
return err_msg.unwrap_or("".to_owned());
}
}
fn share_rdp() -> BOOL {
if get_reg("share_rdp") != "true" {
FALSE
@ -1285,7 +1250,7 @@ pub fn block_input(v: bool) -> (bool, String) {
if BlockInput(v) == TRUE {
(true, "".to_owned())
} else {
(false, format!("Error: {:?}", io::Error::last_os_error()))
(false, format!("Error: {}", io::Error::last_os_error()))
}
}
}
@ -1560,7 +1525,7 @@ pub fn elevate_or_run_as_system(is_setup: bool, is_elevate: bool, is_run_as_syst
std::process::exit(0);
} else {
log::error!(
"Failed to run as system, error {:?}",
"Failed to run as system, error {}",
io::Error::last_os_error()
);
}
@ -1570,16 +1535,13 @@ pub fn elevate_or_run_as_system(is_setup: bool, is_elevate: bool, is_run_as_syst
if let Ok(true) = elevate(arg_elevate) {
std::process::exit(0);
} else {
log::error!(
"Failed to elevate, error {:?}",
io::Error::last_os_error()
);
log::error!("Failed to elevate, error {}", io::Error::last_os_error());
}
}
}
}
Err(_) => log::error!(
"Failed to get elevation status, error {:?}",
"Failed to get elevation status, error {}",
io::Error::last_os_error()
),
}
@ -1595,7 +1557,7 @@ pub fn is_elevated(process_id: Option<DWORD>) -> ResultType<bool> {
};
if handle == NULL {
bail!(
"Failed to open process, error {:?}",
"Failed to open process, error {}",
io::Error::last_os_error()
)
}
@ -1603,7 +1565,7 @@ pub fn is_elevated(process_id: Option<DWORD>) -> ResultType<bool> {
let mut token: HANDLE = mem::zeroed();
if OpenProcessToken(handle, TOKEN_QUERY, &mut token) == FALSE {
bail!(
"Failed to open process token, error {:?}",
"Failed to open process token, error {}",
io::Error::last_os_error()
)
}
@ -1619,7 +1581,7 @@ pub fn is_elevated(process_id: Option<DWORD>) -> ResultType<bool> {
) == FALSE
{
bail!(
"Failed to get token information, error {:?}",
"Failed to get token information, error {}",
io::Error::last_os_error()
)
}
@ -1801,7 +1763,7 @@ pub fn current_resolution(name: &str) -> ResultType<Resolution> {
dm.dmSize = std::mem::size_of::<DEVMODEW>() as _;
if EnumDisplaySettingsW(device_name.as_ptr(), ENUM_CURRENT_SETTINGS, &mut dm) == 0 {
bail!(
"failed to get currrent resolution, error {:?}",
"failed to get currrent resolution, error {}",
io::Error::last_os_error()
);
}
@ -1835,7 +1797,7 @@ pub(super) fn change_resolution_directly(
);
if res != DISP_CHANGE_SUCCESSFUL {
bail!(
"ChangeDisplaySettingsExW failed, res={}, error {:?}",
"ChangeDisplaySettingsExW failed, res={}, error {}",
res,
io::Error::last_os_error()
);

View File

@ -43,7 +43,7 @@ fn do_hook(tx: Sender<String>) -> ResultType<(HHOOK, HHOOK)> {
&mut hm_keyboard as _,
) {
tx.send(format!(
"Failed to GetModuleHandleExA, error: {:?}",
"Failed to GetModuleHandleExA, error: {}",
Error::last_os_error()
))?;
return Ok(invalid_ret);
@ -55,7 +55,7 @@ fn do_hook(tx: Sender<String>) -> ResultType<(HHOOK, HHOOK)> {
&mut hm_mouse as _,
) {
tx.send(format!(
"Failed to GetModuleHandleExA, error: {:?}",
"Failed to GetModuleHandleExA, error: {}",
Error::last_os_error()
))?;
return Ok(invalid_ret);
@ -69,7 +69,7 @@ fn do_hook(tx: Sender<String>) -> ResultType<(HHOOK, HHOOK)> {
);
if hook_keyboard.is_null() {
tx.send(format!(
" SetWindowsHookExA keyboard, error {:?}",
" SetWindowsHookExA keyboard, error {}",
Error::last_os_error()
))?;
return Ok(invalid_ret);
@ -80,12 +80,12 @@ fn do_hook(tx: Sender<String>) -> ResultType<(HHOOK, HHOOK)> {
if FALSE == UnhookWindowsHookEx(hook_keyboard) {
// Fatal error
log::error!(
" UnhookWindowsHookEx keyboard, error {:?}",
" UnhookWindowsHookEx keyboard, error {}",
Error::last_os_error()
);
}
tx.send(format!(
" SetWindowsHookExA mouse, error {:?}",
" SetWindowsHookExA mouse, error {}",
Error::last_os_error()
))?;
return Ok(invalid_ret);
@ -141,7 +141,7 @@ pub fn hook() -> ResultType<()> {
if FALSE == UnhookWindowsHookEx(hook_keyboard as _) {
// Fatal error
log::error!(
"Failed UnhookWindowsHookEx keyboard, error {:?}",
"Failed UnhookWindowsHookEx keyboard, error {}",
Error::last_os_error()
);
}
@ -149,7 +149,7 @@ pub fn hook() -> ResultType<()> {
if FALSE == UnhookWindowsHookEx(hook_mouse as _) {
// Fatal error
log::error!(
"Failed UnhookWindowsHookEx mouse, error {:?}",
"Failed UnhookWindowsHookEx mouse, error {}",
Error::last_os_error()
);
}
@ -178,7 +178,7 @@ pub fn unhook() -> ResultType<()> {
if *cur_hook_thread_id != 0 {
if FALSE == PostThreadMessageA(*cur_hook_thread_id, WM_USER_EXIT_HOOK, 0, 0) {
bail!(
"Failed to post message to exit hook, error {:?}",
"Failed to post message to exit hook, error {}",
Error::last_os_error()
);
}

View File

@ -266,7 +266,7 @@ impl PrivacyModeImpl {
CloseHandle(token);
if 0 == create_res {
bail!(
"Failed to create privacy window process {}, error {:?}",
"Failed to create privacy window process {}, error {}",
cmdline,
Error::last_os_error()
);
@ -284,7 +284,7 @@ impl PrivacyModeImpl {
CloseHandle(proc_info.hProcess);
bail!(
"Failed to create privacy window process, error {:?}",
"Failed to create privacy window process, error {}",
Error::last_os_error()
);
}

View File

@ -195,7 +195,7 @@ impl PrivacyModeImpl {
)
{
bail!(
"Failed EnumDisplaySettingsW, device name: {:?}, error: {:?}",
"Failed EnumDisplaySettingsW, device name: {:?}, error: {}",
std::string::String::from_utf16(&display.name),
Error::last_os_error()
);
@ -229,7 +229,7 @@ impl PrivacyModeImpl {
== EnumDisplaySettingsW(dd.DeviceName.as_ptr(), ENUM_CURRENT_SETTINGS, &mut dm)
{
bail!(
"Failed EnumDisplaySettingsW, device name: {:?}, error: {:?}",
"Failed EnumDisplaySettingsW, device name: {:?}, error: {}",
std::string::String::from_utf16(&dd.DeviceName),
Error::last_os_error()
);