* System_test.dll.sources: Added Win32ExceptionTest.cs.
[mono.git] / mcs / class / System / System.ComponentModel / Win32Exception.cs
1 //
2 // System.ComponentModel.Win32Exception.cs
3 //
4 // Author:
5 //   Dick Porter (dick@ximian.com)
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
30 using System.Security;
31 using System.Security.Permissions;
32 using System.Runtime.InteropServices;
33 using System.Runtime.Serialization;
34 using System.Collections;
35 using System.Globalization;
36
37 namespace System.ComponentModel
38 {
39         [Serializable, SuppressUnmanagedCodeSecurity]
40         public class Win32Exception : ExternalException
41         {
42                 private int native_error_code;
43
44 //              [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
45 #if TARGET_JVM
46                 [MonoNotSupported("")]
47 #endif
48                 public Win32Exception ()
49                         : base (W32ErrorMessage (Marshal.GetLastWin32Error ()))
50                 {
51                         native_error_code = Marshal.GetLastWin32Error ();
52                 }
53
54 //              [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
55                 public Win32Exception (int error)
56                         : base (W32ErrorMessage (error))
57                 {
58                         native_error_code = error;
59                 }
60
61 //              [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
62                 public Win32Exception (int error, string message) 
63                         : base (message)
64                 {
65                         native_error_code = error;
66                 }
67 #if NET_2_0
68                 [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
69 #if TARGET_JVM
70                 [MonoNotSupported ("")]
71 #endif
72                 public Win32Exception (string message)
73                         : base (message)
74                 {
75                         native_error_code = Marshal.GetLastWin32Error ();
76                 }
77
78 #if TARGET_JVM
79                 [MonoNotSupported ("")]
80 #endif
81                 [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
82                 public Win32Exception (string message, Exception innerException)
83                         : base (message, innerException)
84                 {
85                         native_error_code = Marshal.GetLastWin32Error ();
86                 }
87 #endif
88                 protected Win32Exception(SerializationInfo info,
89                                          StreamingContext context)
90                         : base (info, context) {
91
92                         native_error_code = info.GetInt32 ("NativeErrorCode");
93                 }
94
95                 public int NativeErrorCode {
96                         get {
97                                 return(native_error_code);
98                         }
99                 }
100
101                 [SecurityPermission (SecurityAction.Demand, SerializationFormatter = true)]
102                 public override void GetObjectData(SerializationInfo info, StreamingContext context)
103                 {
104                         if (info == null)
105                                 throw new ArgumentNullException ("info");
106
107                         info.AddValue ("NativeErrorCode", native_error_code);
108                         base.GetObjectData (info, context);
109                 }
110
111                 static string W32ErrorMessage (int error_code)
112                 {
113                         string message;
114
115                         switch (error_code) {
116                         case 2:
117                                 message = Locale.GetText ("Cannot find the specified file");
118                                 break;
119                         case 3:
120                                 message = Locale.GetText ("Cannot find the specified file");
121                                 break;
122                         case 50:
123                                 message = Locale.GetText ("Operation not supported");
124                                 break;
125                         case 267:
126                                 message = Locale.GetText ("Is a directory");
127                                 break;
128                         case 10004:
129                                 message = Locale.GetText ("interrupted");
130                                 break;
131                         case 10013:
132                                 message = Locale.GetText ("Access denied");
133                                 break;
134                         case 10022:
135                                 message = Locale.GetText ("Invalid arguments");
136                                 break;
137                         case 10024:
138                                 message = Locale.GetText ("Too many open files");
139                                 break;
140                         case 10035:
141                                 message = Locale.GetText ("Operation on non-blocking socket would block");
142                                 break;
143                         case 10036:
144                                 message = Locale.GetText ("Operation in progress");
145                                 break;
146                         case 10038:
147                                 message = Locale.GetText ("The descriptor is not a socket");
148                                 break;
149                         case 10040:
150                                 message = Locale.GetText ("Message too long");
151                                 break;
152                         case 10042:
153                                 message = Locale.GetText ("Protocol option not supported");
154                                 break;
155                         case 10043:
156                                 message = Locale.GetText ("Protocol not supported");
157                                 break;
158                         case 10044:
159                                 message = Locale.GetText ("Socket not supported");
160                                 break;
161                         case 10045:
162                                 message = Locale.GetText ("Operation not supported");
163                                 break;
164                         case 10047:
165                                 message = Locale.GetText ("An address incompatible with the requested protocol was used");
166                                 break;
167                         case 10048:
168                                 message = Locale.GetText ("Address already in use");
169                                 break;
170                         case 10049:
171                                 message = Locale.GetText ("The requested address is not valid in this context");
172                                 break;
173                         case 10050:
174                                 message = Locale.GetText ("Network subsystem is down");
175                                 break;
176                         case 10051:
177                                 message = Locale.GetText ("Network is unreachable");
178                                 break;
179                         case 10052:
180                                 message = Locale.GetText ("Connection broken, keep-alive detected a problem");
181                                 break;
182                         case 10053:
183                                 message = Locale.GetText ("An established connection was aborted in your host machine.");
184                                 break;
185                         case 10054:
186                                 message = Locale.GetText ("Connection reset by peer");
187                                 break;
188                         case 10055:
189                                 message = Locale.GetText ("Not enough buffer space is available");
190                                 break;
191                         case 10056:
192                                 message = Locale.GetText ("Socket is already connected");
193                                 break;
194                         case 10057:
195                                 message = Locale.GetText ("The socket is not connected");
196                                 break;
197                         case 10058:
198                                 message = Locale.GetText ("The socket has been shut down");
199                                 break;
200                         case 10060:
201                                 message = Locale.GetText ("Connection timed out");
202                                 break;
203                         case 10061:
204                                 message = Locale.GetText ("Connection refused");
205                                 break;
206                         case 10065:
207                                 message = Locale.GetText ("No route to host");
208                                 break;
209                         case 10093:
210                                 message = Locale.GetText ("Winsock not initialized");
211                                 break;
212                         case 10107:
213                                 message = Locale.GetText ("System call failed");
214                                 break;
215                         case 11001:
216                                 message = Locale.GetText ("No such host is known");
217                                 break;
218                         case 11002:
219                                 message = Locale.GetText ("A temporary error occurred on an " +
220                                                           "authoritative name server. Try  again later.");
221                                 break;
222                         default:
223                                 message = Locale.GetText ("Some sort of w32 error occurred: ") + error_code;
224                                 break;
225                         }
226
227                         return message;
228                 }
229         }
230 }