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
use core::ptr;
use std::io;
pub struct LockedData(pub winapi::shared::ntdef::HANDLE);
impl LockedData {
#[inline]
pub fn new<T>(handle: winapi::shared::ntdef::HANDLE) -> io::Result<(ptr::NonNull<T>, Self)> {
let ptr = unsafe {
winapi::um::winbase::GlobalLock(handle)
};
match ptr::NonNull::new(ptr as *mut _) {
Some(ptr) => Ok((ptr, LockedData(handle))),
None => Err(io::Error::last_os_error()),
}
}
}
impl Drop for LockedData {
fn drop(&mut self) {
unsafe {
winapi::um::winbase::GlobalUnlock(self.0);
}
}
}