Reorganized and formatted.

pull/119/head
dahall 2020-05-04 10:21:51 -06:00
parent 1efa3a860a
commit d353468dd7
3 changed files with 394 additions and 396 deletions

View File

@ -51,6 +51,14 @@ namespace Vanara.PInvoke
private const uint severityMask = 0x80000000;
private const int severityShift = 31;
/// <summary>Initializes a new instance of the <see cref="HRESULT"/> structure.</summary>
/// <param name="rawValue">The raw HRESULT value.</param>
public HRESULT(int rawValue) => _value = rawValue;
/// <summary>Initializes a new instance of the <see cref="HRESULT"/> structure.</summary>
/// <param name="rawValue">The raw HRESULT value.</param>
public HRESULT(uint rawValue) => _value = unchecked((int)rawValue);
/// <summary>Enumeration of facility codes</summary>
[PInvokeData("winerr.h")]
public enum FacilityCode
@ -223,14 +231,6 @@ namespace Vanara.PInvoke
Fail = 1
}
/// <summary>Initializes a new instance of the <see cref="HRESULT"/> structure.</summary>
/// <param name="rawValue">The raw HRESULT value.</param>
public HRESULT(int rawValue) => _value = rawValue;
/// <summary>Initializes a new instance of the <see cref="HRESULT"/> structure.</summary>
/// <param name="rawValue">The raw HRESULT value.</param>
public HRESULT(uint rawValue) => _value = unchecked((int)rawValue);
/// <summary>Gets the code portion of the <see cref="HRESULT"/>.</summary>
/// <value>The code value (bits 0-15).</value>
public int Code => GetCode(_value);
@ -251,6 +251,102 @@ namespace Vanara.PInvoke
/// <value><c>true</c> if succeeded; otherwise, <c>false</c>.</value>
public bool Succeeded => _value >= 0;
/// <summary>Performs an explicit conversion from <see cref="System.Boolean"/> to <see cref="HRESULT"/>.</summary>
/// <param name="value">if set to <see langword="true"/> returns S_OK; otherwise S_FALSE.</param>
/// <returns>The result of the conversion.</returns>
public static explicit operator HRESULT(bool value) => value ? S_OK : S_FALSE;
/// <summary>Performs an explicit conversion from <see cref="HRESULT"/> to <see cref="System.Int32"/>.</summary>
/// <param name="value">The value.</param>
/// <returns>The result of the conversion.</returns>
public static explicit operator int(HRESULT value) => value._value;
/// <summary>Gets the code value from a 32-bit value.</summary>
/// <param name="hresult">The 32-bit raw HRESULT value.</param>
/// <returns>The code value (bits 0-15).</returns>
public static int GetCode(int hresult) => hresult & codeMask;
/// <summary>Gets the facility value from a 32-bit value.</summary>
/// <param name="hresult">The 32-bit raw HRESULT value.</param>
/// <returns>The facility value (bits 16-26).</returns>
public static FacilityCode GetFacility(int hresult) => (FacilityCode)((hresult & facilityMask) >> facilityShift);
/// <summary>Gets the severity value from a 32-bit value.</summary>
/// <param name="hresult">The 32-bit raw HRESULT value.</param>
/// <returns>The severity value (bit 31).</returns>
public static SeverityLevel GetSeverity(int hresult)
=> (SeverityLevel)((hresult & severityMask) >> severityShift);
/// <summary>Performs an implicit conversion from <see cref="System.Int32"/> to <see cref="HRESULT"/>.</summary>
/// <param name="value">The value.</param>
/// <returns>The result of the conversion.</returns>
public static implicit operator HRESULT(int value) => new HRESULT(value);
/// <summary>Performs an implicit conversion from <see cref="System.UInt32"/> to <see cref="HRESULT"/>.</summary>
/// <param name="value">The value.</param>
/// <returns>The resulting <see cref="HRESULT"/> instance from the conversion.</returns>
public static implicit operator HRESULT(uint value) => new HRESULT(value);
/// <summary>Creates a new <see cref="HRESULT"/> from provided values.</summary>
/// <param name="severe">if set to <c>false</c>, sets the severity bit to 1.</param>
/// <param name="facility">The facility.</param>
/// <param name="code">The code.</param>
/// <returns>The resulting <see cref="HRESULT"/>.</returns>
public static HRESULT Make(bool severe, FacilityCode facility, uint code) => Make(severe, (uint)facility, code);
/// <summary>Creates a new <see cref="HRESULT"/> from provided values.</summary>
/// <param name="severe">if set to <c>false</c>, sets the severity bit to 1.</param>
/// <param name="facility">The facility.</param>
/// <param name="code">The code.</param>
/// <returns>The resulting <see cref="HRESULT"/>.</returns>
public static HRESULT Make(bool severe, uint facility, uint code) =>
new HRESULT(unchecked((int)((severe ? severityMask : 0) | (facility << facilityShift) | code)));
/// <summary>Implements the operator !=.</summary>
/// <param name="hrLeft">The first <see cref="HRESULT"/>.</param>
/// <param name="hrRight">The second <see cref="HRESULT"/>.</param>
/// <returns>The result of the operator.</returns>
public static bool operator !=(HRESULT hrLeft, HRESULT hrRight) => !(hrLeft == hrRight);
/// <summary>Implements the operator !=.</summary>
/// <param name="hrLeft">The first <see cref="HRESULT"/>.</param>
/// <param name="hrRight">The second <see cref="int"/>.</param>
/// <returns>The result of the operator.</returns>
public static bool operator !=(HRESULT hrLeft, int hrRight) => !(hrLeft == hrRight);
/// <summary>Implements the operator !=.</summary>
/// <param name="hrLeft">The first <see cref="HRESULT"/>.</param>
/// <param name="hrRight">The second <see cref="uint"/>.</param>
/// <returns>The result of the operator.</returns>
public static bool operator !=(HRESULT hrLeft, uint hrRight) => !(hrLeft == hrRight);
/// <summary>Implements the operator ==.</summary>
/// <param name="hrLeft">The first <see cref="HRESULT"/>.</param>
/// <param name="hrRight">The second <see cref="HRESULT"/>.</param>
/// <returns>The result of the operator.</returns>
public static bool operator ==(HRESULT hrLeft, HRESULT hrRight) => hrLeft._value == hrRight._value;
/// <summary>Implements the operator ==.</summary>
/// <param name="hrLeft">The first <see cref="HRESULT"/>.</param>
/// <param name="hrRight">The second <see cref="int"/>.</param>
/// <returns>The result of the operator.</returns>
public static bool operator ==(HRESULT hrLeft, int hrRight) => hrLeft._value == hrRight;
/// <summary>Implements the operator ==.</summary>
/// <param name="hrLeft">The first <see cref="HRESULT"/>.</param>
/// <param name="hrRight">The second <see cref="uint"/>.</param>
/// <returns>The result of the operator.</returns>
public static bool operator ==(HRESULT hrLeft, uint hrRight) => hrLeft._value == unchecked((int)hrRight);
/// <summary>
/// If the supplied raw HRESULT value represents a failure, throw the associated <see cref="Exception"/> with the optionally
/// supplied message.
/// </summary>
/// <param name="hresult">The 32-bit raw HRESULT value.</param>
/// <param name="message">The optional message to assign to the <see cref="Exception"/>.</param>
[System.Diagnostics.DebuggerStepThrough]
public static void ThrowIfFailed(int hresult, string message = null) => new HRESULT(hresult).ThrowIfFailed(message);
/// <summary>Compares the current object with another object of the same type.</summary>
/// <param name="other">An object to compare with this object.</param>
/// <returns>
@ -288,11 +384,6 @@ namespace Vanara.PInvoke
/// <returns>true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.</returns>
public bool Equals(HRESULT other) => other._value == _value;
/// <summary>Gets the code value from a 32-bit value.</summary>
/// <param name="hresult">The 32-bit raw HRESULT value.</param>
/// <returns>The code value (bits 0-15).</returns>
public static int GetCode(int hresult) => hresult & codeMask;
/// <summary>Gets the .NET <see cref="Exception"/> associated with the HRESULT value and optionally adds the supplied message.</summary>
/// <param name="message">The optional message to assign to the <see cref="Exception"/>.</param>
/// <returns>The associated <see cref="Exception"/> or <c>null</c> if this HRESULT is not a failure.</returns>
@ -321,36 +412,10 @@ namespace Vanara.PInvoke
return exceptionForHR;
}
/// <summary>Gets the facility value from a 32-bit value.</summary>
/// <param name="hresult">The 32-bit raw HRESULT value.</param>
/// <returns>The facility value (bits 16-26).</returns>
public static FacilityCode GetFacility(int hresult) => (FacilityCode)((hresult & facilityMask) >> facilityShift);
/// <summary>Returns a hash code for this instance.</summary>
/// <returns>A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.</returns>
public override int GetHashCode() => _value;
/// <summary>Gets the severity value from a 32-bit value.</summary>
/// <param name="hresult">The 32-bit raw HRESULT value.</param>
/// <returns>The severity value (bit 31).</returns>
public static SeverityLevel GetSeverity(int hresult)
=> (SeverityLevel)((hresult & severityMask) >> severityShift);
/// <summary>Creates a new <see cref="HRESULT"/> from provided values.</summary>
/// <param name="severe">if set to <c>false</c>, sets the severity bit to 1.</param>
/// <param name="facility">The facility.</param>
/// <param name="code">The code.</param>
/// <returns>The resulting <see cref="HRESULT"/>.</returns>
public static HRESULT Make(bool severe, FacilityCode facility, uint code) => Make(severe, (uint)facility, code);
/// <summary>Creates a new <see cref="HRESULT"/> from provided values.</summary>
/// <param name="severe">if set to <c>false</c>, sets the severity bit to 1.</param>
/// <param name="facility">The facility.</param>
/// <param name="code">The code.</param>
/// <returns>The resulting <see cref="HRESULT"/>.</returns>
public static HRESULT Make(bool severe, uint facility, uint code) =>
new HRESULT(unchecked((int)((severe ? severityMask : 0) | (facility << facilityShift) | code)));
/// <summary>
/// If this <see cref="HRESULT"/> represents a failure, throw the associated <see cref="Exception"/> with the optionally supplied message.
/// </summary>
@ -364,19 +429,6 @@ namespace Vanara.PInvoke
throw exception;
}
/// <summary>
/// If the supplied raw HRESULT value represents a failure, throw the associated <see cref="Exception"/> with the optionally
/// supplied message.
/// </summary>
/// <param name="hresult">The 32-bit raw HRESULT value.</param>
/// <param name="message">The optional message to assign to the <see cref="Exception"/>.</param>
[System.Diagnostics.DebuggerStepThrough]
public static void ThrowIfFailed(int hresult, string message = null) => new HRESULT(hresult).ThrowIfFailed(message);
/// <summary>Converts this error to an <see cref="T:Vanara.PInvoke.HRESULT"/>.</summary>
/// <returns>An equivalent <see cref="T:Vanara.PInvoke.HRESULT"/>.</returns>
HRESULT IErrorProvider.ToHRESULT() => this;
/// <summary>Returns a <see cref="string"/> that represents this instance.</summary>
/// <returns>A <see cref="string"/> that represents this instance.</returns>
public override string ToString()
@ -398,106 +450,44 @@ namespace Vanara.PInvoke
return (err ?? string.Format(CultureInfo.InvariantCulture, "0x{0:X8}", _value)) + (msg == null ? "" : ": " + msg);
}
/// <summary>Implements the operator ==.</summary>
/// <param name="hrLeft">The first <see cref="HRESULT"/>.</param>
/// <param name="hrRight">The second <see cref="HRESULT"/>.</param>
/// <returns>The result of the operator.</returns>
public static bool operator ==(HRESULT hrLeft, HRESULT hrRight) => hrLeft._value == hrRight._value;
/// <summary>Implements the operator ==.</summary>
/// <param name="hrLeft">The first <see cref="HRESULT"/>.</param>
/// <param name="hrRight">The second <see cref="int"/>.</param>
/// <returns>The result of the operator.</returns>
public static bool operator ==(HRESULT hrLeft, int hrRight) => hrLeft._value == hrRight;
/// <summary>Implements the operator ==.</summary>
/// <param name="hrLeft">The first <see cref="HRESULT"/>.</param>
/// <param name="hrRight">The second <see cref="uint"/>.</param>
/// <returns>The result of the operator.</returns>
public static bool operator ==(HRESULT hrLeft, uint hrRight) => hrLeft._value == unchecked((int)hrRight);
/// <summary>Implements the operator !=.</summary>
/// <param name="hrLeft">The first <see cref="HRESULT"/>.</param>
/// <param name="hrRight">The second <see cref="HRESULT"/>.</param>
/// <returns>The result of the operator.</returns>
public static bool operator !=(HRESULT hrLeft, HRESULT hrRight) => !(hrLeft == hrRight);
/// <summary>Implements the operator !=.</summary>
/// <param name="hrLeft">The first <see cref="HRESULT"/>.</param>
/// <param name="hrRight">The second <see cref="int"/>.</param>
/// <returns>The result of the operator.</returns>
public static bool operator !=(HRESULT hrLeft, int hrRight) => !(hrLeft == hrRight);
/// <summary>Implements the operator !=.</summary>
/// <param name="hrLeft">The first <see cref="HRESULT"/>.</param>
/// <param name="hrRight">The second <see cref="uint"/>.</param>
/// <returns>The result of the operator.</returns>
public static bool operator !=(HRESULT hrLeft, uint hrRight) => !(hrLeft == hrRight);
/// <summary>Performs an implicit conversion from <see cref="System.Int32"/> to <see cref="HRESULT"/>.</summary>
/// <param name="value">The value.</param>
/// <returns>The result of the conversion.</returns>
public static implicit operator HRESULT(int value) => new HRESULT(value);
/// <summary>Performs an implicit conversion from <see cref="System.UInt32"/> to <see cref="HRESULT"/>.</summary>
/// <param name="value">The value.</param>
/// <returns>The resulting <see cref="HRESULT"/> instance from the conversion.</returns>
public static implicit operator HRESULT(uint value) => new HRESULT(value);
/// <summary>Performs an explicit conversion from <see cref="HRESULT"/> to <see cref="System.Int32"/>.</summary>
/// <param name="value">The value.</param>
/// <returns>The result of the conversion.</returns>
public static explicit operator int(HRESULT value) => value._value;
/// <summary>Performs an explicit conversion from <see cref="System.Boolean"/> to <see cref="HRESULT"/>.</summary>
/// <param name="value">if set to <see langword="true"/> returns S_OK; otherwise S_FALSE.</param>
/// <returns>The result of the conversion.</returns>
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);
/// <summary>Converts this error to an <see cref="T:Vanara.PInvoke.HRESULT"/>.</summary>
/// <returns>An equivalent <see cref="T:Vanara.PInvoke.HRESULT"/>.</returns>
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);
/// <summary>Formats the message.</summary>
/// <param name="id">The error.</param>
@ -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

