1 //------------------------------------------------------------------------------
2 // <copyright file="Win32Exception.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
5 //------------------------------------------------------------------------------
10 namespace System.ComponentModel {
11 using Microsoft.Win32;
13 using System.Diagnostics;
14 using System.Runtime.InteropServices;
15 using System.Runtime.Remoting;
16 using System.Runtime.Serialization;
17 using System.Security;
18 using System.Security.Permissions;
22 /// <para>The exception that is thrown for a Win32 error code.</para>
24 // Code already shipped - safe to place link demand on derived class constructor when base doesn't have it - Suppress message.
25 [HostProtection(SharedState = true)]
26 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
28 [SuppressUnmanagedCodeSecurity]
29 public partial class Win32Exception : ExternalException, ISerializable {
31 /// <para>Represents the Win32 error code associated with this exception. This
32 /// field is read-only.</para>
34 private readonly int nativeErrorCode;
37 /// <para>Initializes a new instance of the <see cref='System.ComponentModel.Win32Exception'/> class with the last Win32 error
38 /// that occured.</para>
40 [SecurityPermission(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.UnmanagedCode)]
41 public Win32Exception() : this(Marshal.GetLastWin32Error()) {
44 /// <para>Initializes a new instance of the <see cref='System.ComponentModel.Win32Exception'/> class with the specified error.</para>
46 [SecurityPermission(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.UnmanagedCode)]
47 public Win32Exception(int error) : this(error, GetErrorMessage(error)) {
50 /// <para>Initializes a new instance of the <see cref='System.ComponentModel.Win32Exception'/> class with the specified error and the
51 /// specified detailed description.</para>
53 [SecurityPermission(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.UnmanagedCode)]
54 public Win32Exception(int error, string message)
56 nativeErrorCode = error;
60 /// Initializes a new instance of the Exception class with a specified error message.
61 /// FxCop CA1032: Multiple constructors are required to correctly implement a custom exception.
63 [SecurityPermission(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.UnmanagedCode)]
64 public Win32Exception( string message ) : this(Marshal.GetLastWin32Error(), message) {
68 /// Initializes a new instance of the Exception class with a specified error message and a
69 /// reference to the inner exception that is the cause of this exception.
70 /// FxCop CA1032: Multiple constructors are required to correctly implement a custom exception.
72 [SecurityPermission(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.UnmanagedCode)]
73 public Win32Exception( string message, Exception innerException ) : base(message, innerException) {
74 nativeErrorCode = Marshal.GetLastWin32Error();
77 protected Win32Exception(SerializationInfo info, StreamingContext context) : base (info, context) {
79 IntSecurity.UnmanagedCode.Demand();
81 nativeErrorCode = info.GetInt32("NativeErrorCode");
85 /// <para>Represents the Win32 error code associated with this exception. This
86 /// field is read-only.</para>
88 public int NativeErrorCode {
90 return nativeErrorCode;
95 private static bool TryGetErrorMessage(int error, StringBuilder sb, out string errorMsg)
98 int result = SafeNativeMethods.FormatMessage(
99 SafeNativeMethods.FORMAT_MESSAGE_IGNORE_INSERTS |
100 SafeNativeMethods.FORMAT_MESSAGE_FROM_SYSTEM |
101 SafeNativeMethods.FORMAT_MESSAGE_ARGUMENT_ARRAY,
102 IntPtr.Zero, (uint) error, 0, sb, sb.Capacity + 1,
108 if (ch > 32 && ch != '.') break;
111 errorMsg = sb.ToString(0, i);
113 else if (Marshal.GetLastWin32Error() == SafeNativeMethods.ERROR_INSUFFICIENT_BUFFER) {
117 errorMsg ="Unknown error (0x" + Convert.ToString(error, 16) + ")";
123 // Windows API FormatMessage lets you format a message string given an errocode.
124 // Unlike other APIs this API does not support a way to query it for the total message size.
126 // So the API can only be used in one of these two ways.
127 // a. You pass a buffer of appropriate size and get the resource.
128 // b. Windows creates a buffer and passes the address back and the onus of releasing the bugffer lies on the caller.
130 // Since the error code is coming from the user, it is not possible to know the size in advance.
131 // Unfortunately we can't use option b. since the buffer can only be freed using LocalFree and it is a private API on onecore.
132 // Also, using option b is ugly for the manged code and could cause memory leak in situations where freeing is unsuccessful.
134 // As a result we use the following approach.
135 // We initially call the API with a buffer size of 256 and then gradually increase the size in case of failure until we reach the max allowed size of 65K bytes.
137 private const int MaxAllowedBufferSize = 65 * 1024;
139 private static string GetErrorMessage(int error) {
142 StringBuilder sb = new StringBuilder(256);
144 if (TryGetErrorMessage(error, sb, out errorMsg))
147 // increase the capacity of the StringBuilder by 4 times.
151 while (sb.Capacity < MaxAllowedBufferSize);
153 // If you come here then a size as large as 65K is also not sufficient and so we give the generic errorMsg.
154 return "Unknown error (0x" + Convert.ToString(error, 16) + ")";
157 // Even though all we're exposing is the nativeErrorCode (which is also available via public property)
158 // it's not a bad idea to have this in place. Later, if more fields are added to this exception,
159 // we won't need to worry about accidentaly exposing them through this interface.
160 [SecurityPermissionAttribute(SecurityAction.Demand,SerializationFormatter=true)]
161 public override void GetObjectData(SerializationInfo info, StreamingContext context) {
163 throw new ArgumentNullException("info");
165 info.AddValue("NativeErrorCode", nativeErrorCode);
166 base.GetObjectData(info, context);