diff --git a/src/windows.rs b/src/windows.rs index a42742c..64c042c 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -1,8 +1,9 @@ //! Windows-specific extensions. +use std::os::windows::io::{AsRawHandle, RawHandle}; use std::os::windows::process::CommandExt as _; -use crate::Command; +use crate::{Child, Command}; /// Windows-specific extensions to the [`Command`] builder. pub trait CommandExt { @@ -20,3 +21,9 @@ impl CommandExt for Command { self } } + +impl AsRawHandle for Child { + fn as_raw_handle(&self) -> RawHandle { + self.child.lock().unwrap().get_mut().as_raw_handle() + } +} diff --git a/tests/std.rs b/tests/std.rs index 63eccde..489cd48 100644 --- a/tests/std.rs +++ b/tests/std.rs @@ -381,3 +381,29 @@ fn child_status_preserved_with_kill_on_drop() { assert!(res.unwrap().status.success()); }) } + +#[test] +#[cfg(windows)] +fn child_as_raw_handle() { + use std::os::windows::io::AsRawHandle; + use winapi::um::processthreadsapi::GetProcessId; + + future::block_on(async { + let p = Command::new("cmd.exe") + .arg("/C") + .arg("pause") + .kill_on_drop(true) + .spawn() + .unwrap(); + + let std_pid = p.id(); + assert!(std_pid > 0); + + let handle = p.as_raw_handle(); + + // We verify that we have the correct handle by obtaining the PID + // with the Windows API rather than via std: + let win_pid = unsafe { GetProcessId(handle as _) }; + assert_eq!(win_pid, std_pid); + }) +}