Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / WCF / infocard / common / managed / HGlobalSafeHandle.cs / 1305376 / HGlobalSafeHandle.cs
//------------------------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- namespace Microsoft.InfoCards { using System; using System.Runtime.InteropServices; using Microsoft.InfoCards.Diagnostics; using System.Runtime.CompilerServices; using System.Runtime.ConstrainedExecution; using System.Security; using IDT=Microsoft.InfoCards.Diagnostics.InfoCardTrace; // // Summary: // Provides a wrapper over HGlobal alloc'd memory guaranteeing that the // contents will be released in the presence of rude app domain and thread aborts. // internal class HGlobalSafeHandle : SafeHandle { public static HGlobalSafeHandle Construct() { return new HGlobalSafeHandle(); } public static HGlobalSafeHandle Construct( string managedString ) { IDT.DebugAssert( !String.IsNullOrEmpty( managedString) , "null string" ); int bytes = (managedString.Length + 1) * 2; return new HGlobalSafeHandle( Marshal.StringToHGlobalUni( managedString ), bytes ); } public static HGlobalSafeHandle Construct( int bytes ) { IDT.DebugAssert( bytes > 0, "attempt to allocate a handle with <= 0 bytes" ); return new HGlobalSafeHandle( Marshal.AllocHGlobal( bytes ), bytes ); } [SuppressUnmanagedCodeSecurity] [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] [DllImport("Kernel32.dll", EntryPoint="RtlZeroMemory", SetLastError=false)] public static extern void ZeroMemory( IntPtr dest, Int32 size ); // // The number of bytes allocated. // private int m_bytes; [ReliabilityContract( Consistency.WillNotCorruptState, Cer.Success ) ] private HGlobalSafeHandle( IntPtr toManage, int length ) : base( IntPtr.Zero, true ) { m_bytes = length; SetHandle( toManage ); } private HGlobalSafeHandle() : base( IntPtr.Zero, true ) {} public override bool IsInvalid { get { return ( IntPtr.Zero == base.handle ); } } // // Summary: // Zero the string contents and release the handle // protected override bool ReleaseHandle() { IDT.DebugAssert( !IsInvalid, "handle is invalid in release handle" ); IDT.DebugAssert( 0 != m_bytes, "invalid size" ); ZeroMemory( base.handle, m_bytes ); Marshal.FreeHGlobal( base.handle ); return true; } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. //------------------------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- namespace Microsoft.InfoCards { using System; using System.Runtime.InteropServices; using Microsoft.InfoCards.Diagnostics; using System.Runtime.CompilerServices; using System.Runtime.ConstrainedExecution; using System.Security; using IDT=Microsoft.InfoCards.Diagnostics.InfoCardTrace; // // Summary: // Provides a wrapper over HGlobal alloc'd memory guaranteeing that the // contents will be released in the presence of rude app domain and thread aborts. // internal class HGlobalSafeHandle : SafeHandle { public static HGlobalSafeHandle Construct() { return new HGlobalSafeHandle(); } public static HGlobalSafeHandle Construct( string managedString ) { IDT.DebugAssert( !String.IsNullOrEmpty( managedString) , "null string" ); int bytes = (managedString.Length + 1) * 2; return new HGlobalSafeHandle( Marshal.StringToHGlobalUni( managedString ), bytes ); } public static HGlobalSafeHandle Construct( int bytes ) { IDT.DebugAssert( bytes > 0, "attempt to allocate a handle with <= 0 bytes" ); return new HGlobalSafeHandle( Marshal.AllocHGlobal( bytes ), bytes ); } [SuppressUnmanagedCodeSecurity] [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] [DllImport("Kernel32.dll", EntryPoint="RtlZeroMemory", SetLastError=false)] public static extern void ZeroMemory( IntPtr dest, Int32 size ); // // The number of bytes allocated. // private int m_bytes; [ReliabilityContract( Consistency.WillNotCorruptState, Cer.Success ) ] private HGlobalSafeHandle( IntPtr toManage, int length ) : base( IntPtr.Zero, true ) { m_bytes = length; SetHandle( toManage ); } private HGlobalSafeHandle() : base( IntPtr.Zero, true ) {} public override bool IsInvalid { get { return ( IntPtr.Zero == base.handle ); } } // // Summary: // Zero the string contents and release the handle // protected override bool ReleaseHandle() { IDT.DebugAssert( !IsInvalid, "handle is invalid in release handle" ); IDT.DebugAssert( 0 != m_bytes, "invalid size" ); ZeroMemory( base.handle, m_bytes ); Marshal.FreeHGlobal( base.handle ); return true; } } } // 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
- SpellerStatusTable.cs
- ByteAnimationUsingKeyFrames.cs
- PagesSection.cs
- AppLevelCompilationSectionCache.cs
- TypeViewSchema.cs
- cookiecollection.cs
- HttpPostServerProtocol.cs
- FileLogRecordEnumerator.cs
- InputMethodStateTypeInfo.cs
- DesignerInterfaces.cs
- DataControlLinkButton.cs
- CharEnumerator.cs
- WorkflowCreationContext.cs
- RemotingConfigParser.cs
- ThreadPool.cs
- dataobject.cs
- WindowsToolbarItemAsMenuItem.cs
- Point3DCollectionConverter.cs
- UnmanagedMarshal.cs
- ServicesUtilities.cs
- JsonQNameDataContract.cs
- XmlSchemaSimpleContentRestriction.cs
- XmlSortKeyAccumulator.cs
- GroupBoxAutomationPeer.cs
- ToolStripComboBox.cs
- ContainerUtilities.cs
- ServiceDeploymentInfo.cs
- FocusChangedEventArgs.cs
- BaseCollection.cs
- EventMappingSettingsCollection.cs
- BigInt.cs
- PkcsUtils.cs
- LoginView.cs
- InputManager.cs
- EventWaitHandle.cs
- WorkflowRuntimeService.cs
- ObjectFullSpanRewriter.cs
- ReadOnlyPropertyMetadata.cs
- WorkflowApplicationCompletedEventArgs.cs
- ExplicitDiscriminatorMap.cs
- TextDecorations.cs
- AssociationEndMember.cs
- XmlHelper.cs
- SqlBulkCopy.cs
- AudioFormatConverter.cs
- DbQueryCommandTree.cs
- regiisutil.cs
- CustomAttributeBuilder.cs
- SqlDataSourceStatusEventArgs.cs
- BitmapEffectState.cs
- ThrowHelper.cs
- SqlTypeConverter.cs
- ImageFormatConverter.cs
- OdbcFactory.cs
- DoubleCollectionValueSerializer.cs
- RecognizeCompletedEventArgs.cs
- SoundPlayerAction.cs
- ExtensionSimplifierMarkupObject.cs
- DoWhile.cs
- Repeater.cs
- TypeUnloadedException.cs
- ListViewUpdatedEventArgs.cs
- XmlChildEnumerator.cs
- Boolean.cs
- Vector3DValueSerializer.cs
- Config.cs
- UnitySerializationHolder.cs
- WindowsAuthenticationEventArgs.cs
- login.cs
- MessageVersion.cs
- SmiMetaData.cs
- BrowserCapabilitiesFactory.cs
- StdValidatorsAndConverters.cs
- EdmValidator.cs
- LinkButton.cs
- Size.cs
- SqlParameterCollection.cs
- CSharpCodeProvider.cs
- PropertyStore.cs
- DatatypeImplementation.cs
- AVElementHelper.cs
- SqlBuilder.cs
- DescendantOverDescendantQuery.cs
- PersonalizationProviderHelper.cs
- smtpconnection.cs
- EllipticalNodeOperations.cs
- WebPartZone.cs
- AsymmetricKeyExchangeFormatter.cs
- RegexCode.cs
- SecurityElement.cs
- StreamResourceInfo.cs
- SHA1Cng.cs
- VirtualPathData.cs
- AuthenticationManager.cs
- SessionStateItemCollection.cs
- _FtpDataStream.cs
- AssemblyCache.cs
- Tile.cs
- UiaCoreTypesApi.cs
- WebConfigurationHostFileChange.cs