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

View File

@ -319,7 +319,7 @@ fn get_rich_cursor_data(
let dc = DC::new()?; let dc = DC::new()?;
let bitmap_dc = BitmapDC::new(dc.0, hbm_color)?; 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 { 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(()) Ok(())
@ -603,7 +603,7 @@ async fn launch_server(session_id: DWORD, close_first: bool) -> ResultType<HANDL
let wstr = wstr.as_ptr(); let wstr = wstr.as_ptr();
let h = unsafe { LaunchProcessWin(wstr, session_id, FALSE) }; let h = unsafe { LaunchProcessWin(wstr, session_id, FALSE) };
if h.is_null() { 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) 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 {}: {}", "Failed to launch {:?} with session id {}: {}",
arg, arg,
session_id, session_id,
get_error() io::Error::last_os_error()
); );
} }
Ok(None) Ok(None)
@ -676,7 +676,7 @@ pub fn try_change_desktop() -> bool {
if !res { if !res {
let mut s = SUPPRESS.lock().unwrap(); let mut s = SUPPRESS.lock().unwrap();
if s.elapsed() > std::time::Duration::from_secs(3) { 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(); *s = Instant::now();
} }
} else { } else {
@ -688,41 +688,6 @@ pub fn try_change_desktop() -> bool {
return false; 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 { fn share_rdp() -> BOOL {
if get_reg("share_rdp") != "true" { if get_reg("share_rdp") != "true" {
FALSE FALSE
@ -1285,7 +1250,7 @@ pub fn block_input(v: bool) -> (bool, String) {
if BlockInput(v) == TRUE { if BlockInput(v) == TRUE {
(true, "".to_owned()) (true, "".to_owned())
} else { } 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); std::process::exit(0);
} else { } else {
log::error!( log::error!(
"Failed to run as system, error {:?}", "Failed to run as system, error {}",
io::Error::last_os_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) { if let Ok(true) = elevate(arg_elevate) {
std::process::exit(0); std::process::exit(0);
} else { } else {
log::error!( log::error!("Failed to elevate, error {}", io::Error::last_os_error());
"Failed to elevate, error {:?}",
io::Error::last_os_error()
);
} }
} }
} }
} }
Err(_) => log::error!( Err(_) => log::error!(
"Failed to get elevation status, error {:?}", "Failed to get elevation status, error {}",
io::Error::last_os_error() io::Error::last_os_error()
), ),
} }
@ -1595,7 +1557,7 @@ pub fn is_elevated(process_id: Option<DWORD>) -> ResultType<bool> {
}; };
if handle == NULL { if handle == NULL {
bail!( bail!(
"Failed to open process, error {:?}", "Failed to open process, error {}",
io::Error::last_os_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(); let mut token: HANDLE = mem::zeroed();
if OpenProcessToken(handle, TOKEN_QUERY, &mut token) == FALSE { if OpenProcessToken(handle, TOKEN_QUERY, &mut token) == FALSE {
bail!( bail!(
"Failed to open process token, error {:?}", "Failed to open process token, error {}",
io::Error::last_os_error() io::Error::last_os_error()
) )
} }
@ -1619,7 +1581,7 @@ pub fn is_elevated(process_id: Option<DWORD>) -> ResultType<bool> {
) == FALSE ) == FALSE
{ {
bail!( bail!(
"Failed to get token information, error {:?}", "Failed to get token information, error {}",
io::Error::last_os_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 _; dm.dmSize = std::mem::size_of::<DEVMODEW>() as _;
if EnumDisplaySettingsW(device_name.as_ptr(), ENUM_CURRENT_SETTINGS, &mut dm) == 0 { if EnumDisplaySettingsW(device_name.as_ptr(), ENUM_CURRENT_SETTINGS, &mut dm) == 0 {
bail!( bail!(
"failed to get currrent resolution, error {:?}", "failed to get currrent resolution, error {}",
io::Error::last_os_error() io::Error::last_os_error()
); );
} }
@ -1835,7 +1797,7 @@ pub(super) fn change_resolution_directly(
); );
if res != DISP_CHANGE_SUCCESSFUL { if res != DISP_CHANGE_SUCCESSFUL {
bail!( bail!(
"ChangeDisplaySettingsExW failed, res={}, error {:?}", "ChangeDisplaySettingsExW failed, res={}, error {}",
res, res,
io::Error::last_os_error() io::Error::last_os_error()
); );

View File

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

View File

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

View File

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