Giter VIP home page Giter VIP logo

createprocessw's Introduction

CreateProcessW

actions status crate version documentation dependencies status licenses

This crate provides an API similar to std::process to create and handle processes on Windows using the Win32 API through the windows-rs crate (see this example).

Its main difference with std::process::Command is that it allows running a command string instead of having to pass the command executable and the arguments separately.

This is equivalent of running:

std::process::Command::new("cmd.exe")
    .arg("/c")
    .arg("any_command_string")
    .spawn().expect("cannot spawn command");

The only difference will be that the Child instance will use the PID of the command instead of the PID of cmd.exe. This is important because calling .kill() in the code above does not work as it kills the PID of cmd.exe instead of the actual command that has been ran.

Usage

Add the following to your Cargo.toml:

[dependencies]
CreateProcessW = "0.1.0"

This crate doesn't follow Rust's naming recommendations. If you want to stay consistent with other imported crates, use the following:

[dependencies]
create_process_w = { version = "0.1.0", package = "CreateProcessW" }

Create a command

The Command struct is used to configure and spawn processes:

use CreateProcessW::Command;

let command = Command::new("cargo.exe clippy -- -D warnings")
    .inherit_handles(true)
    .current_dir(r"C:\Users\<user>\repos\<repo_name>");

Spawning a process

The spawn function spawns the process and returns a Child that represents the spawned child process.

use CreateProcessW::Command;

let child = Command::new("notepad.exe")
    .spawn()
    .expect("notepad failed to start");


std::thread::sleep(std::time::Duration::from_secs(2));

child.kill().expect("cannot kill process");
let status = child.wait().expect("cannot wait process");

if status.success() {
    println!("Success!");
} else {
    println!("Process exited with status {}", status.code());
}

The status function spawns a child process, waits for it to finish and returns its ExitStatus.

use CreateProcessW::Command;

let status = Command::new("notepad.exe")
    .status()
    .expect("notepad failed to start");

if status.success() {
    println!("Success!")
} else {
    println!("Process exited with status {}", status.code())
}

createprocessw's People

Contributors

yozhgoor avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.