Code:
/ Dotnetfx_Vista_SP2 / Dotnetfx_Vista_SP2 / 8.0.50727.4016 / DEVDIV / depot / DevDiv / releases / whidbey / NetFxQFE / 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); } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. //------------------------------------------------------------------------------ //Represents the Win32 error code associated with this exception. This /// field is read-only. ///// 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); } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007.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
- WindowsGraphicsWrapper.cs
- RawStylusInputCustomDataList.cs
- SqlDataSourceConfigureFilterForm.cs
- BinarySerializer.cs
- XmlNullResolver.cs
- EditingScopeUndoUnit.cs
- PersonalizationState.cs
- ListView.cs
- FreezableDefaultValueFactory.cs
- ObfuscationAttribute.cs
- WindowsGraphicsCacheManager.cs
- BamlLocalizationDictionary.cs
- MenuItemAutomationPeer.cs
- HwndPanningFeedback.cs
- ActivityExecutor.cs
- SigningCredentials.cs
- CodeCatchClauseCollection.cs
- PhoneCallDesigner.cs
- BufferedOutputStream.cs
- MobileRedirect.cs
- FileUtil.cs
- SafeMarshalContext.cs
- DataGridViewAutoSizeColumnsModeEventArgs.cs
- WsdlEndpointConversionContext.cs
- RoutedPropertyChangedEventArgs.cs
- ListViewInsertionMark.cs
- DataFormats.cs
- QueryOperationResponseOfT.cs
- EventEntry.cs
- HtmlSelectionListAdapter.cs
- CollectionConverter.cs
- LogSwitch.cs
- CommandHelper.cs
- DataColumnSelectionConverter.cs
- DelayedRegex.cs
- GridViewRowPresenter.cs
- CSharpCodeProvider.cs
- StringFunctions.cs
- ObjectItemLoadingSessionData.cs
- InvalidStoreProtectionKeyException.cs
- SelectedGridItemChangedEvent.cs
- MessageSmuggler.cs
- XmlElement.cs
- LinkDescriptor.cs
- MimeFormImporter.cs
- RuntimeWrappedException.cs
- TableLayout.cs
- Dictionary.cs
- DBSchemaRow.cs
- DataGridViewCellMouseEventArgs.cs
- DBDataPermissionAttribute.cs
- ButtonColumn.cs
- OpenTypeLayout.cs
- SqlException.cs
- PropertyInformation.cs
- SmtpClient.cs
- ManipulationDelta.cs
- PermissionListSet.cs
- validationstate.cs
- UIAgentCrashedException.cs
- StringInfo.cs
- CharacterBufferReference.cs
- EdmComplexPropertyAttribute.cs
- DateTime.cs
- GeometryHitTestResult.cs
- XmlValueConverter.cs
- FormViewModeEventArgs.cs
- XmlCharacterData.cs
- HtmlElement.cs
- SemaphoreFullException.cs
- RepeaterItemCollection.cs
- ByteStreamBufferedMessageData.cs
- TextElementEditingBehaviorAttribute.cs
- ContractTypeNameCollection.cs
- AlphaSortedEnumConverter.cs
- RealProxy.cs
- SchemaElement.cs
- FormatStringEditor.cs
- CharacterMetrics.cs
- TabPanel.cs
- OpacityConverter.cs
- HtmlTableRowCollection.cs
- AutomationEvent.cs
- CanonicalXml.cs
- ClientRuntime.cs
- AddInAttribute.cs
- PointConverter.cs
- DecoderExceptionFallback.cs
- FreezableDefaultValueFactory.cs
- OdbcConnectionOpen.cs
- XmlAggregates.cs
- StructuredTypeInfo.cs
- ComponentSerializationService.cs
- TdsParserStateObject.cs
- CodeSnippetStatement.cs
- RefreshPropertiesAttribute.cs
- TemplatedEditableDesignerRegion.cs
- UidPropertyAttribute.cs
- CommandDevice.cs
- EventRecord.cs