-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #145 from microsoft/fix119
Declare HRESULT.ThrowOnFailure() method
- Loading branch information
Showing
6 changed files
with
162 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/// <summary> | ||
/// Describes an HRESULT error or success condition. | ||
/// </summary> | ||
/// <remarks> | ||
/// HRESULTs are 32 bit values layed out as follows: | ||
/// <code> | ||
/// 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 | ||
/// 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 | ||
/// +-+-+-+-+-+---------------------+-------------------------------+ | ||
/// |S|R|C|N|r| Facility | Code | | ||
/// +-+-+-+-+-+---------------------+-------------------------------+ | ||
/// | ||
/// where | ||
/// | ||
/// S - Severity - indicates success/fail | ||
/// | ||
/// 0 - Success | ||
/// 1 - Fail (COERROR) | ||
/// | ||
/// R - reserved portion of the facility code, corresponds to NT's | ||
/// second severity bit. | ||
/// | ||
/// C - reserved portion of the facility code, corresponds to NT's | ||
/// C field. | ||
/// | ||
/// N - reserved portion of the facility code. Used to indicate a | ||
/// mapped NT status value. | ||
/// | ||
/// r - reserved portion of the facility code. Reserved for internal | ||
/// use. Used to indicate HRESULT values that are not status | ||
/// values, but are instead message ids for display strings. | ||
/// | ||
/// Facility - is the facility code | ||
/// | ||
/// Code - is the facility's status code | ||
/// </code> | ||
/// </remarks> | ||
partial struct HRESULT | ||
{ | ||
public static implicit operator uint(HRESULT value) => (uint)value.Value; | ||
public static explicit operator HRESULT(uint value) => new HRESULT((int)value); | ||
|
||
[DebuggerBrowsable(DebuggerBrowsableState.Never)] | ||
internal bool Succeeded => this.Value >= 0; | ||
|
||
[DebuggerBrowsable(DebuggerBrowsableState.Never)] | ||
internal bool Failed => this.Value < 0; | ||
|
||
/// <inheritdoc cref="Marshal.ThrowExceptionForHR(int, IntPtr)" /> | ||
/// <param name="errorInfo"> | ||
/// A pointer to the IErrorInfo interface that provides more information about the | ||
/// error. You can specify <see cref="IntPtr.Zero"/> to use the current IErrorInfo interface, or | ||
/// <c>new IntPtr(-1)</c> to ignore the current IErrorInfo interface and construct the exception | ||
/// just from the error code. | ||
/// </param> | ||
/// <returns><see langword="this"/> <see cref="HRESULT"/>, if it does not reflect an error.</returns> | ||
/// <seealso cref="Marshal.ThrowExceptionForHR(int, IntPtr)"/> | ||
internal HRESULT ThrowOnFailure(IntPtr errorInfo = default) | ||
{ | ||
Marshal.ThrowExceptionForHR(this.Value, errorInfo); | ||
return this; | ||
} | ||
|
||
public override string ToString() => this.Value.ToString(); | ||
|
||
internal string ToString(string format, IFormatProvider formatProvider) => ((uint)this.Value).ToString(format, formatProvider); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/// <summary> | ||
/// Win32 return error codes. | ||
/// </summary> | ||
/// <remarks> | ||
/// This values come from https://msdn.microsoft.com/en-us/library/cc704588.aspx | ||
/// Values are 32 bit values laid out as follows: | ||
/// 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 | ||
/// 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 | ||
/// +---+-+-+-----------------------+-------------------------------+ | ||
/// |Sev|C|R| Facility | Code | | ||
/// +---+-+-+-----------------------+-------------------------------+ | ||
/// where | ||
/// Sev - is the severity code | ||
/// 00 - Success | ||
/// 01 - Informational | ||
/// 10 - Warning | ||
/// 11 - Error | ||
/// C - is the Customer code flag | ||
/// R - is a reserved bit | ||
/// Facility - is the facility code | ||
/// Code - is the facility's status code | ||
/// | ||
/// FacilityCodes 0x5 - 0xF have been allocated by various drivers. | ||
/// The success status codes 0 - 63 are reserved for wait completion status. | ||
/// </remarks> | ||
partial struct NTSTATUS | ||
{ | ||
public static implicit operator uint(NTSTATUS value) => (uint)value.Value; | ||
public static explicit operator NTSTATUS(uint value) => new NTSTATUS((int)value); | ||
|
||
internal Severity SeverityCode => (Severity)(((uint)this.Value & 0xc0000000) >> 30); | ||
|
||
internal enum Severity | ||
{ | ||
Success, | ||
Informational, | ||
Warning, | ||
Error, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ CreateFile | |
DISPLAYCONFIG_VIDEO_SIGNAL_INFO | ||
BOOL | ||
HDC_UserSize | ||
NTSTATUS |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters