@@ -32,17 +32,35 @@ pub fn errno() -> i32 {
32
32
}
33
33
34
34
/// 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 {
36
36
// This value is calculated from the macro
37
37
// MAKELANGID(LANG_SYSTEM_DEFAULT, SUBLANG_SYS_DEFAULT)
38
38
let langId = 0x0800 as c:: DWORD ;
39
39
40
40
let mut buf = [ 0 as c:: WCHAR ; 2048 ] ;
41
41
42
42
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 |
44
62
c:: FORMAT_MESSAGE_IGNORE_INSERTS ,
45
- ptr :: null_mut ( ) ,
63
+ module ,
46
64
errnum as c:: DWORD ,
47
65
langId,
48
66
buf. as_mut_ptr ( ) ,
0 commit comments