use core::ffi::c_void;
use wdk_sys::ntddk::{ExAcquireRundownProtection, ExReleaseRundownProtection};
#[derive(Debug)]
pub struct RunDownProtectionError {}
pub struct RunDownProtection {
raw: *mut c_void,
}
pub struct RunDownProtectionGuard<'a> {
run_down_protection: &'a RunDownProtection,
}
impl RunDownProtection {
pub fn from_raw(raw: *mut c_void) -> Self {
Self { raw }
}
pub fn lock(&self) -> Result<RunDownProtectionGuard, RunDownProtectionError> {
let r = unsafe { ExAcquireRundownProtection(&self.raw as *const _ as *mut _) };
if r == 0 {
return Err(RunDownProtectionError {});
}
Ok(RunDownProtectionGuard {
run_down_protection: self,
})
}
}
impl Drop for RunDownProtectionGuard<'_> {
fn drop(&mut self) {
unsafe { ExReleaseRundownProtection(&self.run_down_protection.raw as *const _ as *mut _) };
}
}