Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit fe62f6f

Browse files
author
Andres Olivares
committedJul 10, 2024
Exposing STARTUPINFOW.wShowWindow in CommandExt (show_window function) to control how a new process should display its window (normal, minimized, maximized, etc)
1 parent c5f1c76 commit fe62f6f

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed
 

‎std/src/os/windows/process.rs

+12
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,13 @@ 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][1] of [STARTUPINFO][2] that is passed to `CreateProcess`.
185+
///
186+
/// [1]: https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-showwindow
187+
/// [2]: https://learn.microsoft.com/es-es/windows/win32/api/processthreadsapi/ns-processthreadsapi-startupinfow
188+
#[unstable(feature = "windows_process_extensions_show_window", issue = "none")]
189+
fn show_window(&mut self, cmd_show: u16) -> &mut process::Command;
190+
184191
/// Forces all arguments to be wrapped in quote (`"`) characters.
185192
///
186193
/// This is useful for passing arguments to [MSYS2/Cygwin][1] based
@@ -370,6 +377,11 @@ impl CommandExt for process::Command {
370377
self
371378
}
372379

380+
fn show_window(&mut self, cmd_show: u16) -> &mut process::Command {
381+
self.as_inner_mut().show_window(Some(cmd_show));
382+
self
383+
}
384+
373385
fn force_quotes(&mut self, enabled: bool) -> &mut process::Command {
374386
self.as_inner_mut().force_quotes(enabled);
375387
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)
Failed to load comments.