Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. #[cfg(windows)]
  2. pub fn descendents_of(parent_pid: pid_t) -> Result<Vec<pid_t>, Error> {
  3. use std::os::windows::io::RawHandle;
  4. use winapi::um::processthreadsapi::{GetProcessId, OpenProcess};
  5. use winapi::um::winnt::{ACCESS_MASK, MAXIMUM_ALLOWED, HANDLE, PROCESS_QUERY_INFORMATION};
  6. use winapi::um::handleapi::CloseHandle;
  7. use winapi::shared::minwindef::{FALSE, ULONG};
  8. use winapi::shared::ntdef::NTSTATUS;
  9.  
  10. #[link(name="ntdll")]
  11. extern "system" {
  12. // (Vista and above) enumerate process children.
  13. fn NtGetNextProcess(process: HANDLE, access: ACCESS_MASK, attritubes: ULONG, flags: ULONG, new_process: *mut HANDLE) -> NTSTATUS;
  14. }
  15.  
  16. let mut handle = unsafe {
  17. OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, parent_pid)
  18. };
  19.  
  20. let mut handles = vec![handle];
  21.  
  22. if handle == (0 as RawHandle) {
  23. return Err(format_err!(
  24. "Unable to fetch process handle for process {}", parent_pid
  25. ));
  26. }
  27.  
  28. unsafe {
  29. while NtGetNextProcess(handle, MAXIMUM_ALLOWED, 0, 0,
  30. &mut handle) == 0 {
  31. handles.push(handle);
  32. }
  33. }
  34.  
  35. let ret = handles.iter().map(|x| {
  36. let pid = GetProcessId(x);
  37.  
  38. unsafe { CloseHandle(x) };
  39.  
  40. pid
  41. }).collect();
  42.  
  43. println!("TODO: debugging, remove!!! {:?}", ret);
  44.  
  45. Ok(ret)
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement