Skip to content

Commit 4552576

Browse files
committed
Auto merge of rust-lang#126690 - andyolivares:feature/show_window, r=dtolnay
Exposing STARTUPINFOW.wShowWindow in CommandExt trait Hi: I needed a way to control how a new process's window is displayed in Windows (normal, minimized, maximized, etc). I noticed that there is no direct way to do that (I even searched for crates doing this, but didn't find any). Inspecting the standard library source code, I figured that it would be a good addition to CommandExt trait that allows some Windows specific customization to a Command. This is my first time contributing to Rust, so please bear with me if I'm not following the rules :)
2 parents 6f6e343 + 0d64105 commit 4552576

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

std/src/os/windows/process.rs

+13
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,14 @@ pub trait CommandExt: Sealed {
181181
#[stable(feature = "windows_process_extensions", since = "1.16.0")]
182182
fn creation_flags(&mut self, flags: u32) -> &mut process::Command;
183183

184+
/// Sets the field `wShowWindow` of [STARTUPINFO][1] that is passed to `CreateProcess`.
185+
/// Allowed values are the ones listed in
186+
/// <https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-showwindow>
187+
///
188+
/// [1]: <https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/ns-processthreadsapi-startupinfow>
189+
#[unstable(feature = "windows_process_extensions_show_window", issue = "127544")]
190+
fn show_window(&mut self, cmd_show: u16) -> &mut process::Command;
191+
184192
/// Forces all arguments to be wrapped in quote (`"`) characters.
185193
///
186194
/// This is useful for passing arguments to [MSYS2/Cygwin][1] based
@@ -370,6 +378,11 @@ impl CommandExt for process::Command {
370378
self
371379
}
372380

381+
fn show_window(&mut self, cmd_show: u16) -> &mut process::Command {
382+
self.as_inner_mut().show_window(Some(cmd_show));
383+
self
384+
}
385+
373386
fn force_quotes(&mut self, enabled: bool) -> &mut process::Command {
374387
self.as_inner_mut().force_quotes(enabled);
375388
self

std/src/sys/pal/windows/process.rs

+10
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ pub struct Command {
163163
env: CommandEnv,
164164
cwd: Option<OsString>,
165165
flags: u32,
166+
show_window: Option<u16>,
166167
detach: bool, // not currently exposed in std::process
167168
stdin: Option<Stdio>,
168169
stdout: Option<Stdio>,
@@ -194,6 +195,7 @@ impl Command {
194195
env: Default::default(),
195196
cwd: None,
196197
flags: 0,
198+
show_window: None,
197199
detach: false,
198200
stdin: None,
199201
stdout: None,
@@ -224,6 +226,9 @@ impl Command {
224226
pub fn creation_flags(&mut self, flags: u32) {
225227
self.flags = flags;
226228
}
229+
pub fn show_window(&mut self, cmd_show: Option<u16>) {
230+
self.show_window = cmd_show;
231+
}
227232

228233
pub fn force_quotes(&mut self, enabled: bool) {
229234
self.force_quotes_enabled = enabled;
@@ -337,6 +342,11 @@ impl Command {
337342
si.hStdError = stderr.as_raw_handle();
338343
}
339344

345+
if let Some(cmd_show) = self.show_window {
346+
si.dwFlags |= c::STARTF_USESHOWWINDOW;
347+
si.wShowWindow = cmd_show;
348+
}
349+
340350
let si_ptr: *mut c::STARTUPINFOW;
341351

342352
let mut proc_thread_attribute_list;

0 commit comments

Comments
 (0)