/* * Process Hacker - * windows exception * * Copyright (C) 2008-2009 wj32 * * This file is part of Process Hacker. * * Process Hacker is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Process Hacker is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Process Hacker. If not, see . */ using System; using ProcessHacker.Native.Api; namespace ProcessHacker.Native { /// /// Represents a Win32 or Native exception. /// /// /// Unlike the System.ComponentModel.Win32Exception class, /// this class does not get the error's associated /// message unless it is requested. /// public class WindowsException : Exception { private bool _isNtStatus = false; private int _errorCode = 0; private NtStatus _status; private string _message = null; /// /// Creates an exception with no error. /// public WindowsException() { } /// /// Creates an exception from a Win32 error code. /// /// The Win32 error code. public WindowsException(int errorCode) { _errorCode = errorCode; } /// /// Creates an exception from a NT status value. /// /// The NT status value. public WindowsException(NtStatus status) { _status = status; _errorCode = status.ToDosError(); _isNtStatus = true; } /// /// Gets whether the NT status value is valid. /// public bool IsNtStatus { get { return _isNtStatus; } } /// /// Gets a Win32 error code which represents the exception. /// public int ErrorCode { get { return _errorCode; } } /// /// Gets a NT status value which represents the exception. /// public NtStatus Status { get { return _status; } } /// /// Gets a message describing the exception. /// public override string Message { get { // No locking, for performance reasons. Getting the // message doesn't have any side-effects anyway. if (_message == null) { if (_isNtStatus) { // Get the real NT status value. NtStatus status; IntPtr messageEntry; status = Win32.RtlFindMessage( Loader.GetDllHandle("ntdll.dll"), 0xb, System.Threading.Thread.CurrentThread.CurrentUICulture.LCID, (int)_status, out messageEntry ); if (!status.IsError()) { var region = new MemoryRegion(messageEntry); var entry = region.ReadStruct(); // Read the message, depending on format. if ((entry.Flags & MessageResourceFlags.Unicode) == MessageResourceFlags.Unicode) { _message = region.ReadUnicodeString(MessageResourceEntry.TextOffset); } else { _message = region.ReadAnsiString(MessageResourceEntry.TextOffset); } // Fix those messages which are formatted like: // {Asdf}\r\nAsdf asdf asdf... if (_message.StartsWith("{")) { string[] split = _message.Split('\n'); if (split.Length > 1) _message = split[1]; } } else { _message = "Could not retrieve the error message (0x" + ((int)status).ToString("x") + ")."; } } else { // Use the dumbass Win32 way. _message = Win32.GetErrorMessage(_errorCode); } } return _message; } } } }