HRESULT.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. using System;
  2. using System.ComponentModel;
  3. using System.Diagnostics.CodeAnalysis;
  4. using System.Globalization;
  5. using System.Reflection;
  6. using System.Runtime.InteropServices;
  7. namespace Standard;
  8. [StructLayout(LayoutKind.Explicit)]
  9. internal struct HRESULT
  10. {
  11. public HRESULT(uint i)
  12. {
  13. this._value = i;
  14. }
  15. public static HRESULT Make(bool severe, Facility facility, int code)
  16. {
  17. return new HRESULT((uint) ((severe ? (Facility) (-2147483648) : Facility.Null) | (Facility) ((int) facility << 16) | (Facility) code));
  18. }
  19. public Facility Facility
  20. {
  21. get
  22. {
  23. return HRESULT.GetFacility((int) this._value);
  24. }
  25. }
  26. public static Facility GetFacility(int errorCode)
  27. {
  28. return (Facility) (errorCode >> 16 & 8191);
  29. }
  30. public int Code
  31. {
  32. get
  33. {
  34. return HRESULT.GetCode((int) this._value);
  35. }
  36. }
  37. public static int GetCode(int error)
  38. {
  39. return error & 65535;
  40. }
  41. public override string ToString()
  42. {
  43. foreach (FieldInfo fieldInfo in typeof(HRESULT).GetFields(BindingFlags.Static | BindingFlags.Public))
  44. {
  45. if (fieldInfo.FieldType == typeof(HRESULT))
  46. {
  47. HRESULT hrLeft = (HRESULT) fieldInfo.GetValue(null);
  48. if (hrLeft == this)
  49. {
  50. return fieldInfo.Name;
  51. }
  52. }
  53. }
  54. if (this.Facility == Facility.Win32)
  55. {
  56. foreach (FieldInfo fieldInfo2 in typeof(Win32Error).GetFields(BindingFlags.Static | BindingFlags.Public))
  57. {
  58. if (fieldInfo2.FieldType == typeof(Win32Error))
  59. {
  60. Win32Error error = (Win32Error) fieldInfo2.GetValue(null);
  61. if ((HRESULT) error == this)
  62. {
  63. return "HRESULT_FROM_WIN32(" + fieldInfo2.Name + ")";
  64. }
  65. }
  66. }
  67. }
  68. return string.Format(CultureInfo.InvariantCulture, "0x{0:X8}", new object[]
  69. {
  70. this._value
  71. });
  72. }
  73. public override bool Equals(object obj)
  74. {
  75. bool result;
  76. try
  77. {
  78. result = (((HRESULT) obj)._value == this._value);
  79. }
  80. catch (InvalidCastException)
  81. {
  82. result = false;
  83. }
  84. return result;
  85. }
  86. public override int GetHashCode()
  87. {
  88. return this._value.GetHashCode();
  89. }
  90. public static bool operator ==(HRESULT hrLeft, HRESULT hrRight)
  91. {
  92. return hrLeft._value == hrRight._value;
  93. }
  94. public static bool operator !=(HRESULT hrLeft, HRESULT hrRight)
  95. {
  96. return !(hrLeft == hrRight);
  97. }
  98. public bool Succeeded
  99. {
  100. get
  101. {
  102. return this._value >= 0u;
  103. }
  104. }
  105. public bool Failed
  106. {
  107. get
  108. {
  109. return this._value < 0u;
  110. }
  111. }
  112. public void ThrowIfFailed()
  113. {
  114. this.ThrowIfFailed(null);
  115. }
  116. [SuppressMessage("Microsoft.Usage", "CA2201:DoNotRaiseReservedExceptionTypes", Justification = "Only recreating Exceptions that were already raised.")]
  117. [SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands")]
  118. public void ThrowIfFailed(string message)
  119. {
  120. if (this.Failed)
  121. {
  122. if (string.IsNullOrEmpty(message))
  123. {
  124. message = this.ToString();
  125. }
  126. Exception ex = Marshal.GetExceptionForHR((int) this._value, new IntPtr(-1));
  127. if (ex.GetType() == typeof(COMException))
  128. {
  129. Facility facility = this.Facility;
  130. if (facility == Facility.Win32)
  131. {
  132. ex = new Win32Exception(this.Code, message);
  133. }
  134. else
  135. {
  136. ex = new COMException(message, (int) this._value);
  137. }
  138. }
  139. else
  140. {
  141. ConstructorInfo constructor = ex.GetType().GetConstructor(new Type[]
  142. {
  143. typeof(string)
  144. });
  145. if (null != constructor)
  146. {
  147. ex = (constructor.Invoke(new object[]
  148. {
  149. message
  150. }) as Exception);
  151. }
  152. }
  153. throw ex;
  154. }
  155. }
  156. public static void ThrowLastError()
  157. {
  158. ((HRESULT) Win32Error.GetLastError()).ThrowIfFailed();
  159. }
  160. [FieldOffset(0)]
  161. private readonly uint _value;
  162. [SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
  163. public static readonly HRESULT S_OK = new HRESULT(0u);
  164. [SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
  165. public static readonly HRESULT S_FALSE = new HRESULT(1u);
  166. [SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
  167. public static readonly HRESULT E_PENDING = new HRESULT(2147483658u);
  168. [SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
  169. public static readonly HRESULT E_NOTIMPL = new HRESULT(2147500033u);
  170. [SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
  171. public static readonly HRESULT E_NOINTERFACE = new HRESULT(2147500034u);
  172. [SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
  173. public static readonly HRESULT E_POINTER = new HRESULT(2147500035u);
  174. [SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
  175. public static readonly HRESULT E_ABORT = new HRESULT(2147500036u);
  176. [SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
  177. public static readonly HRESULT E_FAIL = new HRESULT(2147500037u);
  178. [SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
  179. public static readonly HRESULT E_UNEXPECTED = new HRESULT(2147549183u);
  180. [SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
  181. public static readonly HRESULT STG_E_INVALIDFUNCTION = new HRESULT(2147680257u);
  182. [SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
  183. public static readonly HRESULT REGDB_E_CLASSNOTREG = new HRESULT(2147746132u);
  184. [SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
  185. public static readonly HRESULT DESTS_E_NO_MATCHING_ASSOC_HANDLER = new HRESULT(2147749635u);
  186. [SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
  187. public static readonly HRESULT DESTS_E_NORECDOCS = new HRESULT(2147749636u);
  188. [SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
  189. public static readonly HRESULT DESTS_E_NOTALLCLEARED = new HRESULT(2147749637u);
  190. [SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
  191. public static readonly HRESULT E_ACCESSDENIED = new HRESULT(2147942405u);
  192. [SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
  193. public static readonly HRESULT E_OUTOFMEMORY = new HRESULT(2147942414u);
  194. [SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
  195. public static readonly HRESULT E_INVALIDARG = new HRESULT(2147942487u);
  196. [SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
  197. public static readonly HRESULT INTSAFE_E_ARITHMETIC_OVERFLOW = new HRESULT(2147942934u);
  198. [SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
  199. public static readonly HRESULT COR_E_OBJECTDISPOSED = new HRESULT(2148734498u);
  200. [SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
  201. public static readonly HRESULT WC_E_GREATERTHAN = new HRESULT(3222072867u);
  202. [SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
  203. public static readonly HRESULT WC_E_SYNTAX = new HRESULT(3222072877u);
  204. }