Code:
/ DotNET / DotNET / 8.0 / untmp / WIN_WINDOWS / lh_tools_devdiv_wpf / Windows / wcp / Core / System / Windows / Media / FactoryMaker.cs / 1 / 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. /// /// Note that for now there is no SUC on ReleaseInterface. We may want to re-consider this. /// [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) { new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Assert(); // BlessedAssert: try { UnsafeNativeMethods.MILUnknown.ReleaseInterface(ref s_pFactory); if (s_pImagingFactory != IntPtr.Zero) { UnsafeNativeMethods.MILUnknown.ReleaseInterface(ref s_pImagingFactory); } } finally { CodeAccessPermission.RevertAssert(); 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; } } ////// milcore.dll has a lower layer number than wmp.dll so it cannot /// depend on wmp.dll. We get around this by registering a factory /// which instantiates the WMP OCX for milcore.dll. Whenever milcore /// needs a copy of the WMP OCX, it calls CreateWmpOcx on the factory /// object, which calls CoCreateInstance and returns a pointer to /// IWMPPlayer. The factory object lives in presentationnative.dll /// ////// Critical - Create unmanaged critical resource /// [SecurityCritical] internal static void RegisterWmpFactory() { lock (s_factoryMakerLock) { if (!s_wmpFactoryRegistered) { int hr = 0; SafeMILHandle wmpFactory; hr = UnsafeNativeMethods.MILFactory2.CreateWmpFactory( out wmpFactory); HRESULT.Check(hr); hr = UnsafeNativeMethods.MILFactory2.RegisterWmpFactory( wmpFactory); HRESULT.Check(hr); s_wmpFactoryRegistered = true; } } } ////// 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; private static bool s_wmpFactoryRegistered = false; ////// "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
- XmlSchemaCollection.cs
- RepeaterItemCollection.cs
- WindowsAuthenticationEventArgs.cs
- ReliableChannelFactory.cs
- ArraySegment.cs
- InstanceContextManager.cs
- CalendarData.cs
- BinHexEncoder.cs
- MsmqIntegrationBindingElement.cs
- CodeArrayIndexerExpression.cs
- HtmlInputFile.cs
- StreamResourceInfo.cs
- ImageButton.cs
- Function.cs
- HWStack.cs
- InputMethodStateTypeInfo.cs
- InvokeGenerator.cs
- DataGridAutoFormat.cs
- WsdlInspector.cs
- CorrelationManager.cs
- StylusPlugin.cs
- ActivationServices.cs
- TraceInternal.cs
- RootNamespaceAttribute.cs
- EmbeddedObject.cs
- LogWriteRestartAreaState.cs
- ListQueryResults.cs
- CompositeDuplexBindingElementImporter.cs
- SystemEvents.cs
- WebPartEventArgs.cs
- ColumnTypeConverter.cs
- SmtpSection.cs
- ClosableStream.cs
- DescendentsWalkerBase.cs
- ValueType.cs
- InstanceLockLostException.cs
- ColorTransformHelper.cs
- FloatUtil.cs
- ToolStripProgressBar.cs
- SqlUdtInfo.cs
- ChannelRequirements.cs
- InstallerTypeAttribute.cs
- RulePatternOps.cs
- DataGridAutomationPeer.cs
- PeerOutputChannel.cs
- NumberAction.cs
- GlobalizationSection.cs
- SizeF.cs
- Binding.cs
- SecurityCriticalDataForSet.cs
- StringInfo.cs
- ReflectTypeDescriptionProvider.cs
- TraceHandlerErrorFormatter.cs
- TagPrefixAttribute.cs
- VirtualDirectoryMappingCollection.cs
- BinaryObjectInfo.cs
- BinaryConverter.cs
- UiaCoreApi.cs
- ByteKeyFrameCollection.cs
- TextTreeRootNode.cs
- NTAccount.cs
- JsonWriterDelegator.cs
- Soap.cs
- NetCodeGroup.cs
- ResourceSet.cs
- ThreadTrace.cs
- ExtensionFile.cs
- Operand.cs
- ComboBoxItem.cs
- WorkflowFileItem.cs
- BridgeDataRecord.cs
- TaiwanCalendar.cs
- BitmapEffectDrawing.cs
- AssemblyAttributes.cs
- ActivationServices.cs
- CompilerInfo.cs
- CompatibleIComparer.cs
- XNodeNavigator.cs
- ObjectHelper.cs
- ConfigurationStrings.cs
- WebBaseEventKeyComparer.cs
- OptimizerPatterns.cs
- LocalValueEnumerator.cs
- PlainXmlSerializer.cs
- CodeGotoStatement.cs
- SortKey.cs
- DeobfuscatingStream.cs
- GlyphRunDrawing.cs
- CodeTypeParameter.cs
- AnimatedTypeHelpers.cs
- GlobalEventManager.cs
- TreeNodeClickEventArgs.cs
- MessageSecurityProtocolFactory.cs
- DynamicMethod.cs
- ApplicationServiceManager.cs
- ContextMenuStrip.cs
- DataColumnChangeEvent.cs
- PrimaryKeyTypeConverter.cs
- ScrollProperties.cs
- CacheMemory.cs