From 056cb003d19a6deb58214a041ee76d62716524c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matou=C5=A1=20Dzivjak?= Date: Sun, 5 Feb 2023 11:23:27 +0000 Subject: [PATCH] feat(windows): use std Command Use std `Command` instead of `ShellExecuteW` from windows sys crate. This change was already attempted in: https://github.com/Byron/open-rs/issues/25 and later reverted in: https://github.com/Byron/open-rs/pull/27 and it it seems that it didn't work due to incorrect usage of `explorer` instead of `cmd /c start`. (see https://github.com/helix-editor/helix/pull/5820#issuecomment-1416796024 for detailed explanation). Related: https://github.com/helix-editor/helix/pull/5820 --- Cargo.toml | 3 --- src/windows.rs | 41 ++++++++++++----------------------------- 2 files changed, 12 insertions(+), 32 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 268168b..e37dcb2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,8 +16,5 @@ test = false doc = false name = "open" -[target.'cfg(windows)'.dependencies] -windows-sys = { version = "0.42", features = ["Win32_UI_Shell", "Win32_Foundation", "Win32_UI_WindowsAndMessaging"] } - [target.'cfg(all(unix, not(macos)))'.dependencies] pathdiff = "0.2.0" diff --git a/src/windows.rs b/src/windows.rs index 125770e..7bd5bcb 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -6,8 +6,6 @@ use std::{ }; use std::os::raw::c_int; -use windows_sys::Win32::UI::Shell::ShellExecuteW; -use windows_sys::Win32::UI::WindowsAndMessaging::SW_SHOW; use crate::IntoResult; @@ -32,35 +30,20 @@ fn convert_path(path: &OsStr) -> io::Result> { pub fn that>(path: T) -> io::Result<()> { let path = convert_path(path.as_ref())?; - let operation: Vec = OsStr::new("open\0").encode_wide().collect(); - let result = unsafe { - ShellExecuteW( - 0, - operation.as_ptr(), - path.as_ptr(), - ptr::null(), - ptr::null(), - SW_SHOW, - ) - }; - (result as c_int).into_result() + Command::new("cmd") + .arg("/c") + .arg("start") + .arg(path.as_ref()) + .status_without_output() + .into_result() } pub fn with>(path: T, app: impl Into) -> io::Result<()> { let path = convert_path(path.as_ref())?; - let operation: Vec = OsStr::new("open\0").encode_wide().collect(); - let app_name: Vec = OsStr::new(&format!("{}\0", app.into())) - .encode_wide() - .collect(); - let result = unsafe { - ShellExecuteW( - 0, - operation.as_ptr(), - app_name.as_ptr(), - path.as_ptr(), - ptr::null(), - SW_SHOW, - ) - }; - (result as c_int).into_result() + Command::new("cmd") + .arg("/c") + .arg(app.into()) + .arg(path.as_ref()) + .status_without_output() + .into_result() }