From d56df22838839b37aca4768fa330f783bda7d593 Mon Sep 17 00:00:00 2001 From: fufesou <13586388+fufesou@users.noreply.github.com> Date: Mon, 5 May 2025 08:51:25 +0800 Subject: [PATCH] fix: win, tray, detect cmdline (#11638) target x86, run on x64 Signed-off-by: fufesou --- src/common.rs | 27 +++++++++++++++++++++++++++ src/platform/windows.rs | 32 ++++++++++++++++++++++++++++++++ src/tray.rs | 2 +- 3 files changed, 60 insertions(+), 1 deletion(-) diff --git a/src/common.rs b/src/common.rs index 6c177a437..d74ecac8b 100644 --- a/src/common.rs +++ b/src/common.rs @@ -1232,7 +1232,34 @@ pub async fn get_next_nonkeyexchange_msg( None } +#[cfg(all(target_os = "windows", not(target_pointer_width = "64")))] +pub fn check_process(arg: &str, same_session_id: bool) -> bool { + let mut path = std::env::current_exe().unwrap_or_default(); + if let Ok(linked) = path.read_link() { + path = linked; + } + let Some(filename) = path.file_name() else { + return false; + }; + let filename = filename.to_string_lossy().to_string(); + match crate::platform::windows::get_pids_with_first_arg_check_session( + &filename, + arg, + same_session_id, + ) { + Ok(pids) => { + let self_pid = hbb_common::sysinfo::Pid::from_u32(std::process::id()); + pids.into_iter().filter(|pid| *pid != self_pid).count() > 0 + } + Err(e) => { + log::error!("Failed to check process with arg: \"{}\", {}", arg, e); + false + } + } +} + #[allow(unused_mut)] +#[cfg(not(all(target_os = "windows", not(target_pointer_width = "64"))))] #[cfg(not(any(target_os = "android", target_os = "ios")))] pub fn check_process(arg: &str, mut same_uid: bool) -> bool { #[cfg(target_os = "macos")] diff --git a/src/platform/windows.rs b/src/platform/windows.rs index 41c3dea91..2a632920e 100644 --- a/src/platform/windows.rs +++ b/src/platform/windows.rs @@ -3170,6 +3170,38 @@ pub fn is_msi_installed() -> std::io::Result { Ok(1 == uninstall_key.get_value::("WindowsInstaller")?) } +#[cfg(not(target_pointer_width = "64"))] +pub fn get_pids_with_first_arg_check_session, S2: AsRef>( + name: S1, + arg: S2, + same_session_id: bool, +) -> ResultType> { + // Though `wmic` can return the sessionId, for simplicity we only return processid. + let pids = get_pids_with_first_arg_by_wmic(name, arg); + if !same_session_id { + return Ok(pids); + } + let Some(cur_sid) = get_current_process_session_id() else { + bail!("Can't get current process session id"); + }; + let mut same_session_pids = vec![]; + for pid in pids.into_iter() { + let mut sid = 0; + if unsafe { ProcessIdToSessionId(pid.as_u32(), &mut sid) == TRUE } { + if sid == cur_sid { + same_session_pids.push(pid); + } + } else { + // Only log here, because this call almost never fails. + log::warn!( + "Failed to get session id of the process id, error: {:?}", + std::io::Error::last_os_error() + ); + } + } + Ok(same_session_pids) +} + #[cfg(not(target_pointer_width = "64"))] fn get_pids_with_args_from_wmic_output>( output: std::borrow::Cow<'_, str>, diff --git a/src/tray.rs b/src/tray.rs index 3a3ae92f3..98ce450e1 100644 --- a/src/tray.rs +++ b/src/tray.rs @@ -56,7 +56,7 @@ fn make_tray() -> hbb_common::ResultType<()> { let mut event_loop = EventLoopBuilder::new().build(); let tray_menu = Menu::new(); - let quit_i = MenuItem::new(translate("Exit".to_owned()), true, None); + let quit_i = MenuItem::new(translate("Stop service".to_owned()), true, None); let open_i = MenuItem::new(translate("Open".to_owned()), true, None); tray_menu.append_items(&[&open_i, &quit_i]).ok(); let tooltip = |count: usize| {