mscorlib(4.0.0.0) API with additions
Win32Exception.cs
1 using Microsoft.Win32;
4 using System.Security;
6 using System.Text;
7 
8 namespace System.ComponentModel
9 {
11  [Serializable]
12  [SuppressUnmanagedCodeSecurity]
13  [HostProtection(SecurityAction.LinkDemand, SharedState = true)]
15  {
16  private readonly int nativeErrorCode;
17 
18  private const int MaxAllowedBufferSize = 66560;
19 
22  public int NativeErrorCode => nativeErrorCode;
23 
25  [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
26  public Win32Exception()
27  : this(Marshal.GetLastWin32Error())
28  {
29  }
30 
33  [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
34  public Win32Exception(int error)
35  : this(error, GetErrorMessage(error))
36  {
37  }
38 
42  [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
43  public Win32Exception(int error, string message)
44  : base(message)
45  {
46  nativeErrorCode = error;
47  }
48 
51  [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
52  public Win32Exception(string message)
53  : this(Marshal.GetLastWin32Error(), message)
54  {
55  }
56 
60  [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
61  public Win32Exception(string message, Exception innerException)
62  : base(message, innerException)
63  {
64  nativeErrorCode = Marshal.GetLastWin32Error();
65  }
66 
71  : base(info, context)
72  {
73  IntSecurity.UnmanagedCode.Demand();
74  nativeErrorCode = info.GetInt32("NativeErrorCode");
75  }
76 
77  private static bool TryGetErrorMessage(int error, StringBuilder sb, out string errorMsg)
78  {
79  errorMsg = "";
80  if (Microsoft.Win32.SafeNativeMethods.FormatMessage(12800, IntPtr.Zero, (uint)error, 0, sb, sb.Capacity + 1, null) != 0)
81  {
82  int num;
83  for (num = sb.Length; num > 0; num--)
84  {
85  char c = sb[num - 1];
86  if (c > ' ' && c != '.')
87  {
88  break;
89  }
90  }
91  errorMsg = sb.ToString(0, num);
92  }
93  else
94  {
95  if (Marshal.GetLastWin32Error() == 122)
96  {
97  return false;
98  }
99  errorMsg = "Unknown error (0x" + Convert.ToString(error, 16) + ")";
100  }
101  return true;
102  }
103 
104  private static string GetErrorMessage(int error)
105  {
106  StringBuilder stringBuilder = new StringBuilder(256);
107  do
108  {
109  if (TryGetErrorMessage(error, stringBuilder, out string errorMsg))
110  {
111  return errorMsg;
112  }
113  stringBuilder.Capacity *= 4;
114  }
115  while (stringBuilder.Capacity < 66560);
116  return "Unknown error (0x" + Convert.ToString(error, 16) + ")";
117  }
118 
124  [SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
125  public override void GetObjectData(SerializationInfo info, StreamingContext context)
126  {
127  if (info == null)
128  {
129  throw new ArgumentNullException("info");
130  }
131  info.AddValue("NativeErrorCode", nativeErrorCode);
132  base.GetObjectData(info, context);
133  }
134  }
135 }
Win32Exception(string message)
Initializes a new instance of the T:System.ComponentModel.Win32Exception class with the specified det...
Win32Exception(SerializationInfo info, StreamingContext context)
Initializes a new instance of the T:System.ComponentModel.Win32Exception class with the specified con...
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
Describes a set of security permissions applied to code. This class cannot be inherited.
Win32Exception()
Initializes a new instance of the T:System.ComponentModel.Win32Exception class with the last Win32 er...
unsafe override string ToString()
Converts the value of this instance to a T:System.String.
Definition: __Canon.cs:3
Describes the source and destination of a given serialized stream, and provides an additional caller-...
SecurityAction
Specifies the security actions that can be performed using declarative security.
A cast or conversion operation, such as (SampleType)obj in C::or CType(obj, SampleType) in Visual Bas...
Throws an exception for a Win32 error code.
int Length
Gets or sets the length of the current T:System.Text.StringBuilder object.
A platform-specific type that is used to represent a pointer or a handle.
Definition: IntPtr.cs:14
Provides a collection of methods for allocating unmanaged memory, copying unmanaged memory blocks,...
Definition: Marshal.cs:15
Win32Exception(int error, string message)
Initializes a new instance of the T:System.ComponentModel.Win32Exception class with the specified err...
Stores all the data needed to serialize or deserialize an object. This class cannot be inherited.
Represents a mutable string of characters. This class cannot be inherited.To browse the ....
Win32Exception(int error)
Initializes a new instance of the T:System.ComponentModel.Win32Exception class with the specified err...
Win32Exception(string message, Exception innerException)
Initializes a new instance of the T:System.ComponentModel.Win32Exception class with the specified det...
The base exception type for all COM interop exceptions and structured exception handling (SEH) except...
Allows an object to control its own serialization and deserialization.
Definition: ISerializable.cs:8
int Capacity
Gets or sets the maximum number of characters that can be contained in the memory allocated by the cu...
Represents errors that occur during application execution.To browse the .NET Framework source code fo...
Definition: Exception.cs:22
static readonly IntPtr Zero
A read-only field that represents a pointer or handle that has been initialized to zero.
Definition: IntPtr.cs:20
override void GetObjectData(SerializationInfo info, StreamingContext context)
Sets the T:System.Runtime.Serialization.SerializationInfo object with the file name and line number a...
Specifies that the class can be serialized.
static int GetLastWin32Error()
Returns the error code returned by the last unmanaged function that was called using platform invoke ...
int NativeErrorCode
Gets the Win32 error code associated with this exception.
SecurityPermissionFlag
Specifies access flags for the security permission object.