View File

@ -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;
/// <summary>Initializes a new instance of the <see cref="NTStatus"/> structure.</summary>
/// <param name="rawValue">The raw NTStatus value.</param>
public NTStatus(int rawValue) => _value = rawValue;
/// <summary>Enumeration of facility codes</summary>
[PInvokeData("winerr.h")]
public enum FacilityCode : ushort
@ -233,10 +238,6 @@ namespace Vanara.PInvoke
STATUS_SEVERITY_ERROR = 0x3
}
/// <summary>Initializes a new instance of the <see cref="NTStatus"/> structure.</summary>
/// <param name="rawValue">The raw NTStatus value.</param>
public NTStatus(int rawValue) => _value = rawValue;
/// <summary>Gets the code portion of the <see cref="NTStatus"/>.</summary>
/// <value>The code value (bits 0-15).</value>
public ushort Code => GetCode(_value);
@ -261,6 +262,110 @@ namespace Vanara.PInvoke
/// <value><c>true</c> if succeeded; otherwise, <c>false</c>.</value>
public bool Succeeded => !Failed;
/// <summary>Performs an explicit conversion from <see cref="NTStatus"/> to <see cref="HRESULT"/>.</summary>
/// <param name="value">The value.</param>
/// <returns>The resulting <see cref="HRESULT"/> instance from the conversion.</returns>
public static explicit operator HRESULT(NTStatus value) => value.ToHRESULT();
/// <summary>Performs an explicit conversion from <see cref="NTStatus"/> to <see cref="System.Int32"/>.</summary>
/// <param name="value">The value.</param>
/// <returns>The result of the conversion.</returns>
public static explicit operator int(NTStatus value) => value._value;
/// <summary>Gets the code value from a 32-bit value.</summary>
/// <param name="ntstatus">The 32-bit raw NTStatus value.</param>
/// <returns>The code value (bits 0-15).</returns>
public static ushort GetCode(int ntstatus) => (ushort)(ntstatus & codeMask);
/// <summary>Gets the facility value from a 32-bit value.</summary>
/// <param name="ntstatus">The 32-bit raw NTStatus value.</param>
/// <returns>The facility value (bits 16-26).</returns>
public static FacilityCode GetFacility(int ntstatus) => (FacilityCode)((ntstatus & facilityMask) >> facilityShift);
/// <summary>Gets the severity value from a 32-bit value.</summary>
/// <param name="ntstatus">The 32-bit raw NTStatus value.</param>
/// <returns>The severity value (bit 31).</returns>
public static SeverityLevel GetSeverity(int ntstatus) => (SeverityLevel)((ntstatus & severityMask) >> severityShift);
/// <summary>Performs an implicit conversion from <see cref="System.Int32"/> to <see cref="NTStatus"/>.</summary>
/// <param name="value">The value.</param>
/// <returns>The result of the conversion.</returns>
public static implicit operator NTStatus(int value) => new NTStatus(value);
/// <summary>Performs an implicit conversion from <see cref="Win32Error"/> to <see cref="NTStatus"/>.</summary>
/// <param name="value">The value.</param>
/// <returns>The resulting <see cref="NTStatus"/> instance from the conversion.</returns>
public static implicit operator NTStatus(Win32Error value) => NTSTATUS_FROM_WIN32((uint)value);
/// <summary>Gets the customer defined bit from a 32-bit value.</summary>
/// <param name="ntstatus">The 32-bit raw NTStatus value.</param>
/// <returns><c>true</c> if the customer defined bit is set; otherwise, <c>false</c>.</returns>
public static bool IsCustomerDefined(int ntstatus) => (ntstatus & customerMask) > 0;
/// <summary>Creates a new <see cref="NTStatus"/> from provided values.</summary>
/// <param name="severity">The severity level.</param>
/// <param name="customerDefined">The bit is set for customer-defined values and clear for Microsoft-defined values.</param>
/// <param name="facility">The facility.</param>
/// <param name="code">The code.</param>
/// <returns>The resulting <see cref="NTStatus"/>.</returns>
public static NTStatus Make(SeverityLevel severity, bool customerDefined, FacilityCode facility, ushort code) => Make(severity, customerDefined, (ushort)facility, code);
/// <summary>Creates a new <see cref="NTStatus"/> from provided values.</summary>
/// <param name="severity">The severity level.</param>
/// <param name="customerDefined">The bit is set for customer-defined values and clear for Microsoft-defined values.</param>
/// <param name="facility">The facility.</param>
/// <param name="code">The code.</param>
/// <returns>The resulting <see cref="NTStatus"/>.</returns>
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)));
/// <summary>Converts a Win32 error to an NTSTATUS.</summary>
/// <param name="x">The Win32 error codex.</param>
/// <returns>The equivalent NTSTATUS value.</returns>
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));
/// <summary>Implements the operator !=.</summary>
/// <param name="hrLeft">The first <see cref="NTStatus"/>.</param>
/// <param name="hrRight">The second <see cref="NTStatus"/>.</param>
/// <returns>The result of the operator.</returns>
public static bool operator !=(NTStatus hrLeft, NTStatus hrRight) => !(hrLeft == hrRight);
/// <summary>Implements the operator !=.</summary>
/// <param name="hrLeft">The first <see cref="NTStatus"/>.</param>
/// <param name="hrRight">The second <see cref="int"/>.</param>
/// <returns>The result of the operator.</returns>
public static bool operator !=(NTStatus hrLeft, int hrRight) => !(hrLeft == hrRight);
/// <summary>Implements the operator ==.</summary>
/// <param name="hrLeft">The first <see cref="NTStatus"/>.</param>
/// <param name="hrRight">The second <see cref="NTStatus"/>.</param>
/// <returns>The result of the operator.</returns>
public static bool operator ==(NTStatus hrLeft, NTStatus hrRight) => hrLeft._value == hrRight._value;
/// <summary>Implements the operator ==.</summary>
/// <param name="hrLeft">The first <see cref="NTStatus"/>.</param>
/// <param name="hrRight">The second <see cref="int"/>.</param>
/// <returns>The result of the operator.</returns>
public static bool operator ==(NTStatus hrLeft, int hrRight) => hrLeft._value == hrRight;
/// <summary>Converts the specified NTSTATUS code to its equivalent system error code.</summary>
/// <param name="status">The NTSTATUS code to be converted.</param>
/// <returns>
/// 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.
/// </returns>
[DllImport(Lib.NtDll, ExactSpelling = true)]
[PInvokeData("Winternl.h", MSDNShortId = "ms680600")]
public static extern uint RtlNtStatusToDosError(int status);
/// <summary>
/// If the supplied raw NTStatus value represents a failure, throw the associated <see cref="Exception"/> with the optionally
/// supplied message.
/// </summary>
/// <param name="ntstatus">The 32-bit raw NTStatus value.</param>
/// <param name="message">The optional message to assign to the <see cref="Exception"/>.</param>
public static void ThrowIfFailed(int ntstatus, string message = null) => new NTStatus(ntstatus).ThrowIfFailed(message);
/// <summary>Compares the current object with another object of the same type.</summary>
/// <param name="other">An object to compare with this object.</param>
/// <returns>
@ -298,16 +403,6 @@ namespace Vanara.PInvoke
/// <returns>true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.</returns>
public bool Equals(NTStatus other) => other._value == _value;
/// <summary>Gets the code value from a 32-bit value.</summary>
/// <param name="ntstatus">The 32-bit raw NTStatus value.</param>
/// <returns>The code value (bits 0-15).</returns>
public static ushort GetCode(int ntstatus) => (ushort)(ntstatus & codeMask);
/// <summary>Gets the customer defined bit from a 32-bit value.</summary>
/// <param name="ntstatus">The 32-bit raw NTStatus value.</param>
/// <returns><c>true</c> if the customer defined bit is set; otherwise, <c>false</c>.</returns>
public static bool IsCustomerDefined(int ntstatus) => (ntstatus & customerMask) > 0;
/// <summary>Gets the .NET <see cref="Exception"/> associated with the NTStatus value and optionally adds the supplied message.</summary>
/// <param name="message">The optional message to assign to the <see cref="Exception"/>.</param>
/// <returns>The associated <see cref="Exception"/> or <c>null</c> if this NTStatus is not a failure.</returns>
@ -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;
/// <summary>Gets the facility value from a 32-bit value.</summary>
/// <param name="ntstatus">The 32-bit raw NTStatus value.</param>
/// <returns>The facility value (bits 16-26).</returns>
public static FacilityCode GetFacility(int ntstatus) => (FacilityCode)((ntstatus & facilityMask) >> facilityShift);
/// <summary>Returns a hash code for this instance.</summary>
/// <returns>A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.</returns>
public override int GetHashCode() => _value;
/// <summary>Gets the severity value from a 32-bit value.</summary>
/// <param name="ntstatus">The 32-bit raw NTStatus value.</param>
/// <returns>The severity value (bit 31).</returns>
public static SeverityLevel GetSeverity(int ntstatus) => (SeverityLevel)((ntstatus & severityMask) >> severityShift);
/// <summary>Creates a new <see cref="NTStatus"/> from provided values.</summary>
/// <param name="severity">The severity level.</param>
/// <param name="customerDefined">The bit is set for customer-defined values and clear for Microsoft-defined values.</param>
/// <param name="facility">The facility.</param>
/// <param name="code">The code.</param>
/// <returns>The resulting <see cref="NTStatus"/>.</returns>
public static NTStatus Make(SeverityLevel severity, bool customerDefined, FacilityCode facility, ushort code) => Make(severity, customerDefined, (ushort)facility, code);
/// <summary>Creates a new <see cref="NTStatus"/> from provided values.</summary>
/// <param name="severity">The severity level.</param>
/// <param name="customerDefined">The bit is set for customer-defined values and clear for Microsoft-defined values.</param>
/// <param name="facility">The facility.</param>
/// <param name="code">The code.</param>
/// <returns>The resulting <see cref="NTStatus"/>.</returns>
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)));
/// <summary>
/// If this <see cref="NTStatus"/> represents a failure, throw the associated <see cref="Exception"/> with the optionally supplied message.
/// </summary>
@ -368,19 +431,6 @@ namespace Vanara.PInvoke
throw exception;
}
/// <summary>Converts a Win32 error to an NTSTATUS.</summary>
/// <param name="x">The Win32 error codex.</param>
/// <returns>The equivalent NTSTATUS value.</returns>
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));
/// <summary>
/// If the supplied raw NTStatus value represents a failure, throw the associated <see cref="Exception"/> with the optionally
/// supplied message.
/// </summary>
/// <param name="ntstatus">The 32-bit raw NTStatus value.</param>
/// <param name="message">The optional message to assign to the <see cref="Exception"/>.</param>
public static void ThrowIfFailed(int ntstatus, string message = null) => new NTStatus(ntstatus).ThrowIfFailed(message);
/// <summary>Converts this error to an <see cref="T:Vanara.PInvoke.HRESULT"/>.</summary>
/// <returns>An equivalent <see cref="T:Vanara.PInvoke.HRESULT"/>.</returns>
public HRESULT ToHRESULT()
@ -399,49 +449,42 @@ namespace Vanara.PInvoke
return (err ?? string.Format(CultureInfo.InvariantCulture, "0x{0:X8}", _value)) + (msg == null ? "" : ": " + msg);
}
/// <summary>Implements the operator ==.</summary>
/// <param name="hrLeft">The first <see cref="NTStatus"/>.</param>
/// <param name="hrRight">The second <see cref="NTStatus"/>.</param>
/// <returns>The result of the operator.</returns>
public static bool operator ==(NTStatus hrLeft, NTStatus hrRight) => hrLeft._value == hrRight._value;
TypeCode IConvertible.GetTypeCode() => _value.GetTypeCode();
/// <summary>Implements the operator ==.</summary>
/// <param name="hrLeft">The first <see cref="NTStatus"/>.</param>
/// <param name="hrRight">The second <see cref="int"/>.</param>
/// <returns>The result of the operator.</returns>
public static bool operator ==(NTStatus hrLeft, int hrRight) => hrLeft._value == hrRight;
bool IConvertible.ToBoolean(IFormatProvider provider) => Succeeded;
/// <summary>Implements the operator !=.</summary>
/// <param name="hrLeft">The first <see cref="NTStatus"/>.</param>
/// <param name="hrRight">The second <see cref="NTStatus"/>.</param>
/// <returns>The result of the operator.</returns>
public static bool operator !=(NTStatus hrLeft, NTStatus hrRight) => !(hrLeft == hrRight);
byte IConvertible.ToByte(IFormatProvider provider) => ((IConvertible)_value).ToByte(provider);
/// <summary>Implements the operator !=.</summary>
/// <param name="hrLeft">The first <see cref="NTStatus"/>.</param>
/// <param name="hrRight">The second <see cref="int"/>.</param>
/// <returns>The result of the operator.</returns>
public static bool operator !=(NTStatus hrLeft, int hrRight) => !(hrLeft == hrRight);
char IConvertible.ToChar(IFormatProvider provider) => throw new NotSupportedException();
/// <summary>Performs an implicit conversion from <see cref="System.Int32"/> to <see cref="NTStatus"/>.</summary>
/// <param name="value">The value.</param>
/// <returns>The result of the conversion.</returns>
public static implicit operator NTStatus(int value) => new NTStatus(value);
DateTime IConvertible.ToDateTime(IFormatProvider provider) => throw new NotSupportedException();
/// <summary>Performs an implicit conversion from <see cref="Win32Error"/> to <see cref="NTStatus"/>.</summary>
/// <param name="value">The value.</param>
/// <returns>The resulting <see cref="NTStatus"/> instance from the conversion.</returns>
public static implicit operator NTStatus(Win32Error value) => NTSTATUS_FROM_WIN32((uint)value);
decimal IConvertible.ToDecimal(IFormatProvider provider) => ((IConvertible)_value).ToDecimal(provider);
/// <summary>Performs an explicit conversion from <see cref="NTStatus"/> to <see cref="System.Int32"/>.</summary>
/// <param name="value">The value.</param>
/// <returns>The result of the conversion.</returns>
public static explicit operator int(NTStatus value) => value._value;
double IConvertible.ToDouble(IFormatProvider provider) => ((IConvertible)_value).ToDouble(provider);
/// <summary>Performs an explicit conversion from <see cref="NTStatus"/> to <see cref="HRESULT"/>.</summary>
/// <param name="value">The value.</param>
/// <returns>The resulting <see cref="HRESULT"/> instance from the conversion.</returns>
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);
/// <summary>Converts the specified NTSTATUS code to its equivalent system error code.</summary>
/// <param name="status">The NTSTATUS code to be converted.</param>
/// <returns>
/// 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.
/// </returns>
[DllImport(Lib.NtDll, ExactSpelling = true)]
[PInvokeData("Winternl.h", MSDNShortId = "ms680600")]
public static extern uint RtlNtStatusToDosError(int status);
}
internal class NTStatusTypeConverter : TypeConverter

View File

@ -26,6 +26,86 @@ namespace Vanara.PInvoke
/// <value><c>true</c> if succeeded; otherwise, <c>false</c>.</value>
public bool Succeeded => value == ERROR_SUCCESS;
/// <summary>Performs an explicit conversion from <see cref="Win32Error"/> to <see cref="HRESULT"/>.</summary>
/// <param name="error">The error.</param>
/// <returns>The result of the conversion.</returns>
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);
/// <summary>Performs an explicit conversion from <see cref="Win32Error"/> to <see cref="System.Int32"/>.</summary>
/// <param name="value">The value.</param>
/// <returns>The result of the conversion.</returns>
public static explicit operator uint(Win32Error value) => value.value;
/// <summary>Tries to extract a Win32Error from an exception.</summary>
/// <param name="exception">The exception.</param>
/// <returns>The error. If undecipherable, ERROR_UNIDENTIFIED_ERROR is returned.</returns>
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;
}
/// <summary>Gets the last error.</summary>
/// <returns></returns>
[SecurityCritical]
[System.Diagnostics.DebuggerStepThrough]
public static Win32Error GetLastError() => new Win32Error(unchecked((uint)Marshal.GetLastWin32Error()));
/// <summary>Performs an explicit conversion from <see cref="System.Int32"/> to <see cref="Win32Error"/>.</summary>
/// <param name="value">The value.</param>
/// <returns>The result of the conversion.</returns>
public static implicit operator Win32Error(uint value) => new Win32Error(value);
/// <summary>Implements the operator !=.</summary>
/// <param name="errLeft">The error left.</param>
/// <param name="errRight">The error right.</param>
/// <returns>The result of the operator.</returns>
public static bool operator !=(Win32Error errLeft, Win32Error errRight) => errLeft.value != errRight.value;
/// <summary>Implements the operator !=.</summary>
/// <param name="errLeft">The error left.</param>
/// <param name="errRight">The error right.</param>
/// <returns>The result of the operator.</returns>
public static bool operator !=(Win32Error errLeft, uint errRight) => errLeft.value != errRight;
/// <summary>Implements the operator ==.</summary>
/// <param name="errLeft">The error left.</param>
/// <param name="errRight">The error right.</param>
/// <returns>The result of the operator.</returns>
public static bool operator ==(Win32Error errLeft, Win32Error errRight) => errLeft.value == errRight.value;
/// <summary>Implements the operator ==.</summary>
/// <param name="errLeft">The error left.</param>
/// <param name="errRight">The error right.</param>
/// <returns>The result of the operator.</returns>
public static bool operator ==(Win32Error errLeft, uint errRight) => errLeft.value == errRight;
/// <summary>Throws if failed.</summary>
/// <param name="err">The error.</param>
/// <param name="message">The message.</param>
[System.Diagnostics.DebuggerStepThrough]
public static void ThrowIfFailed(Win32Error err, string message = null) => err.ThrowIfFailed(message);
/// <summary>Throws the last error.</summary>
/// <param name="message">The message to associate with the exception.</param>
[System.Diagnostics.DebuggerStepThrough]
public static void ThrowLastError(string message = null) => GetLastError().ThrowIfFailed(message);
/// <summary>Throws if the last error failed, unless the error is the specified value.</summary>
/// <param name="exception">The failure code to ignore.</param>
/// <param name="message">The message to associate with the exception.</param>
[System.Diagnostics.DebuggerStepThrough]
public static void ThrowLastErrorUnless(Win32Error exception, string message = null) => GetLastError().ThrowUnless(exception, message);
/// <summary>Compares the current object with another object of the same type.</summary>
/// <param name="other">An object to compare with this object.</param>
/// <returns>
@ -79,54 +159,6 @@ namespace Vanara.PInvoke
/// <summary>Returns a hash code for this instance.</summary>
/// <returns>A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.</returns>
public override int GetHashCode() => unchecked((int)value);
/// <summary>Tries to extract a Win32Error from an exception.</summary>
/// <param name="exception">The exception.</param>
/// <returns>The error. If undecipherable, ERROR_UNIDENTIFIED_ERROR is returned.</returns>
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;
}
/// <summary>Gets the last error.</summary>
/// <returns></returns>
[SecurityCritical]
[System.Diagnostics.DebuggerStepThrough]
public static Win32Error GetLastError() => new Win32Error(unchecked((uint)Marshal.GetLastWin32Error()));
/// <summary>Throws the last error.</summary>
/// <param name="message">The message to associate with the exception.</param>
[System.Diagnostics.DebuggerStepThrough]
public static void ThrowLastError(string message = null) => GetLastError().ThrowIfFailed(message);
/// <summary>Throws if the last error failed, unless the error is the specified value.</summary>
/// <param name="exception">The failure code to ignore.</param>
/// <param name="message">The message to associate with the exception.</param>
[System.Diagnostics.DebuggerStepThrough]
public static void ThrowLastErrorUnless(Win32Error exception, string message = null) => GetLastError().ThrowUnless(exception, message);
/// <summary>Returns a <see cref="System.String"/> that represents this instance.</summary>
/// <returns>A <see cref="System.String"/> that represents this instance.</returns>
public override string ToString()
{
StaticFieldValueHash.TryGetFieldName<Win32Error, uint>(value, out var err);
var msg = HRESULT.FormatMessage(value);
return (err ?? string.Format(CultureInfo.InvariantCulture, "0x{0:X8}", value)) + (msg == null ? "" : ": " + msg);
}
/// <summary>Converts this error to an <see cref="HRESULT"/>.</summary>
/// <returns>The <see cref="HRESULT"/> equivalent of this error.</returns>
public HRESULT ToHRESULT() => (HRESULT)this;
/// <summary>Throws if failed.</summary>
/// <param name="message">The message.</param>
/// <exception cref="Win32Exception"></exception>
@ -145,51 +177,53 @@ namespace Vanara.PInvoke
if (value != ERROR_SUCCESS && value != (uint)exception) throw GetException(message);
}
/// <summary>Throws if failed.</summary>
/// <param name="err">The error.</param>
/// <param name="message">The message.</param>
[System.Diagnostics.DebuggerStepThrough]
public static void ThrowIfFailed(Win32Error err, string message = null) => err.ThrowIfFailed(message);
/// <summary>Converts this error to an <see cref="HRESULT"/>.</summary>
/// <returns>The <see cref="HRESULT"/> equivalent of this error.</returns>
public HRESULT ToHRESULT() => (HRESULT)this;
/// <summary>Performs an explicit conversion from <see cref="System.Int32"/> to <see cref="Win32Error"/>.</summary>
/// <param name="value">The value.</param>
/// <returns>The result of the conversion.</returns>
public static implicit operator Win32Error(uint value) => new Win32Error(value);
/// <summary>Returns a <see cref="System.String"/> that represents this instance.</summary>
/// <returns>A <see cref="System.String"/> that represents this instance.</returns>
public override string ToString()
{
StaticFieldValueHash.TryGetFieldName<Win32Error, uint>(value, out var err);
var msg = HRESULT.FormatMessage(value);
return (err ?? string.Format(CultureInfo.InvariantCulture, "0x{0:X8}", value)) + (msg == null ? "" : ": " + msg);
}
/// <summary>Performs an explicit conversion from <see cref="Win32Error"/> to <see cref="System.Int32"/>.</summary>
/// <param name="value">The value.</param>
/// <returns>The result of the conversion.</returns>
public static explicit operator uint(Win32Error value) => value.value;
TypeCode IConvertible.GetTypeCode() => value.GetTypeCode();
/// <summary>Performs an explicit conversion from <see cref="Win32Error"/> to <see cref="HRESULT"/>.</summary>
/// <param name="error">The error.</param>
/// <returns>The result of the conversion.</returns>
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;
/// <summary>Implements the operator ==.</summary>
/// <param name="errLeft">The error left.</param>
/// <param name="errRight">The error right.</param>
/// <returns>The result of the operator.</returns>
public static bool operator ==(Win32Error errLeft, Win32Error errRight) => errLeft.value == errRight.value;
byte IConvertible.ToByte(IFormatProvider provider) => ((IConvertible)value).ToByte(provider);
/// <summary>Implements the operator ==.</summary>
/// <param name="errLeft">The error left.</param>
/// <param name="errRight">The error right.</param>
/// <returns>The result of the operator.</returns>
public static bool operator ==(Win32Error errLeft, uint errRight) => errLeft.value == errRight;
char IConvertible.ToChar(IFormatProvider provider) => throw new NotSupportedException();
/// <summary>Implements the operator !=.</summary>
/// <param name="errLeft">The error left.</param>
/// <param name="errRight">The error right.</param>
/// <returns>The result of the operator.</returns>
public static bool operator !=(Win32Error errLeft, Win32Error errRight) => errLeft.value != errRight.value;
DateTime IConvertible.ToDateTime(IFormatProvider provider) => throw new NotSupportedException();
/// <summary>Implements the operator !=.</summary>
/// <param name="errLeft">The error left.</param>
/// <param name="errRight">The error right.</param>
/// <returns>The result of the operator.</returns>
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