~ifolder-dev/simias/trunk-packaging

« back to all changes in this revision

Viewing changes to src/core/Simias.log4net/src/Util/.svn/text-base/NativeError.cs.svn-base

  • Committer: Jorge O. Castro
  • Date: 2007-12-03 06:56:46 UTC
  • Revision ID: jorge@ubuntu.com-20071203065646-mupcnjcwgm5mnhyt
* Remove a bunch of .svn directories we no longer need.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#region Copyright & License
2
 
//
3
 
// Copyright 2001-2005 The Apache Software Foundation
4
 
//
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
8
 
//
9
 
// http://www.apache.org/licenses/LICENSE-2.0
10
 
//
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.
16
 
//
17
 
#endregion
18
 
 
19
 
// MONO 1.0 has no support for Win32 Error APIs
20
 
#if !MONO
21
 
// SSCLI 1.0 has no support for Win32 Error APIs
22
 
#if !SSCLI
23
 
// We don't want framework or platform specific code in the CLI version of log4net
24
 
#if !CLI_1_0
25
 
 
26
 
using System;
27
 
using System.Globalization;
28
 
using System.Runtime.InteropServices;
29
 
 
30
 
namespace log4net.Util 
31
 
{
32
 
        /// <summary>
33
 
        /// Represents a native error code and message.
34
 
        /// </summary>
35
 
        /// <remarks>
36
 
        /// <para>
37
 
        /// Represents a Win32 platform native error.
38
 
        /// </para>
39
 
        /// </remarks>
40
 
        /// <author>Nicko Cadell</author>
41
 
        /// <author>Gert Driesen</author>
42
 
        public sealed class NativeError 
43
 
        {
44
 
                #region Protected Instance Constructors
45
 
 
46
 
                /// <summary>
47
 
                /// Create an instance of the <see cref="NativeError" /> class with the specified 
48
 
                /// error number and message.
49
 
                /// </summary>
50
 
                /// <param name="number">The number of the native error.</param>
51
 
                /// <param name="message">The message of the native error.</param>
52
 
                /// <remarks>
53
 
                /// <para>
54
 
                /// Create an instance of the <see cref="NativeError" /> class with the specified 
55
 
                /// error number and message.
56
 
                /// </para>
57
 
                /// </remarks>
58
 
                private NativeError(int number, string message) 
59
 
                {
60
 
                        m_number = number;
61
 
                        m_message = message;
62
 
                }
63
 
 
64
 
                #endregion // Protected Instance Constructors
65
 
 
66
 
                #region Public Instance Properties
67
 
 
68
 
                /// <summary>
69
 
                /// Gets the number of the native error.
70
 
                /// </summary>
71
 
                /// <value>
72
 
                /// The number of the native error.
73
 
                /// </value>
74
 
                /// <remarks>
75
 
                /// <para>
76
 
                /// Gets the number of the native error.
77
 
                /// </para>
78
 
                /// </remarks>
79
 
                public int Number 
80
 
                {
81
 
                        get { return m_number; }
82
 
                }
83
 
 
84
 
                /// <summary>
85
 
                /// Gets the message of the native error.
86
 
                /// </summary>
87
 
                /// <value>
88
 
                /// The message of the native error.
89
 
                /// </value>
90
 
                /// <remarks>
91
 
                /// <para>
92
 
                /// </para>
93
 
                /// Gets the message of the native error.
94
 
                /// </remarks>
95
 
                public string Message 
96
 
                {
97
 
                        get { return m_message; }
98
 
                }
99
 
 
100
 
                #endregion // Public Instance Properties
101
 
 
102
 
                #region Public Static Methods
103
 
 
104
 
                /// <summary>
105
 
                /// Create a new instance of the <see cref="NativeError" /> class for the last Windows error.
106
 
                /// </summary>
107
 
                /// <returns>
108
 
                /// An instance of the <see cref="NativeError" /> class for the last windows error.
109
 
                /// </returns>
110
 
                /// <remarks>
111
 
                /// <para>
112
 
                /// The message for the <see cref="Marshal.GetLastWin32Error"/> error number is lookup up using the 
113
 
                /// native Win32 <c>FormatMessage</c> function.
114
 
                /// </para>
115
 
                /// </remarks>
116
 
                public static NativeError GetLastError() 
117
 
                {
118
 
                        int number = Marshal.GetLastWin32Error();
119
 
                        return new NativeError(number, NativeError.GetErrorMessage(number));
120
 
                }
121
 
 
122
 
                /// <summary>
123
 
                /// Create a new instance of the <see cref="NativeError" /> class.
124
 
                /// </summary>
125
 
                /// <param name="number">the error number for the native error</param>
126
 
                /// <returns>
127
 
                /// An instance of the <see cref="NativeError" /> class for the specified 
128
 
                /// error number.
129
 
                /// </returns>
130
 
                /// <remarks>
131
 
                /// <para>
132
 
                /// The message for the specified error number is lookup up using the 
133
 
                /// native Win32 <c>FormatMessage</c> function.
134
 
                /// </para>
135
 
                /// </remarks>
136
 
                public static NativeError GetError(int number) 
137
 
                {
138
 
                        return new NativeError(number, NativeError.GetErrorMessage(number));
139
 
                }
140
 
 
141
 
                /// <summary>
142
 
                /// Retrieves the message corresponding with a Win32 message identifier.
143
 
                /// </summary>
144
 
                /// <param name="messageId">Message identifier for the requested message.</param>
145
 
                /// <returns>
146
 
                /// The message corresponding with the specified message identifier.
147
 
                /// </returns>
148
 
                /// <remarks>
149
 
                /// <para>
150
 
                /// The message will be searched for in system message-table resource(s)
151
 
                /// using the native <c>FormatMessage</c> function.
152
 
                /// </para>
153
 
                /// </remarks>
154
 
                public static string GetErrorMessage(int messageId) 
155
 
                {
156
 
                        // Win32 constants
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
160
 
 
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
164
 
 
165
 
                        if (messageId != 0) 
166
 
                        {
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, 
170
 
                                        ref sourcePtr, 
171
 
                                        messageId, 
172
 
                                        0, 
173
 
                                        ref msgBuf, 
174
 
                                        255, 
175
 
                                        argumentsPtr);
176
 
 
177
 
                                if (messageSize > 0) 
178
 
                                {
179
 
                                        // Remove trailing null-terminating characters (\r\n) from the message
180
 
                                        msgBuf = msgBuf.TrimEnd(new char[] {'\r', '\n'});
181
 
                                }
182
 
                                else 
183
 
                                {
184
 
                                        // A message could not be located.
185
 
                                        msgBuf = null;
186
 
                                }
187
 
                        } 
188
 
                        else 
189
 
                        {
190
 
                                msgBuf = null;
191
 
                        }
192
 
 
193
 
                        return msgBuf;
194
 
                }
195
 
 
196
 
                #endregion // Public Static Methods
197
 
 
198
 
                #region Override Object Implementation
199
 
 
200
 
                /// <summary>
201
 
                /// Return error information string
202
 
                /// </summary>
203
 
                /// <returns>error information string</returns>
204
 
                /// <remarks>
205
 
                /// <para>
206
 
                /// Return error information string
207
 
                /// </para>
208
 
                /// </remarks>
209
 
                public override string ToString() 
210
 
                {
211
 
                        return string.Format(CultureInfo.InvariantCulture, "0x{0:x8}", this.Number) + (this.Message != null ? ": " + this.Message : "");
212
 
                }
213
 
 
214
 
                #endregion // Override Object Implementation
215
 
 
216
 
                #region Stubs For Native Function Calls
217
 
 
218
 
                /// <summary>
219
 
                /// Formats a message string.
220
 
                /// </summary>
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>
228
 
                /// <remarks>
229
 
                /// <para>
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.
237
 
                /// </para>
238
 
                /// <para>
239
 
                /// To prevent the usage of unsafe code, this stub does not support inserting values in the formatted message.
240
 
                /// </para>
241
 
                /// </remarks>
242
 
                /// <returns>
243
 
                /// <para>
244
 
                /// If the function succeeds, the return value is the number of TCHARs stored in the output 
245
 
                /// buffer, excluding the terminating null character.
246
 
                /// </para>
247
 
                /// <para>
248
 
                /// If the function fails, the return value is zero. To get extended error information, 
249
 
                /// call <see cref="Marshal.GetLastWin32Error()" />.
250
 
                /// </para>
251
 
                /// </returns>
252
 
#if NETCF
253
 
                [DllImport("CoreDll.dll", SetLastError=true, CharSet=CharSet.Unicode)]
254
 
#else
255
 
                [DllImport("Kernel32.dll", SetLastError=true, CharSet=CharSet.Auto)]
256
 
#endif
257
 
                private static extern int FormatMessage(
258
 
                        int dwFlags, 
259
 
                        ref IntPtr lpSource, 
260
 
                        int dwMessageId,
261
 
                        int dwLanguageId, 
262
 
                        ref String lpBuffer, 
263
 
                        int nSize,
264
 
                        IntPtr Arguments);
265
 
 
266
 
                #endregion // Stubs For Native Function Calls
267
 
 
268
 
                #region Private Instance Fields
269
 
 
270
 
                private int m_number;
271
 
                private string m_message;
272
 
 
273
 
                #endregion
274
 
        }
275
 
}
276
 
 
277
 
#endif // !CLI_1_0
278
 
#endif // !SSCLI
279
 
#endif // !MONO