use std::ffi::OsStr; use std::os::windows::ffi::OsStrExt; use windows_sys::Win32::Foundation::LocalFree; use windows_sys::Win32::Foundation::HLOCAL; use windows_sys::Win32::System::Diagnostics::Debug::FormatMessageW; use windows_sys::Win32::System::Diagnostics::Debug::FORMAT_MESSAGE_ALLOCATE_BUFFER; use windows_sys::Win32::System::Diagnostics::Debug::FORMAT_MESSAGE_FROM_SYSTEM; use windows_sys::Win32::System::Diagnostics::Debug::FORMAT_MESSAGE_IGNORE_INSERTS; pub fn to_wide>(s: S) -> Vec { let mut v: Vec = s.as_ref().encode_wide().collect(); v.push(0); v } // Produce a readable description for a Win32 error code. pub fn format_last_error(err: i32) -> String { unsafe { let mut buf_ptr: *mut u16 = std::ptr::null_mut(); let flags = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS; let len = FormatMessageW( flags, std::ptr::null(), err as u32, 0, // FORMAT_MESSAGE_ALLOCATE_BUFFER expects a pointer to receive the allocated buffer. // Cast &mut *mut u16 to *mut u16 as required by windows-sys. (&mut buf_ptr as *mut *mut u16) as *mut u16, 0, std::ptr::null_mut(), ); if len == 0 || buf_ptr.is_null() { return format!("Win32 error {}", err); } let slice = std::slice::from_raw_parts(buf_ptr, len as usize); let mut s = String::from_utf16_lossy(slice); s = s.trim().to_string(); let _ = LocalFree(buf_ptr as HLOCAL); s } }