Win32Exception.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //
  2. // System.ComponentModel.Win32Exceptioncs
  3. //
  4. // Author:
  5. // Dick Porter ([email protected])
  6. //
  7. // (C) 2002 Ximian, Inc. http://www.ximian.com
  8. //
  9. using System.Runtime.InteropServices;
  10. using System.Runtime.Serialization;
  11. using System.Collections;
  12. using System.Globalization;
  13. namespace System.ComponentModel
  14. {
  15. [Serializable]
  16. public class Win32Exception : ExternalException
  17. {
  18. private int native_error_code;
  19. public Win32Exception ()
  20. : base (W32ErrorMessage(Marshal.GetLastWin32Error()),
  21. Marshal.GetLastWin32Error()) {
  22. native_error_code=Marshal.GetLastWin32Error();
  23. }
  24. public Win32Exception(int error)
  25. : base (W32ErrorMessage(error), error) {
  26. native_error_code=error;
  27. }
  28. public Win32Exception(int error, string message)
  29. : base (message, error) {
  30. native_error_code=error;
  31. }
  32. protected Win32Exception(SerializationInfo info,
  33. StreamingContext context)
  34. : base (info, context) {
  35. }
  36. public int NativeErrorCode {
  37. get {
  38. return(native_error_code);
  39. }
  40. }
  41. public override void GetObjectData(SerializationInfo info, StreamingContext context)
  42. {
  43. if (info==null)
  44. throw new ArgumentNullException ("info");
  45. info.AddValue ("native_error_code", native_error_code);
  46. base.GetObjectData (info, context);
  47. }
  48. private static Hashtable w32_errors = new Hashtable();
  49. /* Initialise the list of error strings */
  50. static Win32Exception() {
  51. /* No need to list everything, just the ones
  52. * the runtime can throw. A list of the errors
  53. * can be found in class System.IO.MonoIOError.
  54. */
  55. w32_errors.Add(2,
  56. Locale.GetText("Cannot find the specified file"));
  57. w32_errors.Add(10004,
  58. Locale.GetText("interrupted"));
  59. w32_errors.Add(10013,
  60. Locale.GetText("Access denied"));
  61. w32_errors.Add(10022,
  62. Locale.GetText("Invalid arguments"));
  63. w32_errors.Add(10035,
  64. Locale.GetText("Operation on non-blocking socket would block"));
  65. w32_errors.Add(10036,
  66. Locale.GetText("Operation in progress"));
  67. w32_errors.Add(10038,
  68. Locale.GetText("The descriptor is not a socket"));
  69. w32_errors.Add(10043,
  70. Locale.GetText("proto no supported"));
  71. w32_errors.Add(10044,
  72. Locale.GetText("socket not supproted"));
  73. w32_errors.Add(10045,
  74. Locale.GetText("Operation not supported"));
  75. w32_errors.Add(10047,
  76. Locale.GetText("AF not supported"));
  77. w32_errors.Add(10048,
  78. Locale.GetText("Address already in use"));
  79. w32_errors.Add(10050,
  80. Locale.GetText("Network subsystem is down"));
  81. w32_errors.Add(10051,
  82. Locale.GetText("Network is unreachable"));
  83. w32_errors.Add(10055,
  84. Locale.GetText("Not enough buffer space is available"));
  85. w32_errors.Add(10056,
  86. Locale.GetText("Socket is already connected"));
  87. w32_errors.Add(10057,
  88. Locale.GetText("The socket is not connected"));
  89. w32_errors.Add(10058,
  90. Locale.GetText("The socket has been shut down"));
  91. w32_errors.Add(10060,
  92. Locale.GetText("Connection timed out"));
  93. w32_errors.Add(10061,
  94. Locale.GetText("Connection refused"));
  95. w32_errors.Add(10065,
  96. Locale.GetText("No route to host"));
  97. w32_errors.Add(10093,
  98. Locale.GetText("Winsock not initialized"));
  99. w32_errors.Add(10107,
  100. Locale.GetText("System call failed"));
  101. w32_errors.Add(11001,
  102. Locale.GetText("No such host is known"));
  103. w32_errors.Add(11002,
  104. Locale.GetText("A temporary error occurred on an authoritative name server. Try again later."));
  105. }
  106. private static string W32ErrorMessage(int error_code) {
  107. string message=(string)w32_errors[error_code];
  108. if(message==null) {
  109. return(Locale.GetText("Some sort of w32 error occurred"));
  110. } else {
  111. return(message);
  112. }
  113. }
  114. }
  115. }