Win32Exception.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. //
  2. // System.ComponentModel.Win32Exception.cs
  3. //
  4. // Author:
  5. // Dick Porter ([email protected])
  6. //
  7. // (C) 2002 Ximian, Inc. http://www.ximian.com
  8. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System.Security;
  30. using System.Security.Permissions;
  31. using System.Runtime.InteropServices;
  32. using System.Runtime.Serialization;
  33. using System.Collections;
  34. using System.Globalization;
  35. namespace System.ComponentModel
  36. {
  37. [Serializable, SuppressUnmanagedCodeSecurity]
  38. public class Win32Exception : ExternalException
  39. {
  40. private int native_error_code;
  41. [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
  42. public Win32Exception ()
  43. : base (W32ErrorMessage (Marshal.GetLastWin32Error ()),
  44. Marshal.GetLastWin32Error ())
  45. {
  46. native_error_code = Marshal.GetLastWin32Error ();
  47. }
  48. [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
  49. public Win32Exception (int error)
  50. : base (W32ErrorMessage (error), error)
  51. {
  52. native_error_code = error;
  53. }
  54. [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
  55. public Win32Exception (int error, string message)
  56. : base (message, error)
  57. {
  58. native_error_code = error;
  59. }
  60. #if NET_2_0
  61. [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
  62. public Win32Exception (string message)
  63. : base (message)
  64. {
  65. native_error_code = Marshal.GetLastWin32Error ();
  66. }
  67. [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
  68. public Win32Exception (string message, Exception innerException)
  69. : base (message, innerException)
  70. {
  71. native_error_code = Marshal.GetLastWin32Error ();
  72. }
  73. #endif
  74. protected Win32Exception(SerializationInfo info,
  75. StreamingContext context)
  76. : base (info, context) {
  77. native_error_code = info.GetInt32 ("NativeErrorCode");
  78. }
  79. public int NativeErrorCode {
  80. get {
  81. return(native_error_code);
  82. }
  83. }
  84. public override void GetObjectData(SerializationInfo info, StreamingContext context)
  85. {
  86. if (info==null)
  87. throw new ArgumentNullException ("info");
  88. info.AddValue ("NativeErrorCode", native_error_code);
  89. base.GetObjectData (info, context);
  90. }
  91. static string W32ErrorMessage (int error_code)
  92. {
  93. string message;
  94. switch (error_code) {
  95. case 2:
  96. message = Locale.GetText ("Cannot find the specified file");
  97. break;
  98. case 3:
  99. message = Locale.GetText ("Cannot find the specified file");
  100. break;
  101. case 10004:
  102. message = Locale.GetText ("interrupted");
  103. break;
  104. case 10013:
  105. message = Locale.GetText ("Access denied");
  106. break;
  107. case 10022:
  108. message = Locale.GetText ("Invalid arguments");
  109. break;
  110. case 10024:
  111. message = Locale.GetText ("Too many open files");
  112. break;
  113. case 10035:
  114. message = Locale.GetText ("Operation on non-blocking socket would block");
  115. break;
  116. case 10036:
  117. message = Locale.GetText ("Operation in progress");
  118. break;
  119. case 10038:
  120. message = Locale.GetText ("The descriptor is not a socket");
  121. break;
  122. case 10040:
  123. message = Locale.GetText ("Message too long");
  124. break;
  125. case 10042:
  126. message = Locale.GetText ("Protocol option not supported");
  127. break;
  128. case 10043:
  129. message = Locale.GetText ("Protocol not supported");
  130. break;
  131. case 10044:
  132. message = Locale.GetText ("Socket not supported");
  133. break;
  134. case 10045:
  135. message = Locale.GetText ("Operation not supported");
  136. break;
  137. case 10047:
  138. message = Locale.GetText ("An address incompatible with the requested protocol was used");
  139. break;
  140. case 10048:
  141. message = Locale.GetText ("Address already in use");
  142. break;
  143. case 10049:
  144. message = Locale.GetText ("The requested address is not valid in this context");
  145. break;
  146. case 10050:
  147. message = Locale.GetText ("Network subsystem is down");
  148. break;
  149. case 10051:
  150. message = Locale.GetText ("Network is unreachable");
  151. break;
  152. case 10054:
  153. message = Locale.GetText ("Connection reset by peer");
  154. break;
  155. case 10055:
  156. message = Locale.GetText ("Not enough buffer space is available");
  157. break;
  158. case 10056:
  159. message = Locale.GetText ("Socket is already connected");
  160. break;
  161. case 10057:
  162. message = Locale.GetText ("The socket is not connected");
  163. break;
  164. case 10058:
  165. message = Locale.GetText ("The socket has been shut down");
  166. break;
  167. case 10060:
  168. message = Locale.GetText ("Connection timed out");
  169. break;
  170. case 10061:
  171. message = Locale.GetText ("Connection refused");
  172. break;
  173. case 10065:
  174. message = Locale.GetText ("No route to host");
  175. break;
  176. case 10093:
  177. message = Locale.GetText ("Winsock not initialized");
  178. break;
  179. case 10107:
  180. message = Locale.GetText ("System call failed");
  181. break;
  182. case 11001:
  183. message = Locale.GetText ("No such host is known");
  184. break;
  185. case 11002:
  186. message = Locale.GetText ("A temporary error occurred on an " +
  187. "authoritative name server. Try again later.");
  188. break;
  189. default:
  190. message = Locale.GetText ("Some sort of w32 error occurred: ") + error_code;
  191. break;
  192. }
  193. return message;
  194. }
  195. }
  196. }