Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / CompMod / System / ComponentModel / Win32Exception.cs / 1305376 / Win32Exception.cs
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
/*
*/
namespace System.ComponentModel {
using Microsoft.Win32;
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Runtime.Remoting;
using System.Runtime.Serialization;
using System.Security;
using System.Security.Permissions;
using System.Text;
///
/// The exception that is thrown for a Win32 error code.
///
// Code already shipped - safe to place link demand on derived class constructor when base doesn't have it - Suppress message.
[HostProtection(SharedState = true)]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
[Serializable]
[SuppressUnmanagedCodeSecurity]
public class Win32Exception : ExternalException, ISerializable {
///
/// Represents the Win32 error code associated with this exception. This
/// field is read-only.
///
private readonly int nativeErrorCode;
///
/// Initializes a new instance of the class with the last Win32 error
/// that occured.
///
[SecurityPermission(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.UnmanagedCode)]
public Win32Exception() : this(Marshal.GetLastWin32Error()) {
}
///
/// Initializes a new instance of the class with the specified error.
///
[SecurityPermission(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.UnmanagedCode)]
public Win32Exception(int error) : this(error, GetErrorMessage(error)) {
}
///
/// Initializes a new instance of the class with the specified error and the
/// specified detailed description.
///
[SecurityPermission(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.UnmanagedCode)]
public Win32Exception(int error, string message)
: base(message) {
nativeErrorCode = error;
}
///
/// Initializes a new instance of the Exception class with a specified error message.
/// FxCop CA1032: Multiple constructors are required to correctly implement a custom exception.
///
[SecurityPermission(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.UnmanagedCode)]
public Win32Exception( string message ) : this(Marshal.GetLastWin32Error(), message) {
}
///
/// 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.
/// FxCop CA1032: Multiple constructors are required to correctly implement a custom exception.
///
[SecurityPermission(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.UnmanagedCode)]
public Win32Exception( string message, Exception innerException ) : base(message, innerException) {
nativeErrorCode = Marshal.GetLastWin32Error();
}
protected Win32Exception(SerializationInfo info, StreamingContext context) : base (info, context) {
IntSecurity.UnmanagedCode.Demand();
nativeErrorCode = info.GetInt32("NativeErrorCode");
}
///
/// Represents the Win32 error code associated with this exception. This
/// field is read-only.
///
public int NativeErrorCode {
get {
return nativeErrorCode;
}
}
private static string GetErrorMessage(int error) {
//get the system error message...
string errorMsg = "";
StringBuilder sb = new StringBuilder(256);
int result = SafeNativeMethods.FormatMessage(
SafeNativeMethods.FORMAT_MESSAGE_IGNORE_INSERTS |
SafeNativeMethods.FORMAT_MESSAGE_FROM_SYSTEM |
SafeNativeMethods.FORMAT_MESSAGE_ARGUMENT_ARRAY,
NativeMethods.NullHandleRef, error, 0, sb, sb.Capacity + 1,
IntPtr.Zero);
if (result != 0) {
int i = sb.Length;
while (i > 0) {
char ch = sb[i - 1];
if (ch > 32 && ch != '.') break;
i--;
}
errorMsg = sb.ToString(0, i);
}
else {
errorMsg ="Unknown error (0x" + Convert.ToString(error, 16) + ")";
}
return errorMsg;
}
// Even though all we're exposing is the nativeErrorCode (which is also available via public property)
// it's not a bad idea to have this in place. Later, if more fields are added to this exception,
// we won't need to worry about accidentaly exposing them through this interface.
[SecurityPermissionAttribute(SecurityAction.Demand,SerializationFormatter=true)]
public override void GetObjectData(SerializationInfo info, StreamingContext context) {
if (info==null) {
throw new ArgumentNullException("info");
}
info.AddValue("NativeErrorCode", nativeErrorCode);
base.GetObjectData(info, context);
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
/*
*/
namespace System.ComponentModel {
using Microsoft.Win32;
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Runtime.Remoting;
using System.Runtime.Serialization;
using System.Security;
using System.Security.Permissions;
using System.Text;
///
/// The exception that is thrown for a Win32 error code.
///
// Code already shipped - safe to place link demand on derived class constructor when base doesn't have it - Suppress message.
[HostProtection(SharedState = true)]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
[Serializable]
[SuppressUnmanagedCodeSecurity]
public class Win32Exception : ExternalException, ISerializable {
///
/// Represents the Win32 error code associated with this exception. This
/// field is read-only.
///
private readonly int nativeErrorCode;
///
/// Initializes a new instance of the class with the last Win32 error
/// that occured.
///
[SecurityPermission(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.UnmanagedCode)]
public Win32Exception() : this(Marshal.GetLastWin32Error()) {
}
///
/// Initializes a new instance of the class with the specified error.
///
[SecurityPermission(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.UnmanagedCode)]
public Win32Exception(int error) : this(error, GetErrorMessage(error)) {
}
///
/// Initializes a new instance of the class with the specified error and the
/// specified detailed description.
///
[SecurityPermission(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.UnmanagedCode)]
public Win32Exception(int error, string message)
: base(message) {
nativeErrorCode = error;
}
///
/// Initializes a new instance of the Exception class with a specified error message.
/// FxCop CA1032: Multiple constructors are required to correctly implement a custom exception.
///
[SecurityPermission(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.UnmanagedCode)]
public Win32Exception( string message ) : this(Marshal.GetLastWin32Error(), message) {
}
///
/// 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.
/// FxCop CA1032: Multiple constructors are required to correctly implement a custom exception.
///
[SecurityPermission(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.UnmanagedCode)]
public Win32Exception( string message, Exception innerException ) : base(message, innerException) {
nativeErrorCode = Marshal.GetLastWin32Error();
}
protected Win32Exception(SerializationInfo info, StreamingContext context) : base (info, context) {
IntSecurity.UnmanagedCode.Demand();
nativeErrorCode = info.GetInt32("NativeErrorCode");
}
///
/// Represents the Win32 error code associated with this exception. This
/// field is read-only.
///
public int NativeErrorCode {
get {
return nativeErrorCode;
}
}
private static string GetErrorMessage(int error) {
//get the system error message...
string errorMsg = "";
StringBuilder sb = new StringBuilder(256);
int result = SafeNativeMethods.FormatMessage(
SafeNativeMethods.FORMAT_MESSAGE_IGNORE_INSERTS |
SafeNativeMethods.FORMAT_MESSAGE_FROM_SYSTEM |
SafeNativeMethods.FORMAT_MESSAGE_ARGUMENT_ARRAY,
NativeMethods.NullHandleRef, error, 0, sb, sb.Capacity + 1,
IntPtr.Zero);
if (result != 0) {
int i = sb.Length;
while (i > 0) {
char ch = sb[i - 1];
if (ch > 32 && ch != '.') break;
i--;
}
errorMsg = sb.ToString(0, i);
}
else {
errorMsg ="Unknown error (0x" + Convert.ToString(error, 16) + ")";
}
return errorMsg;
}
// Even though all we're exposing is the nativeErrorCode (which is also available via public property)
// it's not a bad idea to have this in place. Later, if more fields are added to this exception,
// we won't need to worry about accidentaly exposing them through this interface.
[SecurityPermissionAttribute(SecurityAction.Demand,SerializationFormatter=true)]
public override void GetObjectData(SerializationInfo info, StreamingContext context) {
if (info==null) {
throw new ArgumentNullException("info");
}
info.AddValue("NativeErrorCode", nativeErrorCode);
base.GetObjectData(info, context);
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- DelegateSerializationHolder.cs
- AmbiguousMatchException.cs
- TextRunProperties.cs
- TemplateKey.cs
- ProgressChangedEventArgs.cs
- PageMediaType.cs
- Line.cs
- DataGridBoolColumn.cs
- AspNetHostingPermission.cs
- CodeGenerator.cs
- MatrixTransform3D.cs
- BitmapEncoder.cs
- StringInfo.cs
- ArcSegment.cs
- EventDescriptorCollection.cs
- MethodSignatureGenerator.cs
- ProtocolsConfigurationHandler.cs
- DynamicValidatorEventArgs.cs
- TemplateAction.cs
- TraceSection.cs
- TranslateTransform.cs
- DictionaryBase.cs
- CategoryEditor.cs
- DrawingVisual.cs
- JsonXmlDataContract.cs
- BufferAllocator.cs
- TemplatedAdorner.cs
- CommandDevice.cs
- XmlSchemaSimpleTypeList.cs
- ReadOnlyHierarchicalDataSource.cs
- PrintDocument.cs
- Point3DCollectionValueSerializer.cs
- StringFormat.cs
- ErrorHandler.cs
- CodePageEncoding.cs
- RSAPKCS1SignatureDeformatter.cs
- PtsHelper.cs
- ThemeDirectoryCompiler.cs
- MSAAWinEventWrap.cs
- VisualStateChangedEventArgs.cs
- DbConnectionStringBuilder.cs
- ConfigXmlElement.cs
- MultilineStringConverter.cs
- FontFaceLayoutInfo.cs
- LiteralText.cs
- PauseStoryboard.cs
- IdnElement.cs
- DataColumnMapping.cs
- WebPartEditorCancelVerb.cs
- StyleBamlRecordReader.cs
- StringPropertyBuilder.cs
- MultiAsyncResult.cs
- MiniAssembly.cs
- BaseUriWithWildcard.cs
- SynchronizationContext.cs
- EntitySetBase.cs
- DrawingServices.cs
- CommandEventArgs.cs
- HttpServerVarsCollection.cs
- AppSecurityManager.cs
- ExpressionParser.cs
- StreamUpgradeProvider.cs
- SendMailErrorEventArgs.cs
- CompilerParameters.cs
- DataGridItem.cs
- VisualTarget.cs
- BoundsDrawingContextWalker.cs
- UntypedNullExpression.cs
- ArgumentException.cs
- updatecommandorderer.cs
- InvokeBase.cs
- DataGridViewButtonCell.cs
- DataGridViewCellCollection.cs
- XomlCompiler.cs
- COM2AboutBoxPropertyDescriptor.cs
- __ConsoleStream.cs
- ManipulationPivot.cs
- MessageSmuggler.cs
- SeparatorAutomationPeer.cs
- WebServicesSection.cs
- VScrollBar.cs
- XMLDiffLoader.cs
- DbDeleteCommandTree.cs
- RelationshipWrapper.cs
- OptimalBreakSession.cs
- Point3D.cs
- Base64WriteStateInfo.cs
- Random.cs
- BoundPropertyEntry.cs
- BindStream.cs
- ACL.cs
- HeaderedContentControl.cs
- StylusDownEventArgs.cs
- DocumentViewer.cs
- DeflateStream.cs
- TrackingServices.cs
- PhotoPrintingIntent.cs
- SqlDataSourceCache.cs
- metrodevice.cs
- MailAddressCollection.cs