/* * 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 Win32Error _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(Win32Error 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 Win32Error 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) { // We prefer native status messages because they are usually // more detailed. However, for some status values we do // prefer the shorter Win32 error message. if ( _isNtStatus && _status != NtStatus.AccessDenied && _status != NtStatus.AccessViolation ) { string message = _status.GetMessage(); if (message == null) message = "Could not retrieve the error message (0x" + ((int)_status).ToString("x") + ")."; _message = message; } else { _message = _errorCode.GetMessage(); } } return _message; } } } }