7 stable releases

2.0.2 Feb 21, 2025
2.0.1 Jan 23, 2025
2.0.0 Jan 26, 2024
1.0.4 Jan 26, 2024
1.0.1 Feb 5, 2020

#69 in Windows APIs

Download history 3598/week @ 2024-12-09 2793/week @ 2024-12-16 1661/week @ 2024-12-23 1845/week @ 2024-12-30 3057/week @ 2025-01-06 3717/week @ 2025-01-13 4442/week @ 2025-01-20 4934/week @ 2025-01-27 5248/week @ 2025-02-03 5919/week @ 2025-02-10 5185/week @ 2025-02-17 3745/week @ 2025-02-24 2854/week @ 2025-03-03 3193/week @ 2025-03-10 2816/week @ 2025-03-17 3638/week @ 2025-03-24

12,811 downloads per month
Used in 19 crates (5 directly)

MIT/Apache

25KB
380 lines

win32job

docs.rs crates.io

Documentation

A safe API for Windows' job objects, which can be used to set various limits to processes associated with them.

[dependencies]
win32job = "2"

Examples

Limit the amount of memory that will be available for this process (allocating more memory is still possible, but it will be paged):

use win32job::{Job, ExtendedLimitInfo};

fn main() -> Result<(), Box<dyn std::error::Error>>  {
    let mut info = ExtendedLimitInfo::new();

    info.limit_working_memory(1 * 1024 * 1024, 4 * 1024 * 1024);

    let job = Job::create_with_limit_info(&mut info)?;

    job.assign_current_process()?;
    
    Ok(())
}

Force any created sub processes to exit when the main process exits:

use win32job::Job;
use std::process::Command;

fn main() -> Result<(), Box<dyn std::error::Error>>  {
    let job = Job::create()?;
    
    let mut info = job.query_extended_limit_info()?;

    info.limit_kill_on_job_close();

    job.set_extended_limit_info(&mut info)?;
    
    job.assign_current_process()?;

    Command::new("cmd.exe")
            .arg("/C")
            .arg("ping -n 9999 127.0.0.1")
            .spawn()?;

    // The cmd will be killed once we exit, or `job` is dropped.
    
    Ok(())
}

License

The win32job crate is licensed under either of

Apache License, Version 2.0, (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)

at your option.

Dependencies

~118MB
~2M SLoC