about summary refs log tree commit diff
path: root/tests/ui/process/win-creation-flags.rs
blob: 0c7c6883d68e302ec4cda9327c8b6c98dddc62b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Test that windows `creation_flags` extension to `Command` works.

//@ run-pass
//@ only-windows
//@ needs-subprocess

use std::env;
use std::os::windows::process::CommandExt;
use std::process::{Command, exit};

fn main() {
    if env::args().skip(1).any(|s| s == "--child") {
        child();
    } else {
        parent();
    }
}

fn parent() {
    let exe = env::current_exe().unwrap();

    // Use the DETACH_PROCESS to create a subprocess that isn't attached to the console.
    // The subprocess's exit status will be 0 if it's detached.
    let status = Command::new(&exe)
        .arg("--child")
        .creation_flags(DETACH_PROCESS)
        .spawn()
        .unwrap()
        .wait()
        .unwrap();
    assert_eq!(status.code(), Some(0));

    // Try without DETACH_PROCESS to ensure this test works.
    let status = Command::new(&exe).arg("--child").spawn().unwrap().wait().unwrap();
    assert_eq!(status.code(), Some(1));
}

// exits with 1 if the console is attached or 0 otherwise
fn child() {
    // Get the attached console's code page.
    // This will fail (return 0) if no console is attached.
    let has_console = GetConsoleCP() != 0;
    exit(has_console as i32);
}

// Windows API definitions.
const DETACH_PROCESS: u32 = 0x00000008;
#[link(name = "kernel32")]
unsafe extern "system" {
    safe fn GetConsoleCP() -> u32;
}