diff options
Diffstat (limited to 'engine-ecs')
| -rw-r--r-- | engine-ecs/Cargo.toml | 6 | ||||
| -rw-r--r-- | engine-ecs/src/error.rs | 38 |
2 files changed, 43 insertions, 1 deletions
diff --git a/engine-ecs/Cargo.toml b/engine-ecs/Cargo.toml index 001cf54..e9ce85c 100644 --- a/engine-ecs/Cargo.toml +++ b/engine-ecs/Cargo.toml @@ -21,7 +21,11 @@ vizoxide = { version = "1.0.5", optional = true } [target.'cfg(windows)'.dependencies.windows-sys] version = "0.61.2" -features = ["Win32_System_Threading"] +features = [ + "Win32_System_Threading", + "Win32_System_Diagnostics_Debug", + "Win32_Foundation" +] [target.'cfg(target_os = "linux")'.dependencies.libc] version = "0.2.186" diff --git a/engine-ecs/src/error.rs b/engine-ecs/src/error.rs index 2654dde..5ce5632 100644 --- a/engine-ecs/src/error.rs +++ b/engine-ecs/src/error.rs @@ -44,6 +44,44 @@ impl Error { Self { inner: self.inner.context(context) } } + + /// Retrieves the message text of a system-defined Windows Win32 error code and + /// returns it as a `Error`. If this fails (for example if the error code is invalid), + /// the returned `Error` contains the error code number. + #[cfg(windows)] + pub fn from_system_defined_win32_error(error: u32) -> Self + { + use std::ffi::CStr; + use std::ptr::null; + + use windows_sys::Win32::System::Diagnostics::Debug::{ + FormatMessageA, + FORMAT_MESSAGE_FROM_SYSTEM, + FORMAT_MESSAGE_IGNORE_INSERTS, + }; + + let mut buffer = vec![0u8; 256]; + + let len = unsafe { + FormatMessageA( + FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, + null(), + error, + 0, + buffer.as_mut_ptr(), + (buffer.len() - 1) as u32, + null(), + ) + } as usize; + + if len == 0 { + return Self::message(format!("Win32 error {error}")); + } + + let buf = unsafe { CStr::from_bytes_with_nul_unchecked(&buffer[..len + 1]) }; + + Self::message(buf.to_string_lossy().trim().to_owned()) + } } impl Debug for Error |
