Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / wpf / src / Core / CSharp / System / Windows / Media / SafeMILHandle.cs / 1305600 / SafeMILHandle.cs
//----------------------------------------------------------------------------
//
//
// Copyright (C) Microsoft Corporation. All rights reserved.
//
//
//
// Description:
// A safe way to deal with unmanaged MIL interface pointers.
//---------------------------------------------------------------------------
using System;
using System.Security;
using Microsoft.Win32.SafeHandles;
using UnsafeNativeMethods = MS.Win32.PresentationCore.UnsafeNativeMethods;
namespace System.Windows.Media
{
internal class SafeMILHandle : SafeHandleZeroOrMinusOneIsInvalid
{
///
/// Use this constructor if the handle isn't ready yet and later
/// set the handle with SetHandle. SafeMILHandle owns the release
/// of the handle.
///
///
/// Critical: This derives from a class that has a link demand and inheritance demand
/// TreatAsSafe: Ok to call constructor
///
[SecurityCritical,SecurityTreatAsSafe]
internal SafeMILHandle() : base(true)
{
}
///
/// Use this constructor if the handle exists at construction time.
/// SafeMILHandle owns the release of the parameter.
///
///
/// Critical: This derives from a class that has a link demand and inheritance demand
///
[SecurityCritical]
internal SafeMILHandle(IntPtr handle) : base(true)
{
SetHandle(handle);
}
///
/// Change our size to the new size specified
///
///
/// Critical: This code calls into AddMemoryPressure and RemoveMemoryPressure
/// both of which have link demands. It is used to keep memory around
///
[SecurityCritical]
internal void UpdateEstimatedSize(long estimatedSize)
{
if (_gcPressure != null)
{
_gcPressure.Release();
}
//
// estimatedSize may be 0 for small images with fewer than 8 bits per pixel,
// attempting to call GC.AddMemoryPressure with a pressure of 0 will cause it to
// throw, so don't add memory pressure if estimatedSize is 0.
//
if (estimatedSize > 0)
{
_gcPressure = new SafeMILHandleMemoryPressure(estimatedSize);
_gcPressure.AddRef();
}
}
internal void CopyMemoryPressure(SafeMILHandle original)
{
_gcPressure = original._gcPressure;
if (_gcPressure != null)
{
_gcPressure.AddRef();
}
}
///
/// Critical - calls unmanaged code, not treat as safe because you must
/// validate that handle is a valid COM object.
///
[SecurityCritical]
protected override bool ReleaseHandle()
{
UnsafeNativeMethods.MILUnknown.ReleaseInterface(ref handle);
if (_gcPressure != null)
{
_gcPressure.Release();
_gcPressure = null;
}
return true;
}
// Estimated size of the unmanaged memory
private SafeMILHandleMemoryPressure _gcPressure;
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
//----------------------------------------------------------------------------
//
//
// Copyright (C) Microsoft Corporation. All rights reserved.
//
//
//
// Description:
// A safe way to deal with unmanaged MIL interface pointers.
//---------------------------------------------------------------------------
using System;
using System.Security;
using Microsoft.Win32.SafeHandles;
using UnsafeNativeMethods = MS.Win32.PresentationCore.UnsafeNativeMethods;
namespace System.Windows.Media
{
internal class SafeMILHandle : SafeHandleZeroOrMinusOneIsInvalid
{
///
/// Use this constructor if the handle isn't ready yet and later
/// set the handle with SetHandle. SafeMILHandle owns the release
/// of the handle.
///
///
/// Critical: This derives from a class that has a link demand and inheritance demand
/// TreatAsSafe: Ok to call constructor
///
[SecurityCritical,SecurityTreatAsSafe]
internal SafeMILHandle() : base(true)
{
}
///
/// Use this constructor if the handle exists at construction time.
/// SafeMILHandle owns the release of the parameter.
///
///
/// Critical: This derives from a class that has a link demand and inheritance demand
///
[SecurityCritical]
internal SafeMILHandle(IntPtr handle) : base(true)
{
SetHandle(handle);
}
///
/// Change our size to the new size specified
///
///
/// Critical: This code calls into AddMemoryPressure and RemoveMemoryPressure
/// both of which have link demands. It is used to keep memory around
///
[SecurityCritical]
internal void UpdateEstimatedSize(long estimatedSize)
{
if (_gcPressure != null)
{
_gcPressure.Release();
}
//
// estimatedSize may be 0 for small images with fewer than 8 bits per pixel,
// attempting to call GC.AddMemoryPressure with a pressure of 0 will cause it to
// throw, so don't add memory pressure if estimatedSize is 0.
//
if (estimatedSize > 0)
{
_gcPressure = new SafeMILHandleMemoryPressure(estimatedSize);
_gcPressure.AddRef();
}
}
internal void CopyMemoryPressure(SafeMILHandle original)
{
_gcPressure = original._gcPressure;
if (_gcPressure != null)
{
_gcPressure.AddRef();
}
}
///
/// Critical - calls unmanaged code, not treat as safe because you must
/// validate that handle is a valid COM object.
///
[SecurityCritical]
protected override bool ReleaseHandle()
{
UnsafeNativeMethods.MILUnknown.ReleaseInterface(ref handle);
if (_gcPressure != null)
{
_gcPressure.Release();
_gcPressure = null;
}
return true;
}
// Estimated size of the unmanaged memory
private SafeMILHandleMemoryPressure _gcPressure;
}
}
// 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
- ExpandCollapsePattern.cs
- UnsafeNetInfoNativeMethods.cs
- XsdBuildProvider.cs
- SmtpReplyReaderFactory.cs
- RectangleGeometry.cs
- Action.cs
- HttpCacheParams.cs
- NetworkStream.cs
- SmtpNtlmAuthenticationModule.cs
- OpenTypeLayoutCache.cs
- StylusPointPropertyId.cs
- NavigationExpr.cs
- SQLChars.cs
- ReferenceConverter.cs
- RemotingService.cs
- Hex.cs
- QueryOutputWriterV1.cs
- _UncName.cs
- BaseTemplateBuildProvider.cs
- DataGridCellAutomationPeer.cs
- DataControlCommands.cs
- TypeForwardedToAttribute.cs
- OledbConnectionStringbuilder.cs
- LinkConverter.cs
- DbConnectionPoolGroup.cs
- ContentElementAutomationPeer.cs
- BindingOperations.cs
- DataGridColumnCollection.cs
- ActivityCodeGenerator.cs
- PolicyLevel.cs
- TrackingLocation.cs
- PeerPresenceInfo.cs
- PropertyValueChangedEvent.cs
- LinkUtilities.cs
- UniqueSet.cs
- LookupNode.cs
- TableStyle.cs
- CompilerError.cs
- VerificationAttribute.cs
- KeyTime.cs
- GridToolTip.cs
- odbcmetadatafactory.cs
- QilPatternFactory.cs
- DbDataReader.cs
- TextSearch.cs
- Figure.cs
- ToolStripStatusLabel.cs
- Compiler.cs
- configsystem.cs
- Normalizer.cs
- QilTypeChecker.cs
- MatrixUtil.cs
- HeaderedItemsControl.cs
- CellTreeSimplifier.cs
- XmlSchemaException.cs
- DataListItemEventArgs.cs
- WebPartEditVerb.cs
- ServiceModelActivationSectionGroup.cs
- DeclarativeCatalogPart.cs
- XmlSchemaCompilationSettings.cs
- securitymgrsite.cs
- InterleavedZipPartStream.cs
- TabControlAutomationPeer.cs
- XslCompiledTransform.cs
- _SSPIWrapper.cs
- Msmq4PoisonHandler.cs
- ChtmlPhoneCallAdapter.cs
- ThousandthOfEmRealPoints.cs
- AdornerPresentationContext.cs
- PackageDigitalSignature.cs
- TCEAdapterGenerator.cs
- GeneralTransform2DTo3D.cs
- LineGeometry.cs
- SqlCharStream.cs
- CellCreator.cs
- PageFunction.cs
- Socket.cs
- TraceUtility.cs
- FilterQueryOptionExpression.cs
- DragDropHelper.cs
- MonitorWrapper.cs
- SafePEFileHandle.cs
- Matrix.cs
- TypeDescriptionProvider.cs
- ProtocolElement.cs
- DataGridViewRowHeaderCell.cs
- RegisteredHiddenField.cs
- InputLanguageManager.cs
- log.cs
- View.cs
- ZoneMembershipCondition.cs
- HandlerBase.cs
- IndexOutOfRangeException.cs
- DataGridViewColumnStateChangedEventArgs.cs
- Group.cs
- DataBoundControlDesigner.cs
- StylusDownEventArgs.cs
- HttpListenerException.cs
- SqlNamer.cs
- TransformCollection.cs