Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / WCF / infocard / common / managed / GlobalAllocSafeHandle.cs / 1305376 / GlobalAllocSafeHandle.cs
//------------------------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- namespace Microsoft.InfoCards { using System; using System.Runtime.InteropServices; using System.Runtime.CompilerServices; using System.Runtime.ConstrainedExecution; using System.Security; using Microsoft.InfoCards.Diagnostics; using IDT=Microsoft.InfoCards.Diagnostics.InfoCardTrace; // // Summary: // Provides a wrapper over memory allocated by GlobalAlloc // guaranteeing that it will be freed during rude thread / appdomain unloads. // Remarks: // There is a small ---- in the usage of this class, as it is used to wrap return parameters // immediatley following the function return. // internal class GlobalAllocSafeHandle : SafeHandle { [SuppressUnmanagedCodeSecurity] [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] [DllImport("Kernel32.dll", EntryPoint="RtlZeroMemory", SetLastError=false)] public static extern void ZeroMemory( IntPtr dest, Int32 size ); [SuppressUnmanagedCodeSecurity] [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] [DllImport( "kernel32.dll", CallingConvention = CallingConvention.StdCall )] public static extern IntPtr GlobalFree( IntPtr hMem ); // // How many bytes we currently wrap. This can be zero, as our usage allows for a valid handle // backed by 0 bytes of allocated memory - specificially TransformBlock and TransformFinalBlock // can return this by design. // private int m_bytes; private GlobalAllocSafeHandle() : base( IntPtr.Zero, true ) { m_bytes = 0; } public int Length { set{ m_bytes = value; } get{ return m_bytes; } } public override bool IsInvalid { get { return ( IntPtr.Zero == base.handle ); } } // // Summary: // Clear the data held and release the memory. // protected override bool ReleaseHandle() { if( m_bytes > 0 ) { ZeroMemory( base.handle, m_bytes ); GlobalFree( base.handle ); m_bytes = 0; } 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 System.Runtime.CompilerServices; using System.Runtime.ConstrainedExecution; using System.Security; using Microsoft.InfoCards.Diagnostics; using IDT=Microsoft.InfoCards.Diagnostics.InfoCardTrace; // // Summary: // Provides a wrapper over memory allocated by GlobalAlloc // guaranteeing that it will be freed during rude thread / appdomain unloads. // Remarks: // There is a small ---- in the usage of this class, as it is used to wrap return parameters // immediatley following the function return. // internal class GlobalAllocSafeHandle : SafeHandle { [SuppressUnmanagedCodeSecurity] [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] [DllImport("Kernel32.dll", EntryPoint="RtlZeroMemory", SetLastError=false)] public static extern void ZeroMemory( IntPtr dest, Int32 size ); [SuppressUnmanagedCodeSecurity] [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] [DllImport( "kernel32.dll", CallingConvention = CallingConvention.StdCall )] public static extern IntPtr GlobalFree( IntPtr hMem ); // // How many bytes we currently wrap. This can be zero, as our usage allows for a valid handle // backed by 0 bytes of allocated memory - specificially TransformBlock and TransformFinalBlock // can return this by design. // private int m_bytes; private GlobalAllocSafeHandle() : base( IntPtr.Zero, true ) { m_bytes = 0; } public int Length { set{ m_bytes = value; } get{ return m_bytes; } } public override bool IsInvalid { get { return ( IntPtr.Zero == base.handle ); } } // // Summary: // Clear the data held and release the memory. // protected override bool ReleaseHandle() { if( m_bytes > 0 ) { ZeroMemory( base.handle, m_bytes ); GlobalFree( base.handle ); m_bytes = 0; } 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
- UTF8Encoding.cs
- IPAddress.cs
- XmlAttributeCache.cs
- TraceSection.cs
- CommandDevice.cs
- HtmlMeta.cs
- UnaryExpression.cs
- KeyInterop.cs
- ListChangedEventArgs.cs
- Win32.cs
- ApplicationSecurityInfo.cs
- AssociatedControlConverter.cs
- UmAlQuraCalendar.cs
- XNameConverter.cs
- XmlObjectSerializerReadContextComplex.cs
- SqlDataRecord.cs
- Trace.cs
- ConfigurationStrings.cs
- DetailsViewCommandEventArgs.cs
- __ComObject.cs
- webeventbuffer.cs
- PackageProperties.cs
- HttpListenerTimeoutManager.cs
- ZipIOCentralDirectoryDigitalSignature.cs
- AssemblyAssociatedContentFileAttribute.cs
- OdbcError.cs
- SqlCacheDependencyDatabaseCollection.cs
- ShaperBuffers.cs
- SplashScreenNativeMethods.cs
- CopyOnWriteList.cs
- MailMessageEventArgs.cs
- SafeUserTokenHandle.cs
- LinkLabelLinkClickedEvent.cs
- PersonalizableAttribute.cs
- itemelement.cs
- ListSortDescriptionCollection.cs
- QueuePathEditor.cs
- EdmError.cs
- DynamicValueConverter.cs
- DependencyPropertyKey.cs
- ProfileSettingsCollection.cs
- __FastResourceComparer.cs
- ClientTargetSection.cs
- GenericRootAutomationPeer.cs
- SymmetricAlgorithm.cs
- RawKeyboardInputReport.cs
- ProxyManager.cs
- PeerApplicationLaunchInfo.cs
- RemotingAttributes.cs
- Symbol.cs
- OdbcEnvironment.cs
- XamlInt32CollectionSerializer.cs
- SelectionProviderWrapper.cs
- NegatedConstant.cs
- EncoderBestFitFallback.cs
- DefaultWorkflowLoaderService.cs
- SafeHandles.cs
- OutputCacheSection.cs
- BitmapEditor.cs
- StringPropertyBuilder.cs
- XPathNodeHelper.cs
- AuthenticatedStream.cs
- DataGridCommandEventArgs.cs
- ByteConverter.cs
- DecoderBestFitFallback.cs
- AudioLevelUpdatedEventArgs.cs
- LocalizedNameDescriptionPair.cs
- Asn1Utilities.cs
- PerformanceCounterPermissionEntry.cs
- LayoutTable.cs
- HeaderLabel.cs
- CompositeControl.cs
- ExtensibleClassFactory.cs
- IntSecurity.cs
- FileVersion.cs
- SystemGatewayIPAddressInformation.cs
- XmlReader.cs
- WebPartAuthorizationEventArgs.cs
- PrinterUnitConvert.cs
- HttpHostedTransportConfiguration.cs
- EncryptedKeyHashIdentifierClause.cs
- BamlBinaryWriter.cs
- SettingsBindableAttribute.cs
- mil_commands.cs
- DataSourceNameHandler.cs
- SearchExpression.cs
- ButtonFlatAdapter.cs
- KeyManager.cs
- XMLDiffLoader.cs
- DataViewSetting.cs
- LineGeometry.cs
- ADMembershipProvider.cs
- objectquery_tresulttype.cs
- ListBoxItemAutomationPeer.cs
- Properties.cs
- NumberSubstitution.cs
- Reference.cs
- ResourceDescriptionAttribute.cs
- Compress.cs
- TableLayoutStyle.cs