Skip to content

PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust

M10: Windows printing starts PowerShell by its full path (#1106)

jcl-bit0e8c978c6a7e
diff --git a/crates/print/src/spool.rs b/crates/print/src/spool.rs
index b92476136..92a54fa81 100644
--- a/crates/print/src/spool.rs
+++ b/crates/print/src/spool.rs
@@ -351,11 +351,17 @@ pub fn printers_parsed(queues: &str, available: Option<&str>) -> Vec<Printer> {
/// writing banners into what is parsed, `-NonInteractive` from waiting for input.
/// `CREATE_NO_WINDOW`: the release app has no console of its own, so without it Windows opens
/// a console window for PowerShell over the app for as long as the script runs.
+///
+/// Named by its full path, as `rundll32` is in [`open_printer_preferences`]: given a bare name,
+/// Rust looks in the app's own folder before System32, so a `powershell.exe` beside a portable
+/// `pdfcraft.exe` would run in its place each time the Print dialog opens and on every job.
#[cfg(windows)]
-fn powershell_command(script: &str) -> std::process::Command {
+pub(crate) fn powershell_command(script: &str) -> std::process::Command {
use std::os::windows::process::CommandExt as _;
const CREATE_NO_WINDOW: u32 = 0x0800_0000;
- let mut c = std::process::Command::new("powershell");
+ let root = std::env::var_os("SystemRoot").unwrap_or_else(|| "C:\\Windows".into());
+ let exe = std::path::Path::new(&root).join("System32").join("WindowsPowerShell").join("v1.0").join("powershell.exe");
+ let mut c = std::process::Command::new(exe);
c.args(["-NoProfile", "-NonInteractive", "-Command", script]);
c.creation_flags(CREATE_NO_WINDOW);
c
diff --git a/crates/print/src/tests.rs b/crates/print/src/tests.rs
index 0654219a1..261b31785 100644
--- a/crates/print/src/tests.rs
+++ b/crates/print/src/tests.rs
@@ -475,6 +475,19 @@ fn windows_lists_the_printers_the_system_knows() {
assert!(printers.iter().filter(|p| p.default).count() <= 1, "{printers:?}");
}
+/// Windows: PowerShell is started by its full path under System32, never by its bare name.
+/// Rust resolves a bare name in the app's own folder first, so a `powershell.exe` beside a
+/// portable install would run instead, for every printer list and every job.
+#[cfg(windows)]
+#[test]
+fn windows_starts_powershell_by_its_full_path() {
+ let c = crate::spool::powershell_command("$null");
+ let program = std::path::Path::new(c.get_program());
+ assert!(program.is_absolute(), "{program:?}");
+ assert!(program.ends_with("System32/WindowsPowerShell/v1.0/powershell.exe"), "{program:?}");
+ assert!(program.is_file(), "{program:?}");
+}
+
/// Windows: the embedded print script is valid PowerShell, checked on every machine — including
/// runners that have no printer to print to.
#[cfg(windows)]