Debian Codegraph @

src/windows/handle.rs

raw symbols SCIP
1 use std::ops::Deref;
2
3 use windows_sys::Win32::{
4 Foundation::{CloseHandle, FALSE, HANDLE},
5 System::Threading::{OpenProcess, PROCESS_QUERY_LIMITED_INFORMATION},
6 };
7
8 use crate::Pid;
9
10 pub struct Handle(HANDLE);
11
12 impl Handle {
13 pub fn open(pid: Pid) -> Option<Self> {
14 let handle = unsafe { OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, pid.0) };
15 if handle.is_null() {
16 None
17 } else {
18 Some(Self(handle))
19 }
20 }
21 }
22
23 impl Deref for Handle {
24 type Target = HANDLE;
25
26 fn deref(&self) -> &Self::Target {
27 &self.0
28 }
29 }
30
31 impl Drop for Handle {
32 fn drop(&mut self) {
33 unsafe {
34 CloseHandle(self.0);
35 }
36 }
37 }