Win32Exception.cs 5.1 KB

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