@@ -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