Code:
/ FX-1434 / FX-1434 / 1.0 / untmp / whidbey / REDBITS / ndp / fx / src / CompMod / System / ComponentModel / Win32Exception.cs / 1 / 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; ////// // 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 { ///The exception that is thrown for a Win32 error code. ////// private readonly int nativeErrorCode; ///Represents the Win32 error code associated with this exception. This /// field is read-only. ////// [SecurityPermission(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.UnmanagedCode)] public Win32Exception() : this(Marshal.GetLastWin32Error()) { } ///Initializes a new instance of the ///class with the last Win32 error /// that occured. /// [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. /// [SecurityPermission(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.UnmanagedCode)] public Win32Exception(int error, string message) : base(message) { nativeErrorCode = error; } ///Initializes a new instance of the ///class with the specified error and the /// specified detailed description. /// 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"); } ////// 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); } } }Represents the Win32 error code associated with this exception. This /// field is read-only. ///
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- BevelBitmapEffect.cs
- SafeNativeMethodsCLR.cs
- _SslSessionsCache.cs
- SystemIcmpV4Statistics.cs
- UnsafeNativeMethods.cs
- DataTablePropertyDescriptor.cs
- SoapFormatter.cs
- SrgsGrammar.cs
- UnmanagedMemoryStreamWrapper.cs
- Helper.cs
- XPathNodeInfoAtom.cs
- RenderCapability.cs
- SignatureDescription.cs
- WebBrowserHelper.cs
- StateMachineWorkflowInstance.cs
- DoubleCollectionValueSerializer.cs
- CodeSubDirectoriesCollection.cs
- CredentialCache.cs
- DesignerDataTableBase.cs
- ReferenceSchema.cs
- precedingquery.cs
- MessageLogger.cs
- BufferedGraphicsManager.cs
- WorkflowTerminatedException.cs
- ChameleonKey.cs
- SQLChars.cs
- LifetimeServices.cs
- TemplateComponentConnector.cs
- CodeDelegateInvokeExpression.cs
- ContextConfiguration.cs
- DbSetClause.cs
- PenLineJoinValidation.cs
- TaskHelper.cs
- ProfileService.cs
- TextControl.cs
- PrePrepareMethodAttribute.cs
- SystemSounds.cs
- ContextInformation.cs
- CodeGotoStatement.cs
- DataViewManager.cs
- IfAction.cs
- InlineCollection.cs
- DataGridViewRowsAddedEventArgs.cs
- ArrayMergeHelper.cs
- CommandBindingCollection.cs
- ColorContextHelper.cs
- ResourceContainer.cs
- RectValueSerializer.cs
- ProxyWebPartManager.cs
- DbTransaction.cs
- XmlDataSourceNodeDescriptor.cs
- GenericPrincipal.cs
- validationstate.cs
- BStrWrapper.cs
- FlowLayout.cs
- DataControlCommands.cs
- TreeBuilderBamlTranslator.cs
- List.cs
- oledbmetadatacollectionnames.cs
- PngBitmapEncoder.cs
- MenuItemStyle.cs
- GuidConverter.cs
- Buffer.cs
- PrintingPermission.cs
- ToolStripOverflowButton.cs
- _KerberosClient.cs
- AuthenticateEventArgs.cs
- Overlapped.cs
- TokenBasedSetEnumerator.cs
- WinEventWrap.cs
- RadialGradientBrush.cs
- ControlBindingsCollection.cs
- CompiledRegexRunner.cs
- X509SubjectKeyIdentifierClause.cs
- SelectionItemPattern.cs
- SelectionPatternIdentifiers.cs
- _ConnectionGroup.cs
- HttpCacheVaryByContentEncodings.cs
- ColorInterpolationModeValidation.cs
- SqlConnectionPoolGroupProviderInfo.cs
- DataGridViewSelectedCellCollection.cs
- ObjectDataSourceMethodEditor.cs
- PreviewPageInfo.cs
- SelectionRange.cs
- XPathNavigatorKeyComparer.cs
- XmlValueConverter.cs
- IpcClientChannel.cs
- EventMap.cs
- BorderGapMaskConverter.cs
- ByteStreamGeometryContext.cs
- Cursors.cs
- TableAutomationPeer.cs
- TransactionTable.cs
- MarkupExtensionReturnTypeAttribute.cs
- SqlConnectionPoolGroupProviderInfo.cs
- ToolBar.cs
- ServerValidateEventArgs.cs
- ModelVisual3D.cs
- LineGeometry.cs
- ADRole.cs