fix: win, tray, detect cmdline (#11638)

target x86, run on x64

Signed-off-by: fufesou <linlong1266@gmail.com>
This commit is contained in:
fufesou
2025-05-05 08:51:25 +08:00
committed by GitHub
parent ca00706a38
commit d56df22838
3 changed files with 60 additions and 1 deletions

View File

@@ -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")]

View File

@@ -3170,6 +3170,38 @@ pub fn is_msi_installed() -> std::io::Result<bool> {
Ok(1 == uninstall_key.get_value::<u32, _>("WindowsInstaller")?)
}
#[cfg(not(target_pointer_width = "64"))]
pub fn get_pids_with_first_arg_check_session<S1: AsRef<str>, S2: AsRef<str>>(
name: S1,
arg: S2,
same_session_id: bool,
) -> ResultType<Vec<hbb_common::sysinfo::Pid>> {
// 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<S2: AsRef<str>>(
output: std::borrow::Cow<'_, str>,

View File

@@ -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| {