Skip to content

Commit

Permalink
fix: Do not create a command prompt window when running wsl.exe
Browse files Browse the repository at this point in the history
This prevents a CMD window from popping up when running `wsl.exe` (e.g.,
in a GUI application).
  • Loading branch information
Holzhaus committed Jan 11, 2024
1 parent e7e349d commit 15c4330
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

use std::process::Command;

#[cfg(windows)]
use std::os::windows::process::CommandExt;

/// Type of conversion to perform
#[derive(Debug)]
pub enum Conversion {
Expand Down Expand Up @@ -71,17 +74,28 @@ pub fn convert(
args.push("-a");
}

let cmd = Command::new("wsl.exe")
.args(args)
.arg(path.replace('\\', "\\\\"))
.output()?;
let mut cmd = Command::new("wsl.exe");
cmd.args(args);
cmd.arg(path.replace('\\', "\\\\"));

// Disable window creation on Windows
//
// This is necessary to prevent a command prompt window from being shown for a short time,
// which is likely undesired, especially for GUI applications.
//
// The flags are documented here:
// https://learn.microsoft.com/en-us/windows/win32/procthread/process-creation-flags#flags
#[cfg(windows)]
cmd.creation_flags(0x08000000); // CREATE_NO_WINDOW

let output = cmd.output()?;

let code = cmd.status.code().unwrap_or(-1);
let code = output.status.code().unwrap_or(-1);
if code != 0 {
return Err(format!("Error getting wslpath: {}", code).into());
}

Ok(std::str::from_utf8(&cmd.stdout)?.trim().to_string())
Ok(std::str::from_utf8(&output.stdout)?.trim().to_string())
}

#[cfg(test)]
Expand Down

0 comments on commit 15c4330

Please sign in to comment.