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