diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index a9b2998..72e43da 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -28,7 +28,6 @@ jobs: if: matrix.rust == '1.56' run: | cargo update -p libc --precise 0.2.163 - cargo update -p windows-sys --precise 0.52.0 - name: Setup cache uses: Swatinem/rust-cache@v2 - name: Test (no features) diff --git a/Cargo.toml b/Cargo.toml index f3f5ce0..25c1452 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,20 +12,7 @@ description = "Cross-platform interface to the `errno` variable." categories = ["no-std", "os"] rust-version = "1.56" -[target.'cfg(unix)'.dependencies] -libc = { version = "0.2", default-features = false } - -[target.'cfg(windows)'.dependencies.windows-sys] -version = ">=0.52, <0.62" -features = [ - "Win32_Foundation", - "Win32_System_Diagnostics_Debug", -] - -[target.'cfg(target_os="wasi")'.dependencies] -libc = { version = "0.2", default-features = false } - -[target.'cfg(target_os="hermit")'.dependencies] +[target.'cfg(any(unix, target_os="wasi", target_os="hermit"))'.dependencies] libc = { version = "0.2", default-features = false } [features] diff --git a/src/windows.rs b/src/windows.rs index 9c7c0e4..88e49c3 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -15,13 +15,36 @@ use core::char::{self, REPLACEMENT_CHARACTER}; use core::ptr; use core::str; -use windows_sys::Win32::Foundation::{GetLastError, SetLastError, WIN32_ERROR}; -use windows_sys::Win32::System::Diagnostics::Debug::{ - FormatMessageW, FORMAT_MESSAGE_FROM_SYSTEM, FORMAT_MESSAGE_IGNORE_INSERTS, -}; use crate::Errno; +/// Insert sequences in the message definition such as %1 are to be ignored and passed through to the output buffer unchanged. +/// +/// This flag is useful for fetching a message for later formatting. If this flag is set, the `arguments` parameter is ignored. +const FORMAT_MESSAGE_IGNORE_INSERTS: u32 = 512; +/// The function should search the system message-table resource(s) for the requested message. +/// +/// If this flag is specified, an application can pass the result of the GetLastError function to retrieve the message text for a system-defined error. +const FORMAT_MESSAGE_FROM_SYSTEM: u32 = 4096; + +#[link(name = "kernel32")] +extern "system" { + /// + fn FormatMessageW( + flags: u32, + source: *const core::ffi::c_void, + message_id: u32, + language_id: u32, + buffer: *mut u16, + size: u32, + arguments: *const *const i8, + ) -> u32; + /// + fn GetLastError() -> u32; + /// + fn SetLastError(err_code: u32); +} + fn from_utf16_lossy<'a>(input: &[u16], output: &'a mut [u8]) -> &'a str { let mut output_len = 0; for c in char::decode_utf16(input.iter().copied().take_while(|&x| x != 0)) @@ -77,5 +100,5 @@ pub fn errno() -> Errno { } pub fn set_errno(Errno(errno): Errno) { - unsafe { SetLastError(errno as WIN32_ERROR) } + unsafe { SetLastError(errno as u32) } }