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
- FunctionImportMapping.cs
- Thumb.cs
- KeySplineConverter.cs
- DynamicDataManager.cs
- NativeWrapper.cs
- CTreeGenerator.cs
- SQLDecimalStorage.cs
- SqlBuffer.cs
- Operator.cs
- PeerNearMe.cs
- JournalEntryListConverter.cs
- DocumentStream.cs
- SystemResourceHost.cs
- CellTreeSimplifier.cs
- COM2PictureConverter.cs
- RealProxy.cs
- StyleCollectionEditor.cs
- XmlEncoding.cs
- Literal.cs
- LinqDataSource.cs
- LinkClickEvent.cs
- NotifyParentPropertyAttribute.cs
- DaylightTime.cs
- RoleManagerModule.cs
- ManipulationPivot.cs
- ExtendedPropertyDescriptor.cs
- WindowsPrincipal.cs
- RuleSettingsCollection.cs
- ByteStreamGeometryContext.cs
- PointValueSerializer.cs
- CapacityStreamGeometryContext.cs
- ArcSegment.cs
- __ComObject.cs
- SHA1Cng.cs
- WS2007HttpBinding.cs
- NavigationPropertyEmitter.cs
- dbdatarecord.cs
- DataGridState.cs
- DesignerActionUI.cs
- CompatibleComparer.cs
- InvalidAsynchronousStateException.cs
- DesignerActionService.cs
- RegularExpressionValidator.cs
- GPRECT.cs
- EmptyReadOnlyDictionaryInternal.cs
- SingleAnimation.cs
- ping.cs
- HashMembershipCondition.cs
- CodeObject.cs
- FileDataSourceCache.cs
- Gdiplus.cs
- TextFormatterImp.cs
- SafeMILHandle.cs
- EntitySqlException.cs
- GiveFeedbackEvent.cs
- WindowsUpDown.cs
- IntegerValidatorAttribute.cs
- Margins.cs
- DataGridViewCellMouseEventArgs.cs
- DbModificationClause.cs
- RijndaelManaged.cs
- DbProviderServices.cs
- VideoDrawing.cs
- OleDbInfoMessageEvent.cs
- XPathParser.cs
- Formatter.cs
- MarkupObject.cs
- ErrorEventArgs.cs
- SHA384Managed.cs
- WebPartVerbsEventArgs.cs
- QuaternionValueSerializer.cs
- ProcessThreadCollection.cs
- PropertyItemInternal.cs
- BinaryObjectWriter.cs
- HtmlInputFile.cs
- ImmComposition.cs
- QueryContext.cs
- HttpCapabilitiesBase.cs
- EntityExpressionVisitor.cs
- SQLBytesStorage.cs
- HttpConfigurationSystem.cs
- EncoderParameter.cs
- ApplyHostConfigurationBehavior.cs
- ActiveXContainer.cs
- BaseTemplateBuildProvider.cs
- DocumentSchemaValidator.cs
- ShaderEffect.cs
- Base64Decoder.cs
- DbConnectionHelper.cs
- HandlerFactoryCache.cs
- ToolStripDropTargetManager.cs
- SmuggledIUnknown.cs
- AutoGeneratedField.cs
- EntityDataSourceEntitySetNameItem.cs
- httpserverutility.cs
- Splitter.cs
- ToolStripDropDownMenu.cs
- PointHitTestParameters.cs
- ConnectionProviderAttribute.cs
- HitTestWithGeometryDrawingContextWalker.cs