Code:
/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / WCF / infocard / Service / managed / Microsoft / InfoCards / ProcessManager.cs / 1 / ProcessManager.cs
//------------------------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- namespace Microsoft.InfoCards { using System; using System.IO; using System.Text; using Microsoft.Win32; using System.Diagnostics; // Process using System.Collections.Generic; using System.Security.Principal; //WindowsIdentity using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.ComponentModel; using Microsoft.InfoCards; using Microsoft.InfoCards.Diagnostics; using IDT = Microsoft.InfoCards.Diagnostics.InfoCardTrace; // // Summary: // Manages a collection of processes. Uses a job object where possible, and otherwise manages // the collection of processes itself. // class ProcessManager : IDisposable { const int WindowsVistaMajorVersion = 6; const string s_OnScreenKeyboardExeName = "osk"; const string s_SwitcherExeName = "msswchx"; const string s_TcserverExeName = "tcserver"; // // Job handle to use if possible. Triggered off the user being on the console session // SafeJobHandle m_hJob; // // List of processes to manage. // Listm_processList; // // The users ts session id. // uint m_userSessionId; // // The name of the trusted User Sid // private string m_trustedUserSid; // // Summary: // Construct a process manager for the given session. // public ProcessManager( int userSessionId, string trustedUserSid ) { m_hJob = null; m_userSessionId = (uint) userSessionId; m_processList = new List (); m_trustedUserSid = trustedUserSid; } internal bool IsConsoleOrVista { get { return ( m_userSessionId == NativeMethods.WTSGetActiveConsoleSessionId() || Environment.OSVersion.Version.Major >= WindowsVistaMajorVersion ) ; } } internal bool IsXPTablet { get { return ((Environment.OSVersion.Version.Major < WindowsVistaMajorVersion) && (0 != NativeMethods.GetSystemMetrics( NativeMethods.SM_TABLETPC ))); } } // Summary: // Spawns/creates the indicated process and then adds it to a job. // The job is created if needed. We add all the processes launched on // the secure desktop to a job as this allows us to easily terminate // the job when we need to switch back to the user desktop and stop/kill // all the processes we launched on the user desktop easily. // // Parameters: // hTrustedUserToken - Token with the trusted user SID allowed to be owner. // trustedUserSid - Stringized SID, used in the ACL for the process. // infocardDesktop - Desktop to launch the apps on. // userIdentity - Identity of caller. // fullpath - fullpath to the application being launched. // public void AddProcess( SafeNativeHandle hTrustedUserToken, ref string trustedUserSid, string infocardDesktop, uint userProcessId, WindowsIdentity userIdentity, string fullPath, string commandLine, bool fUseElevatedToken ) { IntPtr processHandle = IntPtr.Zero; int processId = 0; bool mustRelease = false; bool releaseJob = false; RuntimeHelpers.PrepareConstrainedRegions(); try { if( IsConsoleOrVista && null == m_hJob ) { m_hJob = Utility.CreateJobObjectWithSdHelper( trustedUserSid ); if( null == m_hJob ) { throw IDT.ThrowHelperError( new Win32Exception( Marshal.GetLastWin32Error() )); } } // // CreateJobObject will return NULL in case of failure // hTrustedUserToken.DangerousAddRef( ref mustRelease ); IntPtr trustedUserToken = hTrustedUserToken.DangerousGetHandle(); IntPtr handleJob = IntPtr.Zero; if( IsConsoleOrVista ) { m_hJob.DangerousAddRef( ref releaseJob ); handleJob = m_hJob.DangerousGetHandle(); } // // The process is assigned to the job if a job is passed in. // int error = (int) NativeMcppMethods.CreateProcessAsTrustedUserWrapper( fullPath, (null == commandLine) ? "" : commandLine, userProcessId, infocardDesktop, userIdentity.Name, m_userSessionId, ref trustedUserToken, ref processHandle, ref processId, handleJob, ref trustedUserSid, fUseElevatedToken ); if( 0 == error ) { using( SafeNativeHandle hProcess = new SafeNativeHandle( processHandle, true ) ) { if( !IsConsoleOrVista ) { m_processList.Add( Process.GetProcessById( processId ) ); } hProcess.Dispose(); IDT.TraceDebug( "ICARDACCESS:{0} launched for user {1}", fullPath, userIdentity.User.Value ); } } else { IDT.TraceDebug( "ICARDACCESS:Failed to create process with error {0}", error ); } } finally { if ( mustRelease ) { hTrustedUserToken.DangerousRelease(); } if( releaseJob ) { m_hJob.DangerousRelease(); } } } public void Dispose() { using( SystemIdentity lsa = new SystemIdentity( false ) ) { if( IsConsoleOrVista ) { if( null != m_hJob ) { m_hJob.Dispose(); m_hJob = null; } } else { // // Used to indicate that the on screen keyboard switcher // watching exe needs to be killed. // bool fKillSwitcher = false; for( int i = 0; i < m_processList.Count; i++ ) { Process p = m_processList[ i ]; if( !p.HasExited ) { try { if( m_userSessionId == p.SessionId ) { if( 0 == String.Compare( p.ProcessName, s_OnScreenKeyboardExeName, StringComparison.OrdinalIgnoreCase ) ) { // // We need to kill the msswchx.exe. // fKillSwitcher = true; } // // Utility will handle exceptions correctly. // Utility.KillHelper( p ); } } catch( InvalidOperationException ) { // // If the process has already terminated then, we can get // this exception. // ; } } } // // Kill switcher if needed, if we are not in a job we need // to handle this as a special case. // if( fKillSwitcher ) { foreach( Process p in Process.GetProcessesByName( s_SwitcherExeName ) ) { if( m_userSessionId == p.SessionId ) { if( !p.HasExited ) { // // Utility will handle exceptions correctly. // Utility.KillHelper( p ); } IDT.TraceDebug( "ICARDACCESS:Killed msswchx.exe on IC desktop.}" ); break; } ((IDisposable)p).Dispose(); } } m_processList.Clear(); } } // // tcserver.exe has a handle to our desktop, and will trap the user on the infocard desktop unless we explicitly kill it. // if ( IsXPTablet ) { KillTcserverInstancesForInfoCardDesktop(); } } // // Retrieves all instances of tcserver.exe that are associated with the infocard desktop and terminates them. // private void KillTcserverInstancesForInfoCardDesktop() { // // Get all instances of tcserver and check to see if the sid matches. // foreach ( Process p in Process.GetProcessesByName( s_TcserverExeName ) ) { if ( NativeMcppMethods.IsCardSpaceTcserverInstance( p.Id, m_trustedUserSid ) ) { Utility.KillHelper( p ); } ((IDisposable)p).Dispose(); } } } } // 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
- ValidationRule.cs
- ContentFileHelper.cs
- QuaternionAnimation.cs
- ObjectDataSource.cs
- EdmRelationshipNavigationPropertyAttribute.cs
- ImageMapEventArgs.cs
- XmlAggregates.cs
- WebEventCodes.cs
- GridItemPattern.cs
- UInt64Storage.cs
- rsa.cs
- BuildResultCache.cs
- _HeaderInfo.cs
- WebServiceClientProxyGenerator.cs
- PersonalizationProvider.cs
- Control.cs
- WebPartTransformer.cs
- IItemProperties.cs
- TypeUtils.cs
- SafeFileHandle.cs
- DetailsViewUpdatedEventArgs.cs
- PersonalizationProviderCollection.cs
- InstanceDataCollectionCollection.cs
- BaseComponentEditor.cs
- FlowNode.cs
- ParseChildrenAsPropertiesAttribute.cs
- LineServicesCallbacks.cs
- MappingMetadataHelper.cs
- ReadOnlyObservableCollection.cs
- TrustSection.cs
- Thread.cs
- HScrollBar.cs
- BitVector32.cs
- TypeNameConverter.cs
- ConfigurationManagerHelper.cs
- NotifyParentPropertyAttribute.cs
- TreeNodeBindingCollection.cs
- UserNameSecurityTokenProvider.cs
- HtmlInputPassword.cs
- translator.cs
- DefaultHttpHandler.cs
- DictionaryBase.cs
- ValueTypePropertyReference.cs
- MappedMetaModel.cs
- DocobjHost.cs
- AutomationElementIdentifiers.cs
- ReferenceSchema.cs
- WebPartCloseVerb.cs
- TextWriter.cs
- XmlSchemaAll.cs
- SuppressMessageAttribute.cs
- QilXmlWriter.cs
- WsdlImporterElementCollection.cs
- DebugController.cs
- PreProcessInputEventArgs.cs
- validation.cs
- ToolStripItemClickedEventArgs.cs
- OleDbWrapper.cs
- ToolStripLocationCancelEventArgs.cs
- TimeZone.cs
- FunctionDetailsReader.cs
- GridPatternIdentifiers.cs
- ToolStripContainer.cs
- InvalidChannelBindingException.cs
- DropDownList.cs
- EditorZoneDesigner.cs
- RoutedEventConverter.cs
- Int16KeyFrameCollection.cs
- XmlDeclaration.cs
- ToolStripItemGlyph.cs
- ExpressionParser.cs
- Parameter.cs
- Decorator.cs
- SvcMapFileSerializer.cs
- XmlArrayAttribute.cs
- DataSetViewSchema.cs
- DataBindingHandlerAttribute.cs
- ParallelTimeline.cs
- FilterableData.cs
- Bezier.cs
- FileFormatException.cs
- webclient.cs
- ToolStripDesignerUtils.cs
- GlobalProxySelection.cs
- Label.cs
- DataGridLinkButton.cs
- DesignSurfaceServiceContainer.cs
- LinkedResource.cs
- XmlAttributeCollection.cs
- DiscreteKeyFrames.cs
- Panel.cs
- SynchronizedInputAdaptor.cs
- WebSysDisplayNameAttribute.cs
- ClearCollection.cs
- DataFormats.cs
- VideoDrawing.cs
- NonVisualControlAttribute.cs
- TextTreeInsertUndoUnit.cs
- TreeViewImageKeyConverter.cs
- XmlDownloadManager.cs