Struct CreateProcessW::Command
source · pub struct Command { /* private fields */ }
Expand description
A process builder, providing control over how a new process should be spawned.
Implementations§
source§impl Command
impl Command
sourcepub fn new(command: impl Into<OsString>) -> Self
pub fn new(command: impl Into<OsString>) -> Self
Create a new Command
, with the following default configuration:
- Inherit handles of the calling process.
- Inherit the current drive and directory of the calling process.
Builder methods are provided to change these defaults and otherwise configure the process.
§Examples
use CreateProcessW::Command;
Command::new("notepad.exe")
.spawn()
.expect("notepad failed to start");
Equivalent to the lpCommandLine
parameter of the
CreateProcessW
function.
sourcepub fn inherit_handles(&mut self, inherit: bool) -> &mut Self
pub fn inherit_handles(&mut self, inherit: bool) -> &mut Self
Enable/disable handles inherance.
If this parameter is true
, each inheritable handle in the calling
process is inherited by the new process. If the parameter is false
,
the handles are not inherited. Note that inherited handles have the
same value and access rights as the original handles.
Equivalent to the bInheritHandles
parameter of the
CreateProcessW
function.
sourcepub fn current_dir(&mut self, dir: impl Into<PathBuf>) -> &mut Self
pub fn current_dir(&mut self, dir: impl Into<PathBuf>) -> &mut Self
Sets the working directory for the child process.
It’s the full path to the current directory for the process. Note that you can use a raw string to avoid error when copy-pasting the path.
§Examples
use CreateProcessW::Command;
let check = Command::new("cargo.exe check")
.current_dir(r"C:\Users\<user>\repos\<repo_name>")
.status()
.expect("cargo check command failed");
Equivalent to the lpCurrentDirectory
parameter of the
CreateProcessW
function.
sourcepub fn spawn(&mut self) -> Result<Child, Error>
pub fn spawn(&mut self) -> Result<Child, Error>
Executes the command as a child process, returning a handle to it.
§Examples
use CreateProcessW::Command;
Command::new("notepad.exe")
.spawn()
.expect("notepad failed to start");
sourcepub fn status(&mut self) -> Result<ExitStatus, Error>
pub fn status(&mut self) -> Result<ExitStatus, Error>
Executes a command as a child process, waiting for it to finish and collecting its status.
§Examples
use CreateProcessW::Command;
let status = Command::new("notepad.exe")
.status()
.expect("failed to execute process");
println!("process finished with: {}", status.code());
assert!(status.success());