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
- InfoCardSymmetricCrypto.cs
- CheckBoxFlatAdapter.cs
- ProfileInfo.cs
- WebConfigurationHost.cs
- XhtmlBasicCommandAdapter.cs
- DesignerTextBoxAdapter.cs
- ActiveXContainer.cs
- ConfigurationSection.cs
- StylusPointDescription.cs
- Inflater.cs
- log.cs
- DbParameterHelper.cs
- Aggregates.cs
- ToolStripMenuItemDesigner.cs
- HttpConfigurationContext.cs
- CustomAttributeFormatException.cs
- PageMediaType.cs
- AcceptorSessionSymmetricTransportSecurityProtocol.cs
- DataKey.cs
- FactoryGenerator.cs
- ObjectDataSourceFilteringEventArgs.cs
- EventLogEntryCollection.cs
- ReferenceConverter.cs
- XPathEmptyIterator.cs
- CacheChildrenQuery.cs
- EditorZoneBase.cs
- ExpressionPrefixAttribute.cs
- RemotingSurrogateSelector.cs
- DataContractSerializerServiceBehavior.cs
- ResourcePool.cs
- VisualTarget.cs
- Quad.cs
- MonitoringDescriptionAttribute.cs
- AttachmentService.cs
- IndexedSelectQueryOperator.cs
- PageParser.cs
- ConstructorNeedsTagAttribute.cs
- HtmlContainerControl.cs
- RSAPKCS1KeyExchangeDeformatter.cs
- FontTypeConverter.cs
- MobileDeviceCapabilitiesSectionHandler.cs
- SqlTypesSchemaImporter.cs
- WebBrowser.cs
- SessionPageStatePersister.cs
- DuplicateWaitObjectException.cs
- CLRBindingWorker.cs
- OdbcConnectionStringbuilder.cs
- SchemaConstraints.cs
- _UriTypeConverter.cs
- WorkflowInstanceUnhandledExceptionRecord.cs
- TextCharacters.cs
- DocumentPage.cs
- WriteTimeStream.cs
- DispatcherProcessingDisabled.cs
- Column.cs
- Simplifier.cs
- ComponentEditorForm.cs
- BufferBuilder.cs
- GeometryModel3D.cs
- PathGeometry.cs
- RealizationDrawingContextWalker.cs
- BindingCompleteEventArgs.cs
- ManagedWndProcTracker.cs
- DataGridViewLinkColumn.cs
- StringComparer.cs
- invalidudtexception.cs
- FunctionDescription.cs
- Rule.cs
- WebDescriptionAttribute.cs
- XmlTextReader.cs
- DataProtection.cs
- CfgSemanticTag.cs
- OdbcPermission.cs
- View.cs
- xmlsaver.cs
- PerformanceCounter.cs
- DependencyPropertyKey.cs
- VirtualDirectoryMappingCollection.cs
- FtpWebRequest.cs
- PropertyTabAttribute.cs
- Rect3D.cs
- DCSafeHandle.cs
- FontResourceCache.cs
- ProcessHostServerConfig.cs
- _ChunkParse.cs
- WindowCollection.cs
- ObjectRef.cs
- SqlDataAdapter.cs
- CategoryGridEntry.cs
- CreateUserWizardStep.cs
- HttpListenerTimeoutManager.cs
- NullableDoubleAverageAggregationOperator.cs
- DataStorage.cs
- OleDbConnectionPoolGroupProviderInfo.cs
- StandardBindingElementCollection.cs
- SerializationInfoEnumerator.cs
- XmlCharacterData.cs
- BevelBitmapEffect.cs
- sitestring.cs
- XmlSchemaException.cs