winwrap/um/
errhandlingapi.rs1use crate::*;
2use crate::handle::*;
3use crate::raw::um::errhandlingapi::*;
4use crate::string::*;
5use crate::um::winnt::ExceptionPointers;
6use crate::vc::excpt::ExceptionHandler;
7use winapi::shared::minwindef::{UINT, DWORD};
8use winapi::um::winnt::PVECTORED_EXCEPTION_HANDLER;
9use winwrap_derive::*;
10
11#[repr(u32)]
12#[allow(non_camel_case_types)]
13pub enum ErrorMode {
14 FailCriticalErrors = 0x0001,
15 NoAlignmentFaultExcept = 0x0004,
16 NoGPFaultErrorBox = 0x0002,
17 NoOpenFileErrorBox = 0x8000,
18}
19
20impl From<UINT> for ErrorMode {
21 fn from(x: u32) -> Self {
22 match x {
23 0x0001 => Self::FailCriticalErrors,
24 0x0004 => Self::NoAlignmentFaultExcept,
25 0x0002 => Self::NoGPFaultErrorBox,
26 0x8000 => Self::NoOpenFileErrorBox,
27 x => panic!("Unknown ErrorMode: {}", x),
28 }
29 }
30}
31
32pub fn get_last_error() -> DWORD { GetLastError() }
33
34pub fn set_last_error(err_code: DWORD) { SetLastError(err_code) }
35
36pub fn get_error_mode() -> ErrorMode { ErrorMode::from(GetErrorMode()) }
37
38pub fn set_error_mode(err_mode: ErrorMode) -> ErrorMode { ErrorMode::from(SetErrorMode(err_mode as u32)) }
39
40#[ansi_fn]
41pub fn fatal_app_exit_a(
42 action: UINT,
43 message_text: &AStr,
44) {
45 unsafe {
46 FatalAppExitA(action, message_text.as_ptr())
47 }
48}
49
50#[unicode_fn]
51pub fn fatal_app_exit_w(
52 action: UINT,
53 message_text: &WStr,
54) {
55 unsafe {
56 FatalAppExitW(action, message_text.as_ptr())
57 }
58}
59
60pub fn get_thread_error_mode() -> ErrorMode { ErrorMode::from(GetThreadErrorMode()) }
61
62pub fn set_thread_error_mode(
63 err_mode: ErrorMode
64) -> OsResult<ErrorMode> {
65 unsafe {
66 let mut old_mode = 0;
67 SetThreadErrorMode(err_mode as u32, &mut old_mode)?;
68 Ok(ErrorMode::from(old_mode))
69 }
70}
71
72pub type VectoredExceptionHandler = extern "system" fn(&mut ExceptionPointers) -> ExceptionHandler;
74
75pub fn add_vectored_exception_handler(
76 is_first: bool,
77 func: VectoredExceptionHandler,
78) -> Result<VEHHandle, ()> {
79 unsafe {
80 let func = std::mem::transmute::<_, PVECTORED_EXCEPTION_HANDLER>(func);
81 Ok(VEHHandle::new(AddVectoredExceptionHandler(is_first.into(), func)?))
82 }
83}
84
85pub unsafe fn remove_vectored_exception_handler(
86 handle: VEHHandle,
87) -> Result<(), ()> {
88 RemoveVectoredExceptionHandler(handle.as_c_handle())?;
89 std::mem::forget(handle);
90 Ok(())
91}
92
93pub fn add_vectored_continue_handler(
94 is_first: bool,
95 func: VectoredExceptionHandler,
96) -> Result<VCHHandle, ()> {
97 unsafe {
98 let func = std::mem::transmute::<_, PVECTORED_EXCEPTION_HANDLER>(func);
99 Ok(VCHHandle::new(AddVectoredContinueHandler(is_first.into(), func)?))
100 }
101}
102
103pub unsafe fn remove_vectored_continue_handler(
104 handle: VCHHandle,
105) -> Result<(), ()> {
106 RemoveVectoredContinueHandler(handle.as_c_handle())?;
107 std::mem::forget(handle);
108 Ok(())
109}