command_group/stdlib/
windows.rs

1use std::{
2	os::windows::{io::AsRawHandle, process::CommandExt},
3	process::Command,
4};
5use winapi::um::winbase::CREATE_SUSPENDED;
6
7use crate::{builder::CommandGroupBuilder, winres::*, GroupChild};
8
9impl CommandGroupBuilder<'_, Command> {
10	/// Executes the command as a child process group, returning a handle to it.
11	///
12	/// By default, stdin, stdout and stderr are inherited from the parent.
13	///
14	/// On Windows, this creates a job object instead of a POSIX process group.
15	///
16	/// # Examples
17	///
18	/// Basic usage:
19	///
20	/// ```no_run
21	/// use std::process::Command;
22	/// use command_group::CommandGroup;
23	///
24	/// Command::new("ls")
25	///         .group()
26	/// 		.spawn()
27	///         .expect("ls command failed to start");
28	/// ```
29	pub fn spawn(&mut self) -> std::io::Result<GroupChild> {
30		self.command
31			.creation_flags(self.creation_flags | CREATE_SUSPENDED);
32
33		let (job, completion_port) = job_object(self.kill_on_drop)?;
34		let child = self.command.spawn()?;
35		assign_child(child.as_raw_handle(), job)?;
36
37		Ok(GroupChild::new(child, job, completion_port))
38	}
39}