From d353468dd77925f7f24c4983fd9eb8baf983c779 Mon Sep 17 00:00:00 2001 From: dahall Date: Mon, 4 May 2020 10:21:51 -0600 Subject: [PATCH] Reorganized and formatted. --- PInvoke/Shared/WinError/HRESULT.cs | 272 ++++++++++++++++----------------- PInvoke/Shared/WinError/NTStatus.cs | 277 +++++++++++++++++----------------- PInvoke/Shared/WinError/Win32Error.cs | 241 +++++++++++++++-------------- 3 files changed, 394 insertions(+), 396 deletions(-) diff --git a/PInvoke/Shared/WinError/HRESULT.cs b/PInvoke/Shared/WinError/HRESULT.cs index 2e813dd4..dfcd3129 100644 --- a/PInvoke/Shared/WinError/HRESULT.cs +++ b/PInvoke/Shared/WinError/HRESULT.cs @@ -51,6 +51,14 @@ namespace Vanara.PInvoke private const uint severityMask = 0x80000000; private const int severityShift = 31; + /// Initializes a new instance of the structure. + /// The raw HRESULT value. + public HRESULT(int rawValue) => _value = rawValue; + + /// Initializes a new instance of the structure. + /// The raw HRESULT value. + public HRESULT(uint rawValue) => _value = unchecked((int)rawValue); + /// Enumeration of facility codes [PInvokeData("winerr.h")] public enum FacilityCode @@ -223,14 +231,6 @@ namespace Vanara.PInvoke Fail = 1 } - /// Initializes a new instance of the structure. - /// The raw HRESULT value. - public HRESULT(int rawValue) => _value = rawValue; - - /// Initializes a new instance of the structure. - /// The raw HRESULT value. - public HRESULT(uint rawValue) => _value = unchecked((int)rawValue); - /// Gets the code portion of the . /// The code value (bits 0-15). public int Code => GetCode(_value); @@ -251,6 +251,102 @@ namespace Vanara.PInvoke /// true if succeeded; otherwise, false. public bool Succeeded => _value >= 0; + /// Performs an explicit conversion from to . + /// if set to returns S_OK; otherwise S_FALSE. + /// The result of the conversion. + public static explicit operator HRESULT(bool value) => value ? S_OK : S_FALSE; + + /// Performs an explicit conversion from to . + /// The value. + /// The result of the conversion. + public static explicit operator int(HRESULT value) => value._value; + + /// Gets the code value from a 32-bit value. + /// The 32-bit raw HRESULT value. + /// The code value (bits 0-15). + public static int GetCode(int hresult) => hresult & codeMask; + + /// Gets the facility value from a 32-bit value. + /// The 32-bit raw HRESULT value. + /// The facility value (bits 16-26). + public static FacilityCode GetFacility(int hresult) => (FacilityCode)((hresult & facilityMask) >> facilityShift); + + /// Gets the severity value from a 32-bit value. + /// The 32-bit raw HRESULT value. + /// The severity value (bit 31). + public static SeverityLevel GetSeverity(int hresult) + => (SeverityLevel)((hresult & severityMask) >> severityShift); + + /// Performs an implicit conversion from to . + /// The value. + /// The result of the conversion. + public static implicit operator HRESULT(int value) => new HRESULT(value); + + /// Performs an implicit conversion from to . + /// The value. + /// The resulting instance from the conversion. + public static implicit operator HRESULT(uint value) => new HRESULT(value); + + /// Creates a new from provided values. + /// if set to false, sets the severity bit to 1. + /// The facility. + /// The code. + /// The resulting . + public static HRESULT Make(bool severe, FacilityCode facility, uint code) => Make(severe, (uint)facility, code); + + /// Creates a new from provided values. + /// if set to false, sets the severity bit to 1. + /// The facility. + /// The code. + /// The resulting . + public static HRESULT Make(bool severe, uint facility, uint code) => + new HRESULT(unchecked((int)((severe ? severityMask : 0) | (facility << facilityShift) | code))); + + /// Implements the operator !=. + /// The first . + /// The second . + /// The result of the operator. + public static bool operator !=(HRESULT hrLeft, HRESULT hrRight) => !(hrLeft == hrRight); + + /// Implements the operator !=. + /// The first . + /// The second . + /// The result of the operator. + public static bool operator !=(HRESULT hrLeft, int hrRight) => !(hrLeft == hrRight); + + /// Implements the operator !=. + /// The first . + /// The second . + /// The result of the operator. + public static bool operator !=(HRESULT hrLeft, uint hrRight) => !(hrLeft == hrRight); + + /// Implements the operator ==. + /// The first . + /// The second . + /// The result of the operator. + public static bool operator ==(HRESULT hrLeft, HRESULT hrRight) => hrLeft._value == hrRight._value; + + /// Implements the operator ==. + /// The first . + /// The second . + /// The result of the operator. + public static bool operator ==(HRESULT hrLeft, int hrRight) => hrLeft._value == hrRight; + + /// Implements the operator ==. + /// The first . + /// The second . + /// The result of the operator. + public static bool operator ==(HRESULT hrLeft, uint hrRight) => hrLeft._value == unchecked((int)hrRight); + + /// + /// If the supplied raw HRESULT value represents a failure, throw the associated with the optionally + /// supplied message. + /// + /// The 32-bit raw HRESULT value. + /// The optional message to assign to the . + [System.Diagnostics.DebuggerStepThrough] + public static void ThrowIfFailed(int hresult, string message = null) => new HRESULT(hresult).ThrowIfFailed(message); + /// Compares the current object with another object of the same type. /// An object to compare with this object. /// @@ -288,11 +384,6 @@ namespace Vanara.PInvoke /// true if the current object is equal to the parameter; otherwise, false. public bool Equals(HRESULT other) => other._value == _value; - /// Gets the code value from a 32-bit value. - /// The 32-bit raw HRESULT value. - /// The code value (bits 0-15). - public static int GetCode(int hresult) => hresult & codeMask; - /// Gets the .NET associated with the HRESULT value and optionally adds the supplied message. /// The optional message to assign to the . /// The associated or null if this HRESULT is not a failure. @@ -321,36 +412,10 @@ namespace Vanara.PInvoke return exceptionForHR; } - /// Gets the facility value from a 32-bit value. - /// The 32-bit raw HRESULT value. - /// The facility value (bits 16-26). - public static FacilityCode GetFacility(int hresult) => (FacilityCode)((hresult & facilityMask) >> facilityShift); - /// Returns a hash code for this instance. /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. public override int GetHashCode() => _value; - /// Gets the severity value from a 32-bit value. - /// The 32-bit raw HRESULT value. - /// The severity value (bit 31). - public static SeverityLevel GetSeverity(int hresult) - => (SeverityLevel)((hresult & severityMask) >> severityShift); - - /// Creates a new from provided values. - /// if set to false, sets the severity bit to 1. - /// The facility. - /// The code. - /// The resulting . - public static HRESULT Make(bool severe, FacilityCode facility, uint code) => Make(severe, (uint)facility, code); - - /// Creates a new from provided values. - /// if set to false, sets the severity bit to 1. - /// The facility. - /// The code. - /// The resulting . - public static HRESULT Make(bool severe, uint facility, uint code) => - new HRESULT(unchecked((int)((severe ? severityMask : 0) | (facility << facilityShift) | code))); - /// /// If this represents a failure, throw the associated with the optionally supplied message. /// @@ -364,19 +429,6 @@ namespace Vanara.PInvoke throw exception; } - /// - /// If the supplied raw HRESULT value represents a failure, throw the associated with the optionally - /// supplied message. - /// - /// The 32-bit raw HRESULT value. - /// The optional message to assign to the . - [System.Diagnostics.DebuggerStepThrough] - public static void ThrowIfFailed(int hresult, string message = null) => new HRESULT(hresult).ThrowIfFailed(message); - - /// Converts this error to an . - /// An equivalent . - HRESULT IErrorProvider.ToHRESULT() => this; - /// Returns a that represents this instance. /// A that represents this instance. public override string ToString() @@ -398,106 +450,44 @@ namespace Vanara.PInvoke return (err ?? string.Format(CultureInfo.InvariantCulture, "0x{0:X8}", _value)) + (msg == null ? "" : ": " + msg); } - /// Implements the operator ==. - /// The first . - /// The second . - /// The result of the operator. - public static bool operator ==(HRESULT hrLeft, HRESULT hrRight) => hrLeft._value == hrRight._value; - - /// Implements the operator ==. - /// The first . - /// The second . - /// The result of the operator. - public static bool operator ==(HRESULT hrLeft, int hrRight) => hrLeft._value == hrRight; - - /// Implements the operator ==. - /// The first . - /// The second . - /// The result of the operator. - public static bool operator ==(HRESULT hrLeft, uint hrRight) => hrLeft._value == unchecked((int)hrRight); - - /// Implements the operator !=. - /// The first . - /// The second . - /// The result of the operator. - public static bool operator !=(HRESULT hrLeft, HRESULT hrRight) => !(hrLeft == hrRight); - - /// Implements the operator !=. - /// The first . - /// The second . - /// The result of the operator. - public static bool operator !=(HRESULT hrLeft, int hrRight) => !(hrLeft == hrRight); - - /// Implements the operator !=. - /// The first . - /// The second . - /// The result of the operator. - public static bool operator !=(HRESULT hrLeft, uint hrRight) => !(hrLeft == hrRight); - - /// Performs an implicit conversion from to . - /// The value. - /// The result of the conversion. - public static implicit operator HRESULT(int value) => new HRESULT(value); - - /// Performs an implicit conversion from to . - /// The value. - /// The resulting instance from the conversion. - public static implicit operator HRESULT(uint value) => new HRESULT(value); - - /// Performs an explicit conversion from to . - /// The value. - /// The result of the conversion. - public static explicit operator int(HRESULT value) => value._value; - - /// Performs an explicit conversion from to . - /// if set to returns S_OK; otherwise S_FALSE. - /// The result of the conversion. - public static explicit operator HRESULT(bool value) => value ? S_OK : S_FALSE; - - private static int? ValueFromObj(object obj) - { - if (obj == null) return null; - var c = TypeDescriptor.GetConverter(obj); - return c.CanConvertTo(typeof(int)) ? (int?)c.ConvertTo(obj, typeof(int)) : null; - } - TypeCode IConvertible.GetTypeCode() => _value.GetTypeCode(); bool IConvertible.ToBoolean(IFormatProvider provider) => Succeeded; - char IConvertible.ToChar(IFormatProvider provider) => throw new NotSupportedException(); - - sbyte IConvertible.ToSByte(IFormatProvider provider) => ((IConvertible)_value).ToSByte(provider); - byte IConvertible.ToByte(IFormatProvider provider) => ((IConvertible)_value).ToByte(provider); - short IConvertible.ToInt16(IFormatProvider provider) => ((IConvertible)_value).ToInt16(provider); + char IConvertible.ToChar(IFormatProvider provider) => throw new NotSupportedException(); - ushort IConvertible.ToUInt16(IFormatProvider provider) => ((IConvertible)_value).ToUInt16(provider); - - int IConvertible.ToInt32(IFormatProvider provider) => ((IConvertible)_value).ToInt32(provider); - - uint IConvertible.ToUInt32(IFormatProvider provider) => ((IConvertible)_value).ToUInt32(provider); - - long IConvertible.ToInt64(IFormatProvider provider) => ((IConvertible)_value).ToInt64(provider); - - ulong IConvertible.ToUInt64(IFormatProvider provider) => ((IConvertible)_value).ToUInt64(provider); - - float IConvertible.ToSingle(IFormatProvider provider) => ((IConvertible)_value).ToSingle(provider); - - double IConvertible.ToDouble(IFormatProvider provider) => ((IConvertible)_value).ToDouble(provider); + DateTime IConvertible.ToDateTime(IFormatProvider provider) => throw new NotSupportedException(); decimal IConvertible.ToDecimal(IFormatProvider provider) => ((IConvertible)_value).ToDecimal(provider); - DateTime IConvertible.ToDateTime(IFormatProvider provider) => throw new NotSupportedException(); + double IConvertible.ToDouble(IFormatProvider provider) => ((IConvertible)_value).ToDouble(provider); + + /// Converts this error to an . + /// An equivalent . + HRESULT IErrorProvider.ToHRESULT() => this; + + short IConvertible.ToInt16(IFormatProvider provider) => ((IConvertible)_value).ToInt16(provider); + + int IConvertible.ToInt32(IFormatProvider provider) => ((IConvertible)_value).ToInt32(provider); + + long IConvertible.ToInt64(IFormatProvider provider) => ((IConvertible)_value).ToInt64(provider); + + sbyte IConvertible.ToSByte(IFormatProvider provider) => ((IConvertible)_value).ToSByte(provider); + + float IConvertible.ToSingle(IFormatProvider provider) => ((IConvertible)_value).ToSingle(provider); string IConvertible.ToString(IFormatProvider provider) => ToString(); object IConvertible.ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)_value).ToType(conversionType, provider); - [DllImport(Lib.Kernel32, SetLastError = true, CharSet = CharSet.Auto)] - private static extern int FormatMessage(uint dwFlags, HINSTANCE lpSource, uint dwMessageId, uint dwLanguageId, System.Text.StringBuilder lpBuffer, uint nSize, IntPtr Arguments); + ushort IConvertible.ToUInt16(IFormatProvider provider) => ((IConvertible)_value).ToUInt16(provider); + + uint IConvertible.ToUInt32(IFormatProvider provider) => ((IConvertible)_value).ToUInt32(provider); + + ulong IConvertible.ToUInt64(IFormatProvider provider) => ((IConvertible)_value).ToUInt64(provider); /// Formats the message. /// The error. @@ -517,6 +507,16 @@ namespace Vanara.PInvoke } while (true && buf.Capacity < 1024 * 16); // Don't go crazy throw lastError.GetException(); } + + [DllImport(Lib.Kernel32, SetLastError = true, CharSet = CharSet.Auto)] + private static extern int FormatMessage(uint dwFlags, HINSTANCE lpSource, uint dwMessageId, uint dwLanguageId, System.Text.StringBuilder lpBuffer, uint nSize, IntPtr Arguments); + + private static int? ValueFromObj(object obj) + { + if (obj == null) return null; + var c = TypeDescriptor.GetConverter(obj); + return c.CanConvertTo(typeof(int)) ? (int?)c.ConvertTo(obj, typeof(int)) : null; + } } internal class HRESULTTypeConverter : TypeConverter diff --git a/PInvoke/Shared/WinError/NTStatus.cs b/PInvoke/Shared/WinError/NTStatus.cs index adac2c08..8a84df54 100644 --- a/PInvoke/Shared/WinError/NTStatus.cs +++ b/PInvoke/Shared/WinError/NTStatus.cs @@ -42,11 +42,16 @@ namespace Vanara.PInvoke private const int codeMask = 0xFFFF; private const uint customerMask = 0x20000000; + private const int FACILITY_NT_BIT = 0x10000000; private const uint facilityMask = 0x0FFF0000; private const int facilityShift = 16; private const uint severityMask = 0xC0000000; private const int severityShift = 30; + /// Initializes a new instance of the structure. + /// The raw NTStatus value. + public NTStatus(int rawValue) => _value = rawValue; + /// Enumeration of facility codes [PInvokeData("winerr.h")] public enum FacilityCode : ushort @@ -233,10 +238,6 @@ namespace Vanara.PInvoke STATUS_SEVERITY_ERROR = 0x3 } - /// Initializes a new instance of the structure. - /// The raw NTStatus value. - public NTStatus(int rawValue) => _value = rawValue; - /// Gets the code portion of the . /// The code value (bits 0-15). public ushort Code => GetCode(_value); @@ -261,6 +262,110 @@ namespace Vanara.PInvoke /// true if succeeded; otherwise, false. public bool Succeeded => !Failed; + /// Performs an explicit conversion from to . + /// The value. + /// The resulting instance from the conversion. + public static explicit operator HRESULT(NTStatus value) => value.ToHRESULT(); + + /// Performs an explicit conversion from to . + /// The value. + /// The result of the conversion. + public static explicit operator int(NTStatus value) => value._value; + + /// Gets the code value from a 32-bit value. + /// The 32-bit raw NTStatus value. + /// The code value (bits 0-15). + public static ushort GetCode(int ntstatus) => (ushort)(ntstatus & codeMask); + + /// Gets the facility value from a 32-bit value. + /// The 32-bit raw NTStatus value. + /// The facility value (bits 16-26). + public static FacilityCode GetFacility(int ntstatus) => (FacilityCode)((ntstatus & facilityMask) >> facilityShift); + + /// Gets the severity value from a 32-bit value. + /// The 32-bit raw NTStatus value. + /// The severity value (bit 31). + public static SeverityLevel GetSeverity(int ntstatus) => (SeverityLevel)((ntstatus & severityMask) >> severityShift); + + /// Performs an implicit conversion from to . + /// The value. + /// The result of the conversion. + public static implicit operator NTStatus(int value) => new NTStatus(value); + + /// Performs an implicit conversion from to . + /// The value. + /// The resulting instance from the conversion. + public static implicit operator NTStatus(Win32Error value) => NTSTATUS_FROM_WIN32((uint)value); + + /// Gets the customer defined bit from a 32-bit value. + /// The 32-bit raw NTStatus value. + /// true if the customer defined bit is set; otherwise, false. + public static bool IsCustomerDefined(int ntstatus) => (ntstatus & customerMask) > 0; + + /// Creates a new from provided values. + /// The severity level. + /// The bit is set for customer-defined values and clear for Microsoft-defined values. + /// The facility. + /// The code. + /// The resulting . + public static NTStatus Make(SeverityLevel severity, bool customerDefined, FacilityCode facility, ushort code) => Make(severity, customerDefined, (ushort)facility, code); + + /// Creates a new from provided values. + /// The severity level. + /// The bit is set for customer-defined values and clear for Microsoft-defined values. + /// The facility. + /// The code. + /// The resulting . + public static NTStatus Make(SeverityLevel severity, bool customerDefined, ushort facility, ushort code) => + new NTStatus(unchecked((int)(((uint)severity << severityShift) | (customerDefined ? customerMask : 0U) | ((uint)facility << facilityShift) | code))); + + /// Converts a Win32 error to an NTSTATUS. + /// The Win32 error codex. + /// The equivalent NTSTATUS value. + public static NTStatus NTSTATUS_FROM_WIN32(uint x) => unchecked((int)x) <= 0 ? unchecked((int)x) : unchecked((int)(((x) & 0x0000FFFF) | ((uint)FacilityCode.FACILITY_NTWIN32 << 16) | 0xC0000000U)); + + /// Implements the operator !=. + /// The first . + /// The second . + /// The result of the operator. + public static bool operator !=(NTStatus hrLeft, NTStatus hrRight) => !(hrLeft == hrRight); + + /// Implements the operator !=. + /// The first . + /// The second . + /// The result of the operator. + public static bool operator !=(NTStatus hrLeft, int hrRight) => !(hrLeft == hrRight); + + /// Implements the operator ==. + /// The first . + /// The second . + /// The result of the operator. + public static bool operator ==(NTStatus hrLeft, NTStatus hrRight) => hrLeft._value == hrRight._value; + + /// Implements the operator ==. + /// The first . + /// The second . + /// The result of the operator. + public static bool operator ==(NTStatus hrLeft, int hrRight) => hrLeft._value == hrRight; + + /// Converts the specified NTSTATUS code to its equivalent system error code. + /// The NTSTATUS code to be converted. + /// + /// The function returns the corresponding system error code. ERROR_MR_MID_NOT_FOUND is returned when the specified NTSTATUS code + /// does not have a corresponding system error code. + /// + [DllImport(Lib.NtDll, ExactSpelling = true)] + [PInvokeData("Winternl.h", MSDNShortId = "ms680600")] + public static extern uint RtlNtStatusToDosError(int status); + + /// + /// If the supplied raw NTStatus value represents a failure, throw the associated with the optionally + /// supplied message. + /// + /// The 32-bit raw NTStatus value. + /// The optional message to assign to the . + public static void ThrowIfFailed(int ntstatus, string message = null) => new NTStatus(ntstatus).ThrowIfFailed(message); + /// Compares the current object with another object of the same type. /// An object to compare with this object. /// @@ -298,16 +403,6 @@ namespace Vanara.PInvoke /// true if the current object is equal to the parameter; otherwise, false. public bool Equals(NTStatus other) => other._value == _value; - /// Gets the code value from a 32-bit value. - /// The 32-bit raw NTStatus value. - /// The code value (bits 0-15). - public static ushort GetCode(int ntstatus) => (ushort)(ntstatus & codeMask); - - /// Gets the customer defined bit from a 32-bit value. - /// The 32-bit raw NTStatus value. - /// true if the customer defined bit is set; otherwise, false. - public static bool IsCustomerDefined(int ntstatus) => (ntstatus & customerMask) > 0; - /// Gets the .NET associated with the NTStatus value and optionally adds the supplied message. /// The optional message to assign to the . /// The associated or null if this NTStatus is not a failure. @@ -319,42 +414,10 @@ namespace Vanara.PInvoke return ToHRESULT().GetException(); } - private const int FACILITY_NT_BIT = 0x10000000; - - [ExcludeFromCodeCoverage] - private static HRESULT HRESULT_FROM_NT(int ntStatus) => ntStatus | FACILITY_NT_BIT; - - /// Gets the facility value from a 32-bit value. - /// The 32-bit raw NTStatus value. - /// The facility value (bits 16-26). - public static FacilityCode GetFacility(int ntstatus) => (FacilityCode)((ntstatus & facilityMask) >> facilityShift); - /// Returns a hash code for this instance. /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. public override int GetHashCode() => _value; - /// Gets the severity value from a 32-bit value. - /// The 32-bit raw NTStatus value. - /// The severity value (bit 31). - public static SeverityLevel GetSeverity(int ntstatus) => (SeverityLevel)((ntstatus & severityMask) >> severityShift); - - /// Creates a new from provided values. - /// The severity level. - /// The bit is set for customer-defined values and clear for Microsoft-defined values. - /// The facility. - /// The code. - /// The resulting . - public static NTStatus Make(SeverityLevel severity, bool customerDefined, FacilityCode facility, ushort code) => Make(severity, customerDefined, (ushort)facility, code); - - /// Creates a new from provided values. - /// The severity level. - /// The bit is set for customer-defined values and clear for Microsoft-defined values. - /// The facility. - /// The code. - /// The resulting . - public static NTStatus Make(SeverityLevel severity, bool customerDefined, ushort facility, ushort code) => - new NTStatus(unchecked((int)(((uint)severity << severityShift) | (customerDefined ? customerMask : 0U) | ((uint)facility << facilityShift) | code))); - /// /// If this represents a failure, throw the associated with the optionally supplied message. /// @@ -368,19 +431,6 @@ namespace Vanara.PInvoke throw exception; } - /// Converts a Win32 error to an NTSTATUS. - /// The Win32 error codex. - /// The equivalent NTSTATUS value. - public static NTStatus NTSTATUS_FROM_WIN32(uint x) => unchecked((int)x) <= 0 ? unchecked((int)x) : unchecked((int)(((x) & 0x0000FFFF) | ((uint)FacilityCode.FACILITY_NTWIN32 << 16) | 0xC0000000U)); - - /// - /// If the supplied raw NTStatus value represents a failure, throw the associated with the optionally - /// supplied message. - /// - /// The 32-bit raw NTStatus value. - /// The optional message to assign to the . - public static void ThrowIfFailed(int ntstatus, string message = null) => new NTStatus(ntstatus).ThrowIfFailed(message); - /// Converts this error to an . /// An equivalent . public HRESULT ToHRESULT() @@ -399,49 +449,42 @@ namespace Vanara.PInvoke return (err ?? string.Format(CultureInfo.InvariantCulture, "0x{0:X8}", _value)) + (msg == null ? "" : ": " + msg); } - /// Implements the operator ==. - /// The first . - /// The second . - /// The result of the operator. - public static bool operator ==(NTStatus hrLeft, NTStatus hrRight) => hrLeft._value == hrRight._value; + TypeCode IConvertible.GetTypeCode() => _value.GetTypeCode(); - /// Implements the operator ==. - /// The first . - /// The second . - /// The result of the operator. - public static bool operator ==(NTStatus hrLeft, int hrRight) => hrLeft._value == hrRight; + bool IConvertible.ToBoolean(IFormatProvider provider) => Succeeded; - /// Implements the operator !=. - /// The first . - /// The second . - /// The result of the operator. - public static bool operator !=(NTStatus hrLeft, NTStatus hrRight) => !(hrLeft == hrRight); + byte IConvertible.ToByte(IFormatProvider provider) => ((IConvertible)_value).ToByte(provider); - /// Implements the operator !=. - /// The first . - /// The second . - /// The result of the operator. - public static bool operator !=(NTStatus hrLeft, int hrRight) => !(hrLeft == hrRight); + char IConvertible.ToChar(IFormatProvider provider) => throw new NotSupportedException(); - /// Performs an implicit conversion from to . - /// The value. - /// The result of the conversion. - public static implicit operator NTStatus(int value) => new NTStatus(value); + DateTime IConvertible.ToDateTime(IFormatProvider provider) => throw new NotSupportedException(); - /// Performs an implicit conversion from to . - /// The value. - /// The resulting instance from the conversion. - public static implicit operator NTStatus(Win32Error value) => NTSTATUS_FROM_WIN32((uint)value); + decimal IConvertible.ToDecimal(IFormatProvider provider) => ((IConvertible)_value).ToDecimal(provider); - /// Performs an explicit conversion from to . - /// The value. - /// The result of the conversion. - public static explicit operator int(NTStatus value) => value._value; + double IConvertible.ToDouble(IFormatProvider provider) => ((IConvertible)_value).ToDouble(provider); - /// Performs an explicit conversion from to . - /// The value. - /// The resulting instance from the conversion. - public static explicit operator HRESULT(NTStatus value) => value.ToHRESULT(); + short IConvertible.ToInt16(IFormatProvider provider) => ((IConvertible)_value).ToInt16(provider); + + int IConvertible.ToInt32(IFormatProvider provider) => ((IConvertible)_value).ToInt32(provider); + + long IConvertible.ToInt64(IFormatProvider provider) => ((IConvertible)_value).ToInt64(provider); + + sbyte IConvertible.ToSByte(IFormatProvider provider) => ((IConvertible)_value).ToSByte(provider); + + float IConvertible.ToSingle(IFormatProvider provider) => ((IConvertible)_value).ToSingle(provider); + + string IConvertible.ToString(IFormatProvider provider) => ToString(); + + object IConvertible.ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)_value).ToType(conversionType, provider); + + ushort IConvertible.ToUInt16(IFormatProvider provider) => ((IConvertible)_value).ToUInt16(provider); + + uint IConvertible.ToUInt32(IFormatProvider provider) => ((IConvertible)_value).ToUInt32(provider); + + ulong IConvertible.ToUInt64(IFormatProvider provider) => ((IConvertible)_value).ToUInt64(provider); + + [ExcludeFromCodeCoverage] + private static HRESULT HRESULT_FROM_NT(int ntStatus) => ntStatus | FACILITY_NT_BIT; private static int? ValueFromObj(object obj) { @@ -449,50 +492,6 @@ namespace Vanara.PInvoke var c = TypeDescriptor.GetConverter(obj); return c.CanConvertTo(typeof(int)) ? (int?)c.ConvertTo(obj, typeof(int)) : null; } - - TypeCode IConvertible.GetTypeCode() => _value.GetTypeCode(); - - bool IConvertible.ToBoolean(IFormatProvider provider) => Succeeded; - - char IConvertible.ToChar(IFormatProvider provider) => throw new NotSupportedException(); - - sbyte IConvertible.ToSByte(IFormatProvider provider) => ((IConvertible)_value).ToSByte(provider); - - byte IConvertible.ToByte(IFormatProvider provider) => ((IConvertible)_value).ToByte(provider); - - short IConvertible.ToInt16(IFormatProvider provider) => ((IConvertible)_value).ToInt16(provider); - - ushort IConvertible.ToUInt16(IFormatProvider provider) => ((IConvertible)_value).ToUInt16(provider); - - int IConvertible.ToInt32(IFormatProvider provider) => ((IConvertible)_value).ToInt32(provider); - - uint IConvertible.ToUInt32(IFormatProvider provider) => ((IConvertible)_value).ToUInt32(provider); - - long IConvertible.ToInt64(IFormatProvider provider) => ((IConvertible)_value).ToInt64(provider); - - ulong IConvertible.ToUInt64(IFormatProvider provider) => ((IConvertible)_value).ToUInt64(provider); - - float IConvertible.ToSingle(IFormatProvider provider) => ((IConvertible)_value).ToSingle(provider); - - double IConvertible.ToDouble(IFormatProvider provider) => ((IConvertible)_value).ToDouble(provider); - - decimal IConvertible.ToDecimal(IFormatProvider provider) => ((IConvertible)_value).ToDecimal(provider); - - DateTime IConvertible.ToDateTime(IFormatProvider provider) => throw new NotSupportedException(); - - string IConvertible.ToString(IFormatProvider provider) => ToString(); - - object IConvertible.ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)_value).ToType(conversionType, provider); - - /// Converts the specified NTSTATUS code to its equivalent system error code. - /// The NTSTATUS code to be converted. - /// - /// The function returns the corresponding system error code. ERROR_MR_MID_NOT_FOUND is returned when the specified NTSTATUS code - /// does not have a corresponding system error code. - /// - [DllImport(Lib.NtDll, ExactSpelling = true)] - [PInvokeData("Winternl.h", MSDNShortId = "ms680600")] - public static extern uint RtlNtStatusToDosError(int status); } internal class NTStatusTypeConverter : TypeConverter diff --git a/PInvoke/Shared/WinError/Win32Error.cs b/PInvoke/Shared/WinError/Win32Error.cs index ceb28cf9..e597f931 100644 --- a/PInvoke/Shared/WinError/Win32Error.cs +++ b/PInvoke/Shared/WinError/Win32Error.cs @@ -26,6 +26,86 @@ namespace Vanara.PInvoke /// true if succeeded; otherwise, false. public bool Succeeded => value == ERROR_SUCCESS; + /// Performs an explicit conversion from to . + /// The error. + /// The result of the conversion. + public static explicit operator HRESULT(Win32Error error) => + unchecked((int)error.value) <= 0 ? unchecked((int)error.value) : HRESULT.Make(true, HRESULT.FacilityCode.FACILITY_WIN32, error.value & 0xffff); + + /// Performs an explicit conversion from to . + /// The value. + /// The result of the conversion. + public static explicit operator uint(Win32Error value) => value.value; + + /// Tries to extract a Win32Error from an exception. + /// The exception. + /// The error. If undecipherable, ERROR_UNIDENTIFIED_ERROR is returned. + public static Win32Error FromException(Exception exception) + { + if (exception is Win32Exception we) + return unchecked((uint)we.NativeErrorCode); + if (exception.InnerException is Win32Exception iwe) + return unchecked((uint)iwe.NativeErrorCode); +#if !(NET20 || NET35 || NET40) + var hr = new HRESULT(exception.HResult); + if (hr.Facility == HRESULT.FacilityCode.FACILITY_WIN32) + return unchecked((uint)hr.Code); +#endif + return ERROR_UNIDENTIFIED_ERROR; + } + + /// Gets the last error. + /// + [SecurityCritical] + [System.Diagnostics.DebuggerStepThrough] + public static Win32Error GetLastError() => new Win32Error(unchecked((uint)Marshal.GetLastWin32Error())); + + /// Performs an explicit conversion from to . + /// The value. + /// The result of the conversion. + public static implicit operator Win32Error(uint value) => new Win32Error(value); + + /// Implements the operator !=. + /// The error left. + /// The error right. + /// The result of the operator. + public static bool operator !=(Win32Error errLeft, Win32Error errRight) => errLeft.value != errRight.value; + + /// Implements the operator !=. + /// The error left. + /// The error right. + /// The result of the operator. + public static bool operator !=(Win32Error errLeft, uint errRight) => errLeft.value != errRight; + + /// Implements the operator ==. + /// The error left. + /// The error right. + /// The result of the operator. + public static bool operator ==(Win32Error errLeft, Win32Error errRight) => errLeft.value == errRight.value; + + /// Implements the operator ==. + /// The error left. + /// The error right. + /// The result of the operator. + public static bool operator ==(Win32Error errLeft, uint errRight) => errLeft.value == errRight; + + /// Throws if failed. + /// The error. + /// The message. + [System.Diagnostics.DebuggerStepThrough] + public static void ThrowIfFailed(Win32Error err, string message = null) => err.ThrowIfFailed(message); + + /// Throws the last error. + /// The message to associate with the exception. + [System.Diagnostics.DebuggerStepThrough] + public static void ThrowLastError(string message = null) => GetLastError().ThrowIfFailed(message); + + /// Throws if the last error failed, unless the error is the specified value. + /// The failure code to ignore. + /// The message to associate with the exception. + [System.Diagnostics.DebuggerStepThrough] + public static void ThrowLastErrorUnless(Win32Error exception, string message = null) => GetLastError().ThrowUnless(exception, message); + /// Compares the current object with another object of the same type. /// An object to compare with this object. /// @@ -79,54 +159,6 @@ namespace Vanara.PInvoke /// Returns a hash code for this instance. /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. public override int GetHashCode() => unchecked((int)value); - - /// Tries to extract a Win32Error from an exception. - /// The exception. - /// The error. If undecipherable, ERROR_UNIDENTIFIED_ERROR is returned. - public static Win32Error FromException(Exception exception) - { - if (exception is Win32Exception we) - return unchecked((uint)we.NativeErrorCode); - if (exception.InnerException is Win32Exception iwe) - return unchecked((uint)iwe.NativeErrorCode); -#if !(NET20 || NET35 || NET40) - var hr = new HRESULT(exception.HResult); - if (hr.Facility == HRESULT.FacilityCode.FACILITY_WIN32) - return unchecked((uint)hr.Code); -#endif - return ERROR_UNIDENTIFIED_ERROR; - } - - /// Gets the last error. - /// - [SecurityCritical] - [System.Diagnostics.DebuggerStepThrough] - public static Win32Error GetLastError() => new Win32Error(unchecked((uint)Marshal.GetLastWin32Error())); - - /// Throws the last error. - /// The message to associate with the exception. - [System.Diagnostics.DebuggerStepThrough] - public static void ThrowLastError(string message = null) => GetLastError().ThrowIfFailed(message); - - /// Throws if the last error failed, unless the error is the specified value. - /// The failure code to ignore. - /// The message to associate with the exception. - [System.Diagnostics.DebuggerStepThrough] - public static void ThrowLastErrorUnless(Win32Error exception, string message = null) => GetLastError().ThrowUnless(exception, message); - - /// Returns a that represents this instance. - /// A that represents this instance. - public override string ToString() - { - StaticFieldValueHash.TryGetFieldName(value, out var err); - var msg = HRESULT.FormatMessage(value); - return (err ?? string.Format(CultureInfo.InvariantCulture, "0x{0:X8}", value)) + (msg == null ? "" : ": " + msg); - } - - /// Converts this error to an . - /// The equivalent of this error. - public HRESULT ToHRESULT() => (HRESULT)this; - /// Throws if failed. /// The message. /// @@ -145,51 +177,53 @@ namespace Vanara.PInvoke if (value != ERROR_SUCCESS && value != (uint)exception) throw GetException(message); } - /// Throws if failed. - /// The error. - /// The message. - [System.Diagnostics.DebuggerStepThrough] - public static void ThrowIfFailed(Win32Error err, string message = null) => err.ThrowIfFailed(message); + /// Converts this error to an . + /// The equivalent of this error. + public HRESULT ToHRESULT() => (HRESULT)this; - /// Performs an explicit conversion from to . - /// The value. - /// The result of the conversion. - public static implicit operator Win32Error(uint value) => new Win32Error(value); + /// Returns a that represents this instance. + /// A that represents this instance. + public override string ToString() + { + StaticFieldValueHash.TryGetFieldName(value, out var err); + var msg = HRESULT.FormatMessage(value); + return (err ?? string.Format(CultureInfo.InvariantCulture, "0x{0:X8}", value)) + (msg == null ? "" : ": " + msg); + } - /// Performs an explicit conversion from to . - /// The value. - /// The result of the conversion. - public static explicit operator uint(Win32Error value) => value.value; + TypeCode IConvertible.GetTypeCode() => value.GetTypeCode(); - /// Performs an explicit conversion from to . - /// The error. - /// The result of the conversion. - public static explicit operator HRESULT(Win32Error error) => - unchecked((int)error.value) <= 0 ? unchecked((int)error.value) : HRESULT.Make(true, HRESULT.FacilityCode.FACILITY_WIN32, error.value & 0xffff); + bool IConvertible.ToBoolean(IFormatProvider provider) => Succeeded; - /// Implements the operator ==. - /// The error left. - /// The error right. - /// The result of the operator. - public static bool operator ==(Win32Error errLeft, Win32Error errRight) => errLeft.value == errRight.value; + byte IConvertible.ToByte(IFormatProvider provider) => ((IConvertible)value).ToByte(provider); - /// Implements the operator ==. - /// The error left. - /// The error right. - /// The result of the operator. - public static bool operator ==(Win32Error errLeft, uint errRight) => errLeft.value == errRight; + char IConvertible.ToChar(IFormatProvider provider) => throw new NotSupportedException(); - /// Implements the operator !=. - /// The error left. - /// The error right. - /// The result of the operator. - public static bool operator !=(Win32Error errLeft, Win32Error errRight) => errLeft.value != errRight.value; + DateTime IConvertible.ToDateTime(IFormatProvider provider) => throw new NotSupportedException(); - /// Implements the operator !=. - /// The error left. - /// The error right. - /// The result of the operator. - public static bool operator !=(Win32Error errLeft, uint errRight) => errLeft.value != errRight; + decimal IConvertible.ToDecimal(IFormatProvider provider) => ((IConvertible)value).ToDecimal(provider); + + double IConvertible.ToDouble(IFormatProvider provider) => ((IConvertible)value).ToDouble(provider); + + short IConvertible.ToInt16(IFormatProvider provider) => ((IConvertible)value).ToInt16(provider); + + int IConvertible.ToInt32(IFormatProvider provider) => ((IConvertible)value).ToInt32(provider); + + long IConvertible.ToInt64(IFormatProvider provider) => ((IConvertible)value).ToInt64(provider); + + sbyte IConvertible.ToSByte(IFormatProvider provider) => ((IConvertible)value).ToSByte(provider); + + float IConvertible.ToSingle(IFormatProvider provider) => ((IConvertible)value).ToSingle(provider); + + string IConvertible.ToString(IFormatProvider provider) => ToString(); + + object IConvertible.ToType(Type conversionType, IFormatProvider provider) => + ((IConvertible)value).ToType(conversionType, provider); + + ushort IConvertible.ToUInt16(IFormatProvider provider) => ((IConvertible)value).ToUInt16(provider); + + uint IConvertible.ToUInt32(IFormatProvider provider) => ((IConvertible)value).ToUInt32(provider); + + ulong IConvertible.ToUInt64(IFormatProvider provider) => ((IConvertible)value).ToUInt64(provider); private static uint? ValueFromObj(object obj) { @@ -197,41 +231,6 @@ namespace Vanara.PInvoke var c = TypeDescriptor.GetConverter(obj); return c.CanConvertTo(typeof(uint)) ? (uint?)c.ConvertTo(obj, typeof(uint)) : null; } - - TypeCode IConvertible.GetTypeCode() => value.GetTypeCode(); - - bool IConvertible.ToBoolean(IFormatProvider provider) => Succeeded; - - char IConvertible.ToChar(IFormatProvider provider) => throw new NotSupportedException(); - - sbyte IConvertible.ToSByte(IFormatProvider provider) => ((IConvertible)value).ToSByte(provider); - - byte IConvertible.ToByte(IFormatProvider provider) => ((IConvertible)value).ToByte(provider); - - short IConvertible.ToInt16(IFormatProvider provider) => ((IConvertible)value).ToInt16(provider); - - ushort IConvertible.ToUInt16(IFormatProvider provider) => ((IConvertible)value).ToUInt16(provider); - - int IConvertible.ToInt32(IFormatProvider provider) => ((IConvertible)value).ToInt32(provider); - - uint IConvertible.ToUInt32(IFormatProvider provider) => ((IConvertible)value).ToUInt32(provider); - - long IConvertible.ToInt64(IFormatProvider provider) => ((IConvertible)value).ToInt64(provider); - - ulong IConvertible.ToUInt64(IFormatProvider provider) => ((IConvertible)value).ToUInt64(provider); - - float IConvertible.ToSingle(IFormatProvider provider) => ((IConvertible)value).ToSingle(provider); - - double IConvertible.ToDouble(IFormatProvider provider) => ((IConvertible)value).ToDouble(provider); - - decimal IConvertible.ToDecimal(IFormatProvider provider) => ((IConvertible)value).ToDecimal(provider); - - DateTime IConvertible.ToDateTime(IFormatProvider provider) => throw new NotSupportedException(); - - string IConvertible.ToString(IFormatProvider provider) => ToString(); - - object IConvertible.ToType(Type conversionType, IFormatProvider provider) => - ((IConvertible)value).ToType(conversionType, provider); } internal class Win32ErrorTypeConverter : TypeConverter