Repository navigation
Expand file tree
/
Copy pathWin32Exception.cs
More file actions
124 lines (109 loc) · 4.44 KB
/
Copy pathWin32Exception.cs
File metadata and controls
124 lines (109 loc) · 4.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Globalization;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Text;
namespace System.ComponentModel
{
/// <summary>
/// The exception that is thrown for a Win32 error code.
/// </summary>
[Serializable]
[System.Runtime.CompilerServices.TypeForwardedFrom("System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
public class Win32Exception : ExternalException, ISerializable
{
private const int E_FAIL = unchecked((int)0x80004005);
/// <summary>
/// Initializes a new instance of the <see cref='System.ComponentModel.Win32Exception'/> class with the last Win32 error
/// that occurred.
/// </summary>
public Win32Exception() : this(Marshal.GetLastPInvokeError())
{
}
/// <summary>
/// Initializes a new instance of the <see cref='System.ComponentModel.Win32Exception'/> class with the specified error.
/// </summary>
public Win32Exception(int error) : this(error, Marshal.GetPInvokeErrorMessage(error))
{
}
/// <summary>
/// Initializes a new instance of the <see cref='System.ComponentModel.Win32Exception'/> class with the specified error and the
/// specified detailed description.
/// </summary>
public Win32Exception(int error, string? message) : base(message)
{
NativeErrorCode = error;
}
/// <summary>
/// Initializes a new instance of the Exception class with a specified error message.
/// </summary>
public Win32Exception(string? message) : this(Marshal.GetLastPInvokeError(), message)
{
}
/// <summary>
/// Initializes a new instance of the Exception class with a specified error message and a
/// reference to the inner exception that is the cause of this exception.
/// </summary>
public Win32Exception(string? message, Exception? innerException) : base(message, innerException)
{
NativeErrorCode = Marshal.GetLastPInvokeError();
}
protected Win32Exception(SerializationInfo info, StreamingContext context) : base(info, context)
{
NativeErrorCode = info.GetInt32(nameof(NativeErrorCode));
}
public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);
info.AddValue(nameof(NativeErrorCode), NativeErrorCode);
}
/// <summary>
/// Represents the Win32 error code associated with this exception. This field is read-only.
/// </summary>
public int NativeErrorCode { get; }
/// <summary>
/// Returns a string that contains the <see cref="NativeErrorCode"/>, or <see cref="Exception.HResult"/>, or both.
/// </summary>
/// <returns>A string that represents the <see cref="NativeErrorCode"/>, or <see cref="Exception.HResult"/>, or both.</returns>
public override string ToString()
{
if (NativeErrorCode == 0 || NativeErrorCode == HResult)
{
return base.ToString();
}
string message = Message;
string className = GetType().ToString();
StringBuilder s = new StringBuilder(className);
string nativeErrorString = NativeErrorCode < 0
? $"0x{NativeErrorCode:X8}"
: NativeErrorCode.ToString(CultureInfo.InvariantCulture);
if (HResult == E_FAIL)
{
s.Append($" ({nativeErrorString})");
}
else
{
s.Append($" ({HResult:X8}, {nativeErrorString})");
}
if (!(string.IsNullOrEmpty(message)))
{
s.Append(": ");
s.Append(message);
}
Exception? innerException = InnerException;
if (innerException != null)
{
s.Append(" ---> ");
s.Append(innerException.ToString());
}
string? stackTrace = StackTrace;
if (stackTrace != null)
{
s.AppendLine();
s.Append(stackTrace);
}
return s.ToString();
}
}
}