use winapi::shared::minwindef::DWORD;
use winapi::shared::ntdef::NULL;
use winapi::um::errhandlingapi::GetLastError;
use winapi::um::handleapi::{CloseHandle, INVALID_HANDLE_VALUE};
use winapi::um::winnt::HANDLE;
#[repr(transparent)]
#[derive(Debug)]
pub struct Handle(HANDLE);
impl Handle {
pub unsafe fn new(h: HANDLE) -> Result<Handle, DWORD> {
if h == NULL || h == INVALID_HANDLE_VALUE {
Err(GetLastError())
} else {
Ok(Handle(h))
}
}
pub fn as_raw(&self) -> HANDLE {
self.0
}
}
impl Drop for Handle {
fn drop(&mut self) {
unsafe {
CloseHandle(self.0);
}
}
}
#[macro_export]
macro_rules! call_handle_getter {
($f:ident ( $($arg:expr),* )) => {
{
use $crate::error::{ErrorCode, FileLine, ResultExt, Win32Error};
$crate::handle::Handle::new($f($($arg),*))
.map_err(Win32Error::new)
.function(stringify!($f))
.file_line(file!(), line!())
}
};
($f:ident ( $($arg:expr),+ , )) => {
$crate::call_valid_handle_getter!($f($($arg),*))
};
}