/// <summary> /// Turns NTSTATUS errors into the appropriate exception (that maps with existing .NET behavior as much as possible). /// There are additional IOException derived errors for ease of client error handling. /// </summary> public static Exception GetIoExceptionForNTStatus(NTSTATUS status, string path = null) { switch (status) { case NTSTATUS.STATUS_NOT_IMPLEMENTED: return(new NotImplementedException(path ?? WInteropStrings.NoValue)); } return(GetIoExceptionForError(ErrorMethods.NtStatusToWinError(status), path)); }
/// <summary> /// Try to get the error message for GetLastError result /// </summary> public static string LastErrorToString(WindowsError error) { string message = ErrorMethods.FormatMessage( messageId: (uint)error, source: IntPtr.Zero, flags: FormatMessageFlags.FORMAT_MESSAGE_FROM_SYSTEM); return(Enum.IsDefined(typeof(WindowsError), error) ? $"{error} ({(uint)error}): {message}" : $"Error {error}: {message}"); }
/// <summary> /// Try to get the string for an HRESULT /// </summary> public static string HResultToString(HRESULT hr) { string message; if (ErrorMacros.HRESULT_FACILITY(hr) == Facility.WIN32) { // Win32 Error, extract the code message = ErrorMethods.FormatMessage( messageId: (uint)ErrorMacros.HRESULT_CODE(hr), source: IntPtr.Zero, flags: FormatMessageFlags.FORMAT_MESSAGE_FROM_SYSTEM); } else { // Hope that we get a rational IErrorInfo Exception exception = Marshal.GetExceptionForHR((int)hr); message = exception.Message; } return($"HRESULT {(int)hr:D} [0x{(int)hr:X}]: {message}"); }