Win32Exception.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. //
  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.Runtime.InteropServices;
  31. using System.Runtime.Serialization;
  32. using System.Collections;
  33. using System.Globalization;
  34. namespace System.ComponentModel
  35. {
  36. [Serializable, SuppressUnmanagedCodeSecurity]
  37. public class Win32Exception : ExternalException
  38. {
  39. private int native_error_code;
  40. public Win32Exception ()
  41. : base (W32ErrorMessage(Marshal.GetLastWin32Error()),
  42. Marshal.GetLastWin32Error()) {
  43. native_error_code=Marshal.GetLastWin32Error();
  44. }
  45. public Win32Exception(int error)
  46. : base (W32ErrorMessage(error), error) {
  47. native_error_code=error;
  48. }
  49. public Win32Exception(int error, string message)
  50. : base (message, error) {
  51. native_error_code=error;
  52. }
  53. protected Win32Exception(SerializationInfo info,
  54. StreamingContext context)
  55. : base (info, context) {
  56. native_error_code = info.GetInt32 ("NativeErrorCode");
  57. }
  58. public int NativeErrorCode {
  59. get {
  60. return(native_error_code);
  61. }
  62. }
  63. public override void GetObjectData(SerializationInfo info, StreamingContext context)
  64. {
  65. if (info==null)
  66. throw new ArgumentNullException ("info");
  67. info.AddValue ("NativeErrorCode", native_error_code);
  68. base.GetObjectData (info, context);
  69. }
  70. static string W32ErrorMessage (int error_code)
  71. {
  72. string message;
  73. switch (error_code) {
  74. case 2:
  75. message = Locale.GetText ("Cannot find the specified file");
  76. break;
  77. case 3:
  78. message = Locale.GetText ("Cannot find the specified file");
  79. break;
  80. case 10004:
  81. message = Locale.GetText ("interrupted");
  82. break;
  83. case 10013:
  84. message = Locale.GetText ("Access denied");
  85. break;
  86. case 10022:
  87. message = Locale.GetText ("Invalid arguments");
  88. break;
  89. case 10024:
  90. message = Locale.GetText ("Too many open files");
  91. break;
  92. case 10035:
  93. message = Locale.GetText ("Operation on non-blocking socket would block");
  94. break;
  95. case 10036:
  96. message = Locale.GetText ("Operation in progress");
  97. break;
  98. case 10038:
  99. message = Locale.GetText ("The descriptor is not a socket");
  100. break;
  101. case 10040:
  102. message = Locale.GetText ("Message too long");
  103. break;
  104. case 10042:
  105. message = Locale.GetText ("Protocol option not supported");
  106. break;
  107. case 10043:
  108. message = Locale.GetText ("Protocol not supported");
  109. break;
  110. case 10044:
  111. message = Locale.GetText ("Socket not supported");
  112. break;
  113. case 10045:
  114. message = Locale.GetText ("Operation not supported");
  115. break;
  116. case 10047:
  117. message = Locale.GetText ("AF not supported");
  118. break;
  119. case 10048:
  120. message = Locale.GetText ("Address already in use");
  121. break;
  122. case 10049:
  123. message = Locale.GetText ("The requested address is not valid in this context");
  124. break;
  125. case 10050:
  126. message = Locale.GetText ("Network subsystem is down");
  127. break;
  128. case 10051:
  129. message = Locale.GetText ("Network is unreachable");
  130. break;
  131. case 10054:
  132. message = Locale.GetText ("Connection reset by peer");
  133. break;
  134. case 10055:
  135. message = Locale.GetText ("Not enough buffer space is available");
  136. break;
  137. case 10056:
  138. message = Locale.GetText ("Socket is already connected");
  139. break;
  140. case 10057:
  141. message = Locale.GetText ("The socket is not connected");
  142. break;
  143. case 10058:
  144. message = Locale.GetText ("The socket has been shut down");
  145. break;
  146. case 10060:
  147. message = Locale.GetText ("Connection timed out");
  148. break;
  149. case 10061:
  150. message = Locale.GetText ("Connection refused");
  151. break;
  152. case 10065:
  153. message = Locale.GetText ("No route to host");
  154. break;
  155. case 10093:
  156. message = Locale.GetText ("Winsock not initialized");
  157. break;
  158. case 10107:
  159. message = Locale.GetText ("System call failed");
  160. break;
  161. case 11001:
  162. message = Locale.GetText ("No such host is known");
  163. break;
  164. case 11002:
  165. message = Locale.GetText ("A temporary error occurred on an " +
  166. "authoritative name server. Try again later.");
  167. break;
  168. default:
  169. message = Locale.GetText ("Some sort of w32 error occurred: ") + error_code;
  170. break;
  171. }
  172. return message;
  173. }
  174. }
  175. }