use windows::Win32::Foundation::TRUE;
use windows::Win32::System::Console::SetConsoleCtrlHandler;
#[derive(Debug, Clone, Copy)]
pub struct CtrlHandlerError(i32);
impl CtrlHandlerError {
#[must_use]
pub const fn code(&self) -> i32 {
self.0
}
}
#[cfg(feature = "std")]
impl std::fmt::Display for CtrlHandlerError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"failed to set console control handler (os error {})",
self.0
)
}
}
#[cfg(feature = "std")]
impl std::error::Error for CtrlHandlerError {}
#[allow(unsafe_code)]
pub fn install_ctrl_handler() -> Result<(), CtrlHandlerError> {
unsafe extern "system" fn handler(_: u32) -> windows::core::BOOL {
TRUE
}
unsafe { SetConsoleCtrlHandler(Some(handler), true) }.map_err(|e| CtrlHandlerError(e.code().0))
}