Skip to content

Commit 70c267f

Browse files
author
Jethro Beekman
committed
Windows io::Error: also format NTSTATUS error codes
1 parent 526d399 commit 70c267f

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

src/libstd/sys/windows/c.rs

+3
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,10 @@ pub const ERROR_TIMEOUT: DWORD = 0x5B4;
198198

199199
pub const INVALID_HANDLE_VALUE: HANDLE = !0 as HANDLE;
200200

201+
pub const FACILITY_NT_BIT: DWORD = 0x1000_0000;
202+
201203
pub const FORMAT_MESSAGE_FROM_SYSTEM: DWORD = 0x00001000;
204+
pub const FORMAT_MESSAGE_FROM_HMODULE: DWORD = 0x00000800;
202205
pub const FORMAT_MESSAGE_IGNORE_INSERTS: DWORD = 0x00000200;
203206

204207
pub const TLS_OUT_OF_INDEXES: DWORD = 0xFFFFFFFF;

src/libstd/sys/windows/os.rs

+21-3
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,35 @@ pub fn errno() -> i32 {
3232
}
3333

3434
/// Gets a detailed string description for the given error number.
35-
pub fn error_string(errnum: i32) -> String {
35+
pub fn error_string(mut errnum: i32) -> String {
3636
// This value is calculated from the macro
3737
// MAKELANGID(LANG_SYSTEM_DEFAULT, SUBLANG_SYS_DEFAULT)
3838
let langId = 0x0800 as c::DWORD;
3939

4040
let mut buf = [0 as c::WCHAR; 2048];
4141

4242
unsafe {
43-
let res = c::FormatMessageW(c::FORMAT_MESSAGE_FROM_SYSTEM |
43+
let mut module = ptr::null_mut();
44+
let mut flags = 0;
45+
46+
// NTSTATUS errors may be encoded as HRESULT, which may returned from
47+
// GetLastError. For more information about Windows error codes, see
48+
// `[MS-ERREF]`: https://msdn.microsoft.com/en-us/library/cc231198.aspx
49+
if (errnum & c::FACILITY_NT_BIT as i32) != 0 {
50+
// format according to https://support.microsoft.com/en-us/help/259693
51+
const NTDLL_DLL: &'static [u16] = &['N' as _, 'T' as _, 'D' as _, 'L' as _, 'L' as _,
52+
'.' as _, 'D' as _, 'L' as _, 'L' as _, 0];
53+
module = c::GetModuleHandleW(NTDLL_DLL.as_ptr());
54+
55+
if module != ptr::null_mut() {
56+
errnum ^= c::FACILITY_NT_BIT as i32;
57+
flags = c::FORMAT_MESSAGE_FROM_HMODULE;
58+
}
59+
}
60+
61+
let res = c::FormatMessageW(flags | c::FORMAT_MESSAGE_FROM_SYSTEM |
4462
c::FORMAT_MESSAGE_IGNORE_INSERTS,
45-
ptr::null_mut(),
63+
module,
4664
errnum as c::DWORD,
4765
langId,
4866
buf.as_mut_ptr(),

0 commit comments

Comments
 (0)