From 649ff285eec4222ff29573ead1f250dfc84c45a2 Mon Sep 17 00:00:00 2001 From: Ilya Date: Tue, 8 Aug 2017 20:20:57 +0400 Subject: [PATCH] Clean up '#if CORE' in ProcessCodeMethods.cs and 'ClrFacade.GetSafeProcessHandle' (#4428) --- .../commands/management/Process.cs | 40 +++++++++---------- .../engine/ProcessCodeMethods.cs | 5 +-- .../namespaces/Win32Native.cs | 2 +- .../utils/ClrFacade.cs | 14 ------- 4 files changed, 22 insertions(+), 39 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs index c231e762e9..f359b83004 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/Process.cs @@ -677,7 +677,7 @@ namespace Microsoft.PowerShell.Commands } else if (Module.IsPresent) { - //if only modules are to be displayed + //if only modules are to be displayed try { WriteObject(process.Modules, true); @@ -800,7 +800,7 @@ namespace Microsoft.PowerShell.Commands do { int error; - if (!Win32Native.OpenProcessToken(ClrFacade.GetSafeProcessHandle(process), TOKEN_QUERY, out processTokenHandler)) { break; } + if (!Win32Native.OpenProcessToken(process.Handle, TOKEN_QUERY, out processTokenHandler)) { break; } // Set the default length to be 256, so it will be sufficient for most cases int tokenInfoLength = 256; @@ -1196,7 +1196,7 @@ namespace Microsoft.PowerShell.Commands //Addition by v-ramch Mar 18 2008 - //Added force parameter + //Added force parameter /// /// Specifies whether to force a process to kill /// even if it has dependent services. @@ -1235,7 +1235,7 @@ namespace Microsoft.PowerShell.Commands try { - // Many properties including Name are not available if the process has exited. + // Many properties including Name are not available if the process has exited. // If this is the case, we skip the process. If the process is from a remote // machine, then we generate a non-terminating error because .NET doesn't support // terminate a remote process. @@ -1370,7 +1370,7 @@ namespace Microsoft.PowerShell.Commands IntPtr ph = IntPtr.Zero; try { - if (Win32Native.OpenProcessToken(ClrFacade.GetSafeProcessHandle(process), TOKEN_QUERY, out ph)) + if (Win32Native.OpenProcessToken(process.Handle, TOKEN_QUERY, out ph)) { if (_currentUserName == null) { @@ -1423,7 +1423,7 @@ namespace Microsoft.PowerShell.Commands string serviceName = oService.CimInstanceProperties["Name"].Value.ToString(); using (var service = new System.ServiceProcess.ServiceController(serviceName)) { - //try stopping the service, if cant we are not writing exception + //try stopping the service, if cant we are not writing exception try { service.Stop(); @@ -1782,7 +1782,7 @@ namespace Microsoft.PowerShell.Commands /// - /// Redirect input + /// Redirect input /// [Parameter(ParameterSetName = "Default")] [Alias("RSI")] @@ -1800,7 +1800,7 @@ namespace Microsoft.PowerShell.Commands /// - /// Redirect output + /// Redirect output /// [Parameter(ParameterSetName = "Default")] [Alias("RSO")] @@ -1820,7 +1820,7 @@ namespace Microsoft.PowerShell.Commands /// Verb /// /// - /// The 'Verb' parameter is not supported in OneCore PowerShell + /// The 'Verb' parameter is not supported in OneCore PowerShell /// because 'UseShellExecute' is not supported in CoreCLR. /// [Parameter(ParameterSetName = "UseShellExecute")] @@ -2155,7 +2155,7 @@ namespace Microsoft.PowerShell.Commands #region Private Methods /// - /// When Process exits the wait handle is set. + /// When Process exits the wait handle is set. /// private void myProcess_Exited(object sender, System.EventArgs e) { @@ -2449,7 +2449,7 @@ namespace Microsoft.PowerShell.Commands { lpStartupInfo.hStdError = new SafeFileHandle(ProcessNativeMethods.GetStdHandle(-12), false); } - //STARTF_USESTDHANDLES + //STARTF_USESTDHANDLES lpStartupInfo.dwFlags = 0x100; int creationFlags = 0; @@ -2529,7 +2529,7 @@ namespace Microsoft.PowerShell.Commands } else if (error == 0x424) { - // The API 'CreateProcessWithLogonW' depends on the 'Secondary Logon' service, but the component 'Microsoft-Windows-SecondaryLogonService' + // The API 'CreateProcessWithLogonW' depends on the 'Secondary Logon' service, but the component 'Microsoft-Windows-SecondaryLogonService' // is not installed in OneCoreUAP. We will get error code 0x424 when the service is not available. message = StringUtil.Format(ProcessResources.ParameterNotSupported, "-Credential", "Start-Process"); er = new ErrorRecord(new NotSupportedException(message), "NotSupportedException", ErrorCategory.NotInstalled, null); @@ -2607,7 +2607,7 @@ namespace Microsoft.PowerShell.Commands ErrorRecord er = new ErrorRecord(new InvalidOperationException(message), "InvalidOperationException", ErrorCategory.InvalidOperation, null); ThrowTerminatingError(er); } - return result; + return result; } #endif #endregion @@ -2621,7 +2621,7 @@ namespace Microsoft.PowerShell.Commands internal class ProcessCollection { /// - /// JobObjectHandle is a reference to the job object used to track + /// JobObjectHandle is a reference to the job object used to track /// the child processes created by the main process hosted by the Start-Process cmdlet. /// private Microsoft.PowerShell.Commands.SafeJobHandle _jobObjectHandle; @@ -2636,7 +2636,7 @@ namespace Microsoft.PowerShell.Commands } /// - /// Start API assigns the process to the JobObject and starts monitoring + /// Start API assigns the process to the JobObject and starts monitoring /// the child processes hosted by the process created by Start-Process cmdlet. /// internal bool AssignProcessToJobObject(Process process) @@ -2688,8 +2688,8 @@ namespace Microsoft.PowerShell.Commands } /// - /// JOBOBJECT_BASIC_PROCESS_ID_LIST Contains the process identifier list for a job object. - /// If the job is nested, the process identifier list consists of all + /// JOBOBJECT_BASIC_PROCESS_ID_LIST Contains the process identifier list for a job object. + /// If the job is nested, the process identifier list consists of all /// processes associated with the job and its child jobs. /// [StructLayout(LayoutKind.Sequential)] @@ -2701,14 +2701,14 @@ namespace Microsoft.PowerShell.Commands public uint NumberOfAssignedProcess; /// - /// The number of process identifiers returned in the ProcessIdList buffer. - /// If this number is less than NumberOfAssignedProcesses, increase + /// The number of process identifiers returned in the ProcessIdList buffer. + /// If this number is less than NumberOfAssignedProcesses, increase /// the size of the buffer to accommodate the complete list. /// public uint NumberOfProcessIdsInList; /// - /// A variable-length array of process identifiers returned by this call. + /// A variable-length array of process identifiers returned by this call. /// Array elements 0 through NumberOfProcessIdsInList minus 1 /// contain valid process identifiers. /// diff --git a/src/System.Management.Automation/engine/ProcessCodeMethods.cs b/src/System.Management.Automation/engine/ProcessCodeMethods.cs index 3cc5c2c67f..3349a1fcdf 100644 --- a/src/System.Management.Automation/engine/ProcessCodeMethods.cs +++ b/src/System.Management.Automation/engine/ProcessCodeMethods.cs @@ -58,11 +58,8 @@ namespace Microsoft.PowerShell { Diagnostics.Assert(process != null, "Ensure process is not null before calling"); PROCESS_BASIC_INFORMATION pbi; int size; -#if CORECLR - var res = NtQueryInformationProcess(process.SafeHandle.DangerousGetHandle(), 0, out pbi, Marshal.SizeOf(), out size); -#else var res = NtQueryInformationProcess(process.Handle, 0, out pbi, Marshal.SizeOf(), out size); -#endif + return res != 0 ? InvalidProcessId : pbi.InheritedFromUniqueProcessId.ToInt32(); } diff --git a/src/System.Management.Automation/namespaces/Win32Native.cs b/src/System.Management.Automation/namespaces/Win32Native.cs index d8352f5168..3dfbdf4563 100644 --- a/src/System.Management.Automation/namespaces/Win32Native.cs +++ b/src/System.Management.Automation/namespaces/Win32Native.cs @@ -140,7 +140,7 @@ namespace Microsoft.PowerShell.Commands.Internal [ResourceExposure(ResourceScope.Machine)] [SuppressMessage("Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage")] [return: MarshalAs(UnmanagedType.Bool)] - internal static extern bool OpenProcessToken(SafeHandle processHandle, uint desiredAccess, out IntPtr tokenHandle); + internal static extern bool OpenProcessToken(IntPtr processHandle, uint desiredAccess, out IntPtr tokenHandle); /// /// The GetTokenInformation function retrieves a specified type of information about an access token. diff --git a/src/System.Management.Automation/utils/ClrFacade.cs b/src/System.Management.Automation/utils/ClrFacade.cs index 69058bfa37..7044465b05 100644 --- a/src/System.Management.Automation/utils/ClrFacade.cs +++ b/src/System.Management.Automation/utils/ClrFacade.cs @@ -47,20 +47,6 @@ namespace System.Management.Automation /// internal static readonly char FIRST_CHAR_PSASSEMBLY_MARK = (char)0x29f9; - #region Process - - /// - /// Facade for Process.Handle to get SafeHandle - /// - /// - /// SafeHandle - internal static SafeHandle GetSafeProcessHandle(Process process) - { - return process.SafeHandle; - } - - #endregion Process - #region Assembly internal static IEnumerable GetAssemblies(TypeResolutionState typeResolutionState, TypeName typeName)