1
#region Copyright & License
3
// Copyright 2001-2005 The Apache Software Foundation
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
9
// http://www.apache.org/licenses/LICENSE-2.0
11
// Unless required by applicable law or agreed to in writing, software
12
// distributed under the License is distributed on an "AS IS" BASIS,
13
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
// See the License for the specific language governing permissions and
15
// limitations under the License.
19
// MONO 1.0 has no support for Win32 Error APIs
21
// SSCLI 1.0 has no support for Win32 Error APIs
23
// We don't want framework or platform specific code in the CLI version of log4net
27
using System.Globalization;
28
using System.Runtime.InteropServices;
30
namespace log4net.Util
33
/// Represents a native error code and message.
37
/// Represents a Win32 platform native error.
40
/// <author>Nicko Cadell</author>
41
/// <author>Gert Driesen</author>
42
public sealed class NativeError
44
#region Protected Instance Constructors
47
/// Create an instance of the <see cref="NativeError" /> class with the specified
48
/// error number and message.
50
/// <param name="number">The number of the native error.</param>
51
/// <param name="message">The message of the native error.</param>
54
/// Create an instance of the <see cref="NativeError" /> class with the specified
55
/// error number and message.
58
private NativeError(int number, string message)
64
#endregion // Protected Instance Constructors
66
#region Public Instance Properties
69
/// Gets the number of the native error.
72
/// The number of the native error.
76
/// Gets the number of the native error.
81
get { return m_number; }
85
/// Gets the message of the native error.
88
/// The message of the native error.
93
/// Gets the message of the native error.
97
get { return m_message; }
100
#endregion // Public Instance Properties
102
#region Public Static Methods
105
/// Create a new instance of the <see cref="NativeError" /> class for the last Windows error.
108
/// An instance of the <see cref="NativeError" /> class for the last windows error.
112
/// The message for the <see cref="Marshal.GetLastWin32Error"/> error number is lookup up using the
113
/// native Win32 <c>FormatMessage</c> function.
116
public static NativeError GetLastError()
118
int number = Marshal.GetLastWin32Error();
119
return new NativeError(number, NativeError.GetErrorMessage(number));
123
/// Create a new instance of the <see cref="NativeError" /> class.
125
/// <param name="number">the error number for the native error</param>
127
/// An instance of the <see cref="NativeError" /> class for the specified
132
/// The message for the specified error number is lookup up using the
133
/// native Win32 <c>FormatMessage</c> function.
136
public static NativeError GetError(int number)
138
return new NativeError(number, NativeError.GetErrorMessage(number));
142
/// Retrieves the message corresponding with a Win32 message identifier.
144
/// <param name="messageId">Message identifier for the requested message.</param>
146
/// The message corresponding with the specified message identifier.
150
/// The message will be searched for in system message-table resource(s)
151
/// using the native <c>FormatMessage</c> function.
154
public static string GetErrorMessage(int messageId)
157
int FORMAT_MESSAGE_ALLOCATE_BUFFER = 0x00000100; // The function should allocates a buffer large enough to hold the formatted message
158
int FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200; // Insert sequences in the message definition are to be ignored
159
int FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000; // The function should search the system message-table resource(s) for the requested message
161
string msgBuf = ""; // buffer that will receive the message
162
IntPtr sourcePtr = new IntPtr(); // Location of the message definition, will be ignored
163
IntPtr argumentsPtr = new IntPtr(); // Pointer to array of values to insert, not supported as it requires unsafe code
167
// If the function succeeds, the return value is the number of TCHARs stored in the output buffer, excluding the terminating null character
168
int messageSize = FormatMessage(
169
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
179
// Remove trailing null-terminating characters (\r\n) from the message
180
msgBuf = msgBuf.TrimEnd(new char[] {'\r', '\n'});
184
// A message could not be located.
196
#endregion // Public Static Methods
198
#region Override Object Implementation
201
/// Return error information string
203
/// <returns>error information string</returns>
206
/// Return error information string
209
public override string ToString()
211
return string.Format(CultureInfo.InvariantCulture, "0x{0:x8}", this.Number) + (this.Message != null ? ": " + this.Message : "");
214
#endregion // Override Object Implementation
216
#region Stubs For Native Function Calls
219
/// Formats a message string.
221
/// <param name="dwFlags">Formatting options, and how to interpret the <paramref name="lpSource" /> parameter.</param>
222
/// <param name="lpSource">Location of the message definition.</param>
223
/// <param name="dwMessageId">Message identifier for the requested message.</param>
224
/// <param name="dwLanguageId">Language identifier for the requested message.</param>
225
/// <param name="lpBuffer">If <paramref name="dwFlags" /> includes FORMAT_MESSAGE_ALLOCATE_BUFFER, the function allocates a buffer using the <c>LocalAlloc</c> function, and places the pointer to the buffer at the address specified in <paramref name="lpBuffer" />.</param>
226
/// <param name="nSize">If the FORMAT_MESSAGE_ALLOCATE_BUFFER flag is not set, this parameter specifies the maximum number of TCHARs that can be stored in the output buffer. If FORMAT_MESSAGE_ALLOCATE_BUFFER is set, this parameter specifies the minimum number of TCHARs to allocate for an output buffer.</param>
227
/// <param name="Arguments">Pointer to an array of values that are used as insert values in the formatted message.</param>
230
/// The function requires a message definition as input. The message definition can come from a
231
/// buffer passed into the function. It can come from a message table resource in an
232
/// already-loaded module. Or the caller can ask the function to search the system's message
233
/// table resource(s) for the message definition. The function finds the message definition
234
/// in a message table resource based on a message identifier and a language identifier.
235
/// The function copies the formatted message text to an output buffer, processing any embedded
236
/// insert sequences if requested.
239
/// To prevent the usage of unsafe code, this stub does not support inserting values in the formatted message.
244
/// If the function succeeds, the return value is the number of TCHARs stored in the output
245
/// buffer, excluding the terminating null character.
248
/// If the function fails, the return value is zero. To get extended error information,
249
/// call <see cref="Marshal.GetLastWin32Error()" />.
253
[DllImport("CoreDll.dll", SetLastError=true, CharSet=CharSet.Unicode)]
255
[DllImport("Kernel32.dll", SetLastError=true, CharSet=CharSet.Auto)]
257
private static extern int FormatMessage(
266
#endregion // Stubs For Native Function Calls
268
#region Private Instance Fields
270
private int m_number;
271
private string m_message;