Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / wpf / src / Core / CSharp / System / Windows / Media / FactoryMaker.cs / 1305600 / FactoryMaker.cs
//------------------------------------------------------------------------------ // Microsoft Avalon // Copyright (c) Microsoft Corporation, 2003 // // File: FactoryMaker.cs // //----------------------------------------------------------------------------- using System; using System.Security; using System.Security.Permissions; using System.Diagnostics; using MS.Internal; using MS.Win32; using System.Windows.Media.Composition; using Microsoft.Internal; using SR=MS.Internal.PresentationCore.SR; using SRID=MS.Internal.PresentationCore.SRID; using UnsafeNativeMethods=MS.Win32.PresentationCore.UnsafeNativeMethods; namespace System.Windows.Media { ////// This is a internal class which is used by non context affinity objects /// to get access to a MIL factory object. /// internal class FactoryMaker: IDisposable { private bool _disposed = false; ////// Critical - Create unmanaged critical resource /// [SecurityCritical] internal FactoryMaker() { lock (s_factoryMakerLock) { // If we haven't have a factory, create one if (s_pFactory == IntPtr.Zero) { // Create the Core MIL factory. // Note: the call below might throw exception. The caller // should catch it. We won't add ref counter here if this // happens. HRESULT.Check(UnsafeNativeMethods.MILFactory2.CreateFactory(out s_pFactory, MS.Internal.Composition.Version.MilSdkVersion)); } s_cInstance++; _fValidObject = true; } } ~FactoryMaker() { Dispose(false); } ////// Dispose of any resources /// public void Dispose() { Dispose(true); } ////// Critical - performs an elevation to call MILUnknown.ReleaseInterface. /// TreatAsSafe - this function elevates to call release ( on an object we own). /// net effect is a shutdown of the FactoryMaker. Considered safe. /// [SecurityCritical, SecurityTreatAsSafe] protected virtual void Dispose(bool fDisposing) { if (!_disposed) { if (_fValidObject == true) { lock (s_factoryMakerLock) { s_cInstance--; // Make sure we don't dispose twice _fValidObject = false; // If there is no FactoryMaker object out there, release // factory object if (s_cInstance == 0) { UnsafeNativeMethods.MILUnknown.ReleaseInterface(ref s_pFactory); if (s_pImagingFactory != IntPtr.Zero) { UnsafeNativeMethods.MILUnknown.ReleaseInterface(ref s_pImagingFactory); } s_pFactory = IntPtr.Zero; s_pImagingFactory = IntPtr.Zero; } } } // Set the sentinel. _disposed = true; // Suppress finalization of this disposed instance. if (fDisposing) { GC.SuppressFinalize(this); } } } ////// Critical - returns critical resource created under an assert /// internal IntPtr FactoryPtr { [SecurityCritical] get { Debug.Assert(s_pFactory != IntPtr.Zero); return s_pFactory; } } ////// Critical - calls unmanaged code, returns unmanaged pointer /// internal IntPtr ImagingFactoryPtr { [SecurityCritical] get { if (s_pImagingFactory == IntPtr.Zero) { lock (s_factoryMakerLock) { HRESULT.Check(UnsafeNativeMethods.WICCodec.CreateImagingFactory(UnsafeNativeMethods.WICCodec.WINCODEC_SDK_VERSION, out s_pImagingFactory)); } } Debug.Assert(s_pImagingFactory != IntPtr.Zero); return s_pImagingFactory; } } ////// Critical - this is a pointer to an unmanaged object that methods are called directly on /// [SecurityCritical] private static IntPtr s_pFactory; ////// Critical - this is a pointer to an unmanaged object that methods are called directly on /// [SecurityCritical] private static IntPtr s_pImagingFactory; ////// Keeps track of how many instance of current object have been passed out /// private static int s_cInstance = 0; ////// "FactoryMaker" is free threaded. This lock is used to synchronize /// access to the FactoryMaker. /// private static object s_factoryMakerLock = new object(); private bool _fValidObject; } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved. //------------------------------------------------------------------------------ // Microsoft Avalon // Copyright (c) Microsoft Corporation, 2003 // // File: FactoryMaker.cs // //----------------------------------------------------------------------------- using System; using System.Security; using System.Security.Permissions; using System.Diagnostics; using MS.Internal; using MS.Win32; using System.Windows.Media.Composition; using Microsoft.Internal; using SR=MS.Internal.PresentationCore.SR; using SRID=MS.Internal.PresentationCore.SRID; using UnsafeNativeMethods=MS.Win32.PresentationCore.UnsafeNativeMethods; namespace System.Windows.Media { ////// This is a internal class which is used by non context affinity objects /// to get access to a MIL factory object. /// internal class FactoryMaker: IDisposable { private bool _disposed = false; ////// Critical - Create unmanaged critical resource /// [SecurityCritical] internal FactoryMaker() { lock (s_factoryMakerLock) { // If we haven't have a factory, create one if (s_pFactory == IntPtr.Zero) { // Create the Core MIL factory. // Note: the call below might throw exception. The caller // should catch it. We won't add ref counter here if this // happens. HRESULT.Check(UnsafeNativeMethods.MILFactory2.CreateFactory(out s_pFactory, MS.Internal.Composition.Version.MilSdkVersion)); } s_cInstance++; _fValidObject = true; } } ~FactoryMaker() { Dispose(false); } ////// Dispose of any resources /// public void Dispose() { Dispose(true); } ////// Critical - performs an elevation to call MILUnknown.ReleaseInterface. /// TreatAsSafe - this function elevates to call release ( on an object we own). /// net effect is a shutdown of the FactoryMaker. Considered safe. /// [SecurityCritical, SecurityTreatAsSafe] protected virtual void Dispose(bool fDisposing) { if (!_disposed) { if (_fValidObject == true) { lock (s_factoryMakerLock) { s_cInstance--; // Make sure we don't dispose twice _fValidObject = false; // If there is no FactoryMaker object out there, release // factory object if (s_cInstance == 0) { UnsafeNativeMethods.MILUnknown.ReleaseInterface(ref s_pFactory); if (s_pImagingFactory != IntPtr.Zero) { UnsafeNativeMethods.MILUnknown.ReleaseInterface(ref s_pImagingFactory); } s_pFactory = IntPtr.Zero; s_pImagingFactory = IntPtr.Zero; } } } // Set the sentinel. _disposed = true; // Suppress finalization of this disposed instance. if (fDisposing) { GC.SuppressFinalize(this); } } } ////// Critical - returns critical resource created under an assert /// internal IntPtr FactoryPtr { [SecurityCritical] get { Debug.Assert(s_pFactory != IntPtr.Zero); return s_pFactory; } } ////// Critical - calls unmanaged code, returns unmanaged pointer /// internal IntPtr ImagingFactoryPtr { [SecurityCritical] get { if (s_pImagingFactory == IntPtr.Zero) { lock (s_factoryMakerLock) { HRESULT.Check(UnsafeNativeMethods.WICCodec.CreateImagingFactory(UnsafeNativeMethods.WICCodec.WINCODEC_SDK_VERSION, out s_pImagingFactory)); } } Debug.Assert(s_pImagingFactory != IntPtr.Zero); return s_pImagingFactory; } } ////// Critical - this is a pointer to an unmanaged object that methods are called directly on /// [SecurityCritical] private static IntPtr s_pFactory; ////// Critical - this is a pointer to an unmanaged object that methods are called directly on /// [SecurityCritical] private static IntPtr s_pImagingFactory; ////// Keeps track of how many instance of current object have been passed out /// private static int s_cInstance = 0; ////// "FactoryMaker" is free threaded. This lock is used to synchronize /// access to the FactoryMaker. /// private static object s_factoryMakerLock = new object(); private bool _fValidObject; } } // 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
- XsdDataContractImporter.cs
- SessionState.cs
- BulletedListEventArgs.cs
- MouseActionConverter.cs
- DataTemplateKey.cs
- CodeSnippetExpression.cs
- PointCollectionConverter.cs
- QuaternionRotation3D.cs
- PasswordDeriveBytes.cs
- XsltOutput.cs
- SqlDelegatedTransaction.cs
- COM2PropertyDescriptor.cs
- GAC.cs
- RowUpdatedEventArgs.cs
- QilSortKey.cs
- IERequestCache.cs
- _SslSessionsCache.cs
- ExpandCollapseIsCheckedConverter.cs
- SynchronizedInputHelper.cs
- TcpProcessProtocolHandler.cs
- CanExecuteRoutedEventArgs.cs
- TransportReplyChannelAcceptor.cs
- Accessible.cs
- CompositeControl.cs
- RijndaelManagedTransform.cs
- WindowClosedEventArgs.cs
- ObjectListCommandsPage.cs
- ScriptingRoleServiceSection.cs
- DataGridViewCellParsingEventArgs.cs
- StringArrayConverter.cs
- DataObject.cs
- CrossAppDomainChannel.cs
- ContainerUIElement3D.cs
- Calendar.cs
- DependencyObject.cs
- PreviewKeyDownEventArgs.cs
- CompareInfo.cs
- HtmlInputText.cs
- SqlParameterCollection.cs
- TextPattern.cs
- SHA384.cs
- TypeDescriptor.cs
- CatalogPartCollection.cs
- Journaling.cs
- wgx_render.cs
- ConnectionManagementSection.cs
- DrawListViewColumnHeaderEventArgs.cs
- GuidConverter.cs
- WebConfigurationFileMap.cs
- CreateUserErrorEventArgs.cs
- TemplateParser.cs
- FlowDocumentPageViewerAutomationPeer.cs
- ToolStripItemCollection.cs
- SoapSchemaExporter.cs
- SqlNotificationEventArgs.cs
- ActivityScheduledQuery.cs
- ProjectedWrapper.cs
- WebPartTracker.cs
- RegexMatch.cs
- DbXmlEnabledProviderManifest.cs
- TypedColumnHandler.cs
- TypeCacheManager.cs
- XsltSettings.cs
- QueryReaderSettings.cs
- ToolBarPanel.cs
- ObfuscateAssemblyAttribute.cs
- Size3DConverter.cs
- Comparer.cs
- DrawingDrawingContext.cs
- PointConverter.cs
- DuplexSecurityProtocolFactory.cs
- SimpleTypeResolver.cs
- CodePrimitiveExpression.cs
- UserValidatedEventArgs.cs
- ParserContext.cs
- MarshalByValueComponent.cs
- BuildResultCache.cs
- CheckBox.cs
- SynchronizationContext.cs
- MimeTypePropertyAttribute.cs
- AuthenticateEventArgs.cs
- AssemblyInfo.cs
- invalidudtexception.cs
- WebBrowserProgressChangedEventHandler.cs
- InputManager.cs
- SByteConverter.cs
- XmlTextWriter.cs
- TabPanel.cs
- OperatingSystem.cs
- WebPartConnectionsCancelVerb.cs
- SignatureToken.cs
- WebConfigurationManager.cs
- HttpDictionary.cs
- WebPartConnectionsConfigureVerb.cs
- TextServicesProperty.cs
- Html32TextWriter.cs
- DataGridViewSelectedRowCollection.cs
- MessageBox.cs
- WebHeaderCollection.cs
- TemplateParser.cs