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
![Network programming in C#, Network Programming in VB.NET, Network Programming in .NET](/images/book.jpg)
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- RelationshipConverter.cs
- StringExpressionSet.cs
- WindowsFormsSynchronizationContext.cs
- DbInsertCommandTree.cs
- InputBinding.cs
- followingquery.cs
- XmlUtil.cs
- NavigatingCancelEventArgs.cs
- SelectionRangeConverter.cs
- SqlServices.cs
- WebControlsSection.cs
- PrinterUnitConvert.cs
- ProfileManager.cs
- DESCryptoServiceProvider.cs
- MergeLocalizationDirectives.cs
- HtmlTableCell.cs
- ChangeProcessor.cs
- TreeView.cs
- SocketInformation.cs
- ToolStripPanelSelectionBehavior.cs
- MsmqInputMessagePool.cs
- WindowCollection.cs
- ArgumentOutOfRangeException.cs
- WebServiceClientProxyGenerator.cs
- FormsAuthentication.cs
- DelegateTypeInfo.cs
- IncrementalCompileAnalyzer.cs
- BufferedReadStream.cs
- LinearKeyFrames.cs
- ConfigurationManagerInternal.cs
- EnumMember.cs
- MergePropertyDescriptor.cs
- HttpHandlerActionCollection.cs
- CounterSetInstance.cs
- PackageStore.cs
- WebReferencesBuildProvider.cs
- NegationPusher.cs
- ModuleBuilder.cs
- XPathNode.cs
- CompilerGlobalScopeAttribute.cs
- JsonEncodingStreamWrapper.cs
- ToolboxCategory.cs
- PropertyPath.cs
- XmlQualifiedNameTest.cs
- StringAnimationUsingKeyFrames.cs
- FileSystemInfo.cs
- TraversalRequest.cs
- TextEndOfParagraph.cs
- SchemaNames.cs
- AppSettingsSection.cs
- _HeaderInfoTable.cs
- ResourceAttributes.cs
- HashSetEqualityComparer.cs
- DialogResultConverter.cs
- DbDataReader.cs
- X509ServiceCertificateAuthentication.cs
- SamlAssertionKeyIdentifierClause.cs
- UDPClient.cs
- MetadataItemEmitter.cs
- PrincipalPermission.cs
- PageStatePersister.cs
- InstanceDataCollectionCollection.cs
- DbCommandTree.cs
- ProviderIncompatibleException.cs
- ForEachAction.cs
- UserControlCodeDomTreeGenerator.cs
- SmiEventSink_DeferedProcessing.cs
- XPathDocumentBuilder.cs
- SingleKeyFrameCollection.cs
- XmlDocumentViewSchema.cs
- ResourcesGenerator.cs
- XmlSchemaIdentityConstraint.cs
- LeaseManager.cs
- WebPartEditorApplyVerb.cs
- StreamReader.cs
- DatatypeImplementation.cs
- counter.cs
- ConfigXmlCDataSection.cs
- Rect.cs
- AdapterDictionary.cs
- ClientConfigPaths.cs
- HttpRuntimeSection.cs
- SourceFilter.cs
- AssociationTypeEmitter.cs
- AttributeEmitter.cs
- DataServiceExpressionVisitor.cs
- DateBoldEvent.cs
- HtmlUtf8RawTextWriter.cs
- XmlSchemaCompilationSettings.cs
- BaseProcessor.cs
- SqlMethods.cs
- AffineTransform3D.cs
- DynamicPropertyHolder.cs
- CalendarButton.cs
- FamilyTypeface.cs
- SectionInformation.cs
- ZipArchive.cs
- WebPartUserCapability.cs
- BulletedListEventArgs.cs
- ScaleTransform3D.cs