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
- ReachPrintTicketSerializer.cs
- IntegrationExceptionEventArgs.cs
- CodeAttributeArgumentCollection.cs
- XPathAncestorQuery.cs
- ResourceKey.cs
- CommentAction.cs
- PseudoWebRequest.cs
- DataGridViewHitTestInfo.cs
- BinarySerializer.cs
- Triplet.cs
- DataObjectCopyingEventArgs.cs
- GridView.cs
- SessionPageStateSection.cs
- MapPathBasedVirtualPathProvider.cs
- ToolStripDropDownClosedEventArgs.cs
- FaultDescriptionCollection.cs
- DbUpdateCommandTree.cs
- PropertyDescriptorCollection.cs
- ProcessInputEventArgs.cs
- Block.cs
- FunctionUpdateCommand.cs
- TextDecorationUnitValidation.cs
- EncoderParameter.cs
- SQLConvert.cs
- SignatureHelper.cs
- SiteMapDataSourceDesigner.cs
- UserControl.cs
- DetailsViewInsertEventArgs.cs
- TrackingServices.cs
- XmlDictionaryWriter.cs
- Page.cs
- SoapSchemaMember.cs
- GenericWebPart.cs
- FlagsAttribute.cs
- SQLInt32.cs
- MutexSecurity.cs
- Clause.cs
- StaticFileHandler.cs
- CommandExpr.cs
- _BufferOffsetSize.cs
- TreeNodeMouseHoverEvent.cs
- DataGridViewSelectedCellCollection.cs
- SequentialUshortCollection.cs
- StatusBar.cs
- XPathItem.cs
- PrintPreviewGraphics.cs
- WinFormsSecurity.cs
- LocalsItemDescription.cs
- QuaternionValueSerializer.cs
- TraceHandler.cs
- Trace.cs
- DBConnectionString.cs
- OleDbError.cs
- DeferredSelectedIndexReference.cs
- ReaderContextStackData.cs
- Hex.cs
- ToolStripProgressBar.cs
- InternalControlCollection.cs
- QueryRewriter.cs
- ConfigurationManagerHelper.cs
- DataListItem.cs
- AutoResizedEvent.cs
- TextElementCollectionHelper.cs
- CaseCqlBlock.cs
- XappLauncher.cs
- EventLogPermissionAttribute.cs
- ActiveXSite.cs
- WindowPatternIdentifiers.cs
- WorkflowInstanceProvider.cs
- DetailsViewInsertedEventArgs.cs
- AutomationInteropProvider.cs
- EDesignUtil.cs
- UnSafeCharBuffer.cs
- CallTemplateAction.cs
- _FixedSizeReader.cs
- ParseElementCollection.cs
- propertytag.cs
- WeakEventManager.cs
- ToolStripItemRenderEventArgs.cs
- UncommonField.cs
- DropShadowBitmapEffect.cs
- BitmapSourceSafeMILHandle.cs
- WindowsSpinner.cs
- LiteralText.cs
- coordinator.cs
- ResourceReferenceExpressionConverter.cs
- DataGridViewCellValueEventArgs.cs
- StateRuntime.cs
- DaylightTime.cs
- DynamicDocumentPaginator.cs
- XmlReader.cs
- StorageModelBuildProvider.cs
- SystemIPGlobalStatistics.cs
- TextLine.cs
- GridLength.cs
- RefreshEventArgs.cs
- MemberPath.cs
- DetailsViewPageEventArgs.cs
- KeyNotFoundException.cs
- Translator.cs