diff options
| author | Jethro Beekman <jethro@jbeekman.nl> | 2017-05-03 10:19:23 -0700 |
|---|---|---|
| committer | Jethro Beekman <jethro@jbeekman.nl> | 2017-05-03 10:19:23 -0700 |
| commit | 70c267fc91eddcfa47b5f6be65f8ec01d86edbe3 (patch) | |
| tree | a5e530ddd4fdd1cecd4f2088db8f7ac1a49f47b4 /src/libstd/sys/windows/os.rs | |
| parent | 526d39948af4004bb60c04dc82d60b7b395966bb (diff) | |
| download | rust-70c267fc91eddcfa47b5f6be65f8ec01d86edbe3.tar.gz rust-70c267fc91eddcfa47b5f6be65f8ec01d86edbe3.zip | |
Windows io::Error: also format NTSTATUS error codes
Diffstat (limited to 'src/libstd/sys/windows/os.rs')
| -rw-r--r-- | src/libstd/sys/windows/os.rs | 24 |
1 files changed, 21 insertions, 3 deletions
diff --git a/src/libstd/sys/windows/os.rs b/src/libstd/sys/windows/os.rs index 7e28dd1e259..402d4f9ee6e 100644 --- a/src/libstd/sys/windows/os.rs +++ b/src/libstd/sys/windows/os.rs @@ -32,7 +32,7 @@ pub fn errno() -> i32 { } /// Gets a detailed string description for the given error number. -pub fn error_string(errnum: i32) -> String { +pub fn error_string(mut errnum: i32) -> String { // This value is calculated from the macro // MAKELANGID(LANG_SYSTEM_DEFAULT, SUBLANG_SYS_DEFAULT) let langId = 0x0800 as c::DWORD; @@ -40,9 +40,27 @@ pub fn error_string(errnum: i32) -> String { let mut buf = [0 as c::WCHAR; 2048]; unsafe { - let res = c::FormatMessageW(c::FORMAT_MESSAGE_FROM_SYSTEM | + let mut module = ptr::null_mut(); + let mut flags = 0; + + // NTSTATUS errors may be encoded as HRESULT, which may returned from + // GetLastError. For more information about Windows error codes, see + // `[MS-ERREF]`: https://msdn.microsoft.com/en-us/library/cc231198.aspx + if (errnum & c::FACILITY_NT_BIT as i32) != 0 { + // format according to https://support.microsoft.com/en-us/help/259693 + const NTDLL_DLL: &'static [u16] = &['N' as _, 'T' as _, 'D' as _, 'L' as _, 'L' as _, + '.' as _, 'D' as _, 'L' as _, 'L' as _, 0]; + module = c::GetModuleHandleW(NTDLL_DLL.as_ptr()); + + if module != ptr::null_mut() { + errnum ^= c::FACILITY_NT_BIT as i32; + flags = c::FORMAT_MESSAGE_FROM_HMODULE; + } + } + + let res = c::FormatMessageW(flags | c::FORMAT_MESSAGE_FROM_SYSTEM | c::FORMAT_MESSAGE_IGNORE_INSERTS, - ptr::null_mut(), + module, errnum as c::DWORD, langId, buf.as_mut_ptr(), |
