Merge pull request #4248 from Unity-Technologies/boehm-gc-alloc-fixed
[mono.git] / mcs / class / referencesource / System / compmod / system / componentmodel / Win32Exception.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="Win32Exception.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>                                                                
5 //------------------------------------------------------------------------------
6
7 /*
8  */
9
10 namespace System.ComponentModel {
11     using Microsoft.Win32;
12     using System;
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;
19     using System.Text;
20
21     /// <devdoc>
22     ///    <para>The exception that is thrown for a Win32 error code.</para>
23     /// </devdoc>
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")]
27     [Serializable]
28     [SuppressUnmanagedCodeSecurity]
29     public partial class Win32Exception : ExternalException, ISerializable {
30         /// <devdoc>
31         ///    <para>Represents the Win32 error code associated with this exception. This 
32         ///       field is read-only.</para>
33         /// </devdoc>
34         private readonly int nativeErrorCode;
35
36         /// <devdoc>
37         /// <para>Initializes a new instance of the <see cref='System.ComponentModel.Win32Exception'/> class with the last Win32 error 
38         ///    that occured.</para>
39         /// </devdoc>
40         [SecurityPermission(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.UnmanagedCode)]
41         public Win32Exception() : this(Marshal.GetLastWin32Error()) {
42         }
43         /// <devdoc>
44         /// <para>Initializes a new instance of the <see cref='System.ComponentModel.Win32Exception'/> class with the specified error.</para>
45         /// </devdoc>
46         [SecurityPermission(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.UnmanagedCode)]
47         public Win32Exception(int error) : this(error, GetErrorMessage(error)) {
48         }
49         /// <devdoc>
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>
52         /// </devdoc>
53         [SecurityPermission(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.UnmanagedCode)]
54         public Win32Exception(int error, string message)
55         : base(message) {
56             nativeErrorCode = error;
57         }
58
59         /// <devdoc>
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.
62         /// </devdoc>
63         [SecurityPermission(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.UnmanagedCode)]
64         public Win32Exception( string message ) : this(Marshal.GetLastWin32Error(), message) {
65         }
66
67         /// <devdoc>
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.
71         /// </devdoc>
72         [SecurityPermission(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.UnmanagedCode)]
73         public Win32Exception( string message, Exception innerException ) : base(message, innerException) {
74             nativeErrorCode = Marshal.GetLastWin32Error();
75         }
76
77         protected Win32Exception(SerializationInfo info, StreamingContext context) : base (info, context) {
78 #if MONO_FEATURE_CAS
79             IntSecurity.UnmanagedCode.Demand();
80 #endif
81             nativeErrorCode = info.GetInt32("NativeErrorCode");
82         }
83
84         /// <devdoc>
85         ///    <para>Represents the Win32 error code associated with this exception. This 
86         ///       field is read-only.</para>
87         /// </devdoc>
88         public int NativeErrorCode {
89             get {
90                 return nativeErrorCode;
91             }
92         }
93
94 #if !MONO
95         private static bool TryGetErrorMessage(int error, StringBuilder sb, out string errorMsg)
96         {
97             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,
103                                         null);
104             if (result != 0) {
105                 int i = sb.Length;
106                 while (i > 0) {
107                     char ch = sb[i - 1];
108                     if (ch > 32 && ch != '.') break;
109                     i--;
110                 }
111                 errorMsg = sb.ToString(0, i);
112             }
113             else if (Marshal.GetLastWin32Error() == SafeNativeMethods.ERROR_INSUFFICIENT_BUFFER) {
114                 return false;
115             }
116             else {
117                 errorMsg ="Unknown error (0x" + Convert.ToString(error, 16) + ")";
118             }
119
120             return true;
121         }
122
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.
125         //
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.
129         //
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.
133         // 
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.
136
137         private const int MaxAllowedBufferSize = 65 * 1024;
138
139         private static string GetErrorMessage(int error) {
140             string errorMsg;
141
142             StringBuilder sb = new StringBuilder(256);
143             do {
144                 if (TryGetErrorMessage(error, sb, out errorMsg))
145                     return errorMsg;
146                 else {
147                     // increase the capacity of the StringBuilder by 4 times.
148                     sb.Capacity *= 4;
149                 }
150             }
151             while (sb.Capacity < MaxAllowedBufferSize);
152
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) + ")";
155         }
156 #endif
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) {
162             if (info==null) {
163                 throw new ArgumentNullException("info");
164             }
165             info.AddValue("NativeErrorCode", nativeErrorCode);
166             base.GetObjectData(info, context);
167         }
168     }
169 }