Code:
/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / WCF / infocard / common / managed / HGlobalSafeHandle.cs / 1 / 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.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- AttachmentCollection.cs
- VoiceSynthesis.cs
- RewritingProcessor.cs
- DataGridState.cs
- ProcessRequestArgs.cs
- ToolboxComponentsCreatedEventArgs.cs
- ConfigurationValidatorBase.cs
- HttpServerVarsCollection.cs
- SourceChangedEventArgs.cs
- DesignBindingValueUIHandler.cs
- AttributeTableBuilder.cs
- EntityDataSourceEntityTypeFilterItem.cs
- ProcessStartInfo.cs
- DoubleAnimation.cs
- TreeNodeConverter.cs
- SingleKeyFrameCollection.cs
- Padding.cs
- ToolStripPanelRow.cs
- WizardPanel.cs
- CounterCreationData.cs
- MatchNoneMessageFilter.cs
- OutOfMemoryException.cs
- TdsParserSafeHandles.cs
- CryptoSession.cs
- PatternMatcher.cs
- PreProcessInputEventArgs.cs
- FileRecordSequenceHelper.cs
- AuthenticationConfig.cs
- ScaleTransform3D.cs
- JsonMessageEncoderFactory.cs
- DataGridSortCommandEventArgs.cs
- MethodBuilder.cs
- XPathNavigatorKeyComparer.cs
- TagNameToTypeMapper.cs
- Pens.cs
- AmbientLight.cs
- clipboard.cs
- SmtpReplyReaderFactory.cs
- SqlDataSourceCommandEventArgs.cs
- PathTooLongException.cs
- DBCSCodePageEncoding.cs
- WebPartDescription.cs
- CustomExpressionEventArgs.cs
- IssuedTokenServiceElement.cs
- FullTextLine.cs
- OutputCacheProfile.cs
- SynchronizationContext.cs
- PropertyGridView.cs
- LayoutDump.cs
- NeutralResourcesLanguageAttribute.cs
- ChtmlSelectionListAdapter.cs
- LifetimeServices.cs
- WorkflowInstanceSuspendedRecord.cs
- _DisconnectOverlappedAsyncResult.cs
- DecoderFallback.cs
- WebPartChrome.cs
- ProjectionCamera.cs
- sqlmetadatafactory.cs
- Sentence.cs
- BuildManager.cs
- RadialGradientBrush.cs
- Math.cs
- ConfigurationLocationCollection.cs
- XmlSchemaType.cs
- CategoryGridEntry.cs
- XmlName.cs
- RegexReplacement.cs
- PerformanceCounterCategory.cs
- Win32Exception.cs
- DataFormats.cs
- AdornerLayer.cs
- HMACMD5.cs
- CurrentChangingEventManager.cs
- Size3DConverter.cs
- BitmapCacheBrush.cs
- RoleManagerEventArgs.cs
- RowCache.cs
- SystemFonts.cs
- AffineTransform3D.cs
- EasingKeyFrames.cs
- UInt16Storage.cs
- StatusCommandUI.cs
- CollectionViewGroup.cs
- QueryOutputWriter.cs
- ObjectQueryProvider.cs
- ListBox.cs
- XmlEncApr2001.cs
- ResourceWriter.cs
- PrintPreviewGraphics.cs
- ShapingEngine.cs
- ObjectCloneHelper.cs
- CompositeFontFamily.cs
- HtmlInputControl.cs
- ProcessThreadCollection.cs
- HasCopySemanticsAttribute.cs
- NodeInfo.cs
- ColorDialog.cs
- DateTimeValueSerializerContext.cs
- DataGridColumnCollectionEditor.cs
- FixedHyperLink.cs