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
- FixedDocumentPaginator.cs
- GridView.cs
- PermissionSetEnumerator.cs
- MouseActionValueSerializer.cs
- SizeIndependentAnimationStorage.cs
- WpfXamlType.cs
- OuterGlowBitmapEffect.cs
- DataGridViewBindingCompleteEventArgs.cs
- ButtonFieldBase.cs
- LocalizableResourceBuilder.cs
- DSACryptoServiceProvider.cs
- FastEncoderStatics.cs
- BrowserTree.cs
- ControlBindingsConverter.cs
- DbProviderSpecificTypePropertyAttribute.cs
- Socket.cs
- hebrewshape.cs
- RectangleF.cs
- ProxyManager.cs
- ActiveXHost.cs
- CallTemplateAction.cs
- ParameterModifier.cs
- oledbmetadatacollectionnames.cs
- XmlLinkedNode.cs
- CollectionChangeEventArgs.cs
- EUCJPEncoding.cs
- ParseElement.cs
- UInt64Converter.cs
- AmbiguousMatchException.cs
- WebPartsPersonalization.cs
- SqlCommandSet.cs
- AutomationPeer.cs
- GrammarBuilder.cs
- ClientUtils.cs
- XmlTextReaderImpl.cs
- NativeCompoundFileAPIs.cs
- RenderTargetBitmap.cs
- DependencyObject.cs
- SrgsSemanticInterpretationTag.cs
- DecoderReplacementFallback.cs
- QilTernary.cs
- CompilationLock.cs
- AppPool.cs
- MultipleViewPattern.cs
- SafeNativeMethods.cs
- RootBrowserWindow.cs
- QueryRewriter.cs
- ConfigXmlSignificantWhitespace.cs
- FontResourceCache.cs
- XmlSchemaFacet.cs
- Vector3DAnimation.cs
- GZipObjectSerializer.cs
- SourceChangedEventArgs.cs
- XmlTextReader.cs
- CompositeFontInfo.cs
- FixedDSBuilder.cs
- EntityDataSourceEntityTypeFilterItem.cs
- WinInetCache.cs
- ExpressionConverter.cs
- AuthenticationModulesSection.cs
- Site.cs
- Resources.Designer.cs
- RotateTransform3D.cs
- RankException.cs
- XmlSchemaObject.cs
- COM2ComponentEditor.cs
- ErrorHandler.cs
- PrintDialog.cs
- EqualityComparer.cs
- ThreadExceptionDialog.cs
- SpecularMaterial.cs
- SecurityTokenException.cs
- RandomNumberGenerator.cs
- CharEnumerator.cs
- MaskedTextBoxTextEditor.cs
- CodeNamespaceImportCollection.cs
- DefaultConfirmation.cs
- InitiatorSessionSymmetricMessageSecurityProtocol.cs
- DataSysAttribute.cs
- WebPartConnectionsDisconnectVerb.cs
- unsafeIndexingFilterStream.cs
- ReferenceCountedObject.cs
- _Win32.cs
- ContractUtils.cs
- SymmetricAlgorithm.cs
- securitycriticaldata.cs
- EventHandlerList.cs
- querybuilder.cs
- DelimitedListTraceListener.cs
- UniqueEventHelper.cs
- Rect3DConverter.cs
- DeferredReference.cs
- DataGridView.cs
- SaveFileDialog.cs
- PrintPreviewDialog.cs
- LinearGradientBrush.cs
- WebPartUtil.cs
- TextLineResult.cs
- RtfToken.cs
- Interlocked.cs