Skip to content
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
dd7f67b
Implementation
SteveDunn Sep 27, 2021
41e0197
Merge branch 'dotnet:main' into issue-55685-processname-optimisation
SteveDunn Sep 27, 2021
19b9126
Get short name after calling Kernel32.GetProcessName
SteveDunn Sep 28, 2021
8955b32
PR feedback
SteveDunn Sep 28, 2021
901e8f3
PR feedback
SteveDunn Sep 28, 2021
47262da
PR feedback
SteveDunn Sep 29, 2021
8689265
Removed commented out code
SteveDunn Oct 8, 2021
ac70497
PR feedback
SteveDunn Oct 8, 2021
c5a2790
If processinfo is already available, use that, otherwise use the quic…
SteveDunn Oct 10, 2021
9f69e34
PR feedback
SteveDunn Oct 11, 2021
38e3d14
PR feedback
SteveDunn Oct 11, 2021
3227f87
Update src/libraries/Common/src/Interop/Windows/Kernel32/Interop.GetP…
SteveDunn Dec 7, 2021
f07a0bc
Update src/libraries/Common/src/Interop/Windows/Kernel32/Interop.GetP…
SteveDunn Dec 7, 2021
3728a1f
Update src/libraries/System.Diagnostics.Process/src/System/Diagnostic…
SteveDunn Dec 7, 2021
fcb47a9
Update src/libraries/System.Diagnostics.Process/src/System/Diagnostic…
SteveDunn Dec 7, 2021
23cd323
Update src/libraries/System.Diagnostics.Process/src/System/Diagnostic…
SteveDunn Dec 7, 2021
d4c2e86
Update src/libraries/System.Diagnostics.Process/src/System/Diagnostic…
SteveDunn Dec 7, 2021
96b88b4
Apply suggestions from code review
adamsitnik Dec 7, 2021
67732e4
handle ERROR_INSUFFICIENT_BUFFER
adamsitnik Dec 7, 2021
6676175
discard process name on Refresh
adamsitnik Dec 7, 2021
08ec6d0
Merge remote-tracking branch 'upstream/main' into issue-55685-process…
adamsitnik Dec 7, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.Win32.SafeHandles;

internal static partial class Interop
{
internal static partial class Kernel32
{
[DllImport(Libraries.Kernel32, CharSet = CharSet.Unicode, EntryPoint = "QueryFullProcessImageNameW", ExactSpelling = true)]
private static extern bool QueryFullProcessImageName(
SafeHandle hProcess,
uint dwFlags,
ref char lpBuffer,
ref uint lpdwSize);

private const int MAX_PROCESSNAME_LENGTH = 1024;

internal static string? GetProcessName(uint processId)
{
using (SafeProcessHandle h = Interop.Kernel32.OpenProcess(Interop.Advapi32.ProcessOptions.PROCESS_QUERY_LIMITED_INFORMATION, false, (int)processId))
{
Span<char> buffer = stackalloc char[MAX_PROCESSNAME_LENGTH + 1];
uint length = (uint)buffer.Length;

bool queried = QueryFullProcessImageName(
h,
0,
ref MemoryMarshal.GetReference(buffer),
ref length);

return queried ? buffer.Slice(0, (int)length).ToString() : null;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@
Link="Common\Interop\Windows\Advapi32\Interop.AdjustTokenPrivileges.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.GetComputerName.cs"
Link="Common\Interop\Windows\Kernel32\Interop.GetComputerName.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.GetProcessName.cs"
Link="Common\Interop\Windows\Kernel32\Interop.GetProcessName.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.GetConsoleCP.cs"
Link="Common\Interop\Windows\Kernel32\Interop.GetConsoleCP.cs" />
<Compile Include="$(CommonPath)Interop\Windows\Kernel32\Interop.GetConsoleOutputCP.cs"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1105,5 +1105,15 @@ private static int OnSigChild(int reapAll, int configureConsole)
s_processStartLock.ExitWriteLock();
}
}

/// <summary>Gets the friendly name of the process.</summary>
public string ProcessName
{
get
{
EnsureState(State.HaveProcessInfo);
return _processInfo!.ProcessName;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,14 @@ private string GetPathToOpenFile()
throw new PlatformNotSupportedException();
}

/// <summary>Gets the friendly name of the process.</summary>
public string ProcessName
{
get
{
EnsureState(State.HaveProcessInfo);
return _processInfo!.ProcessName;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public partial class Process : IDisposable
{
private static readonly object s_createProcessLock = new object();

private string? _processName;

/// <summary>
/// Creates an array of <see cref="Process"/> components that are associated with process resources on a
/// remote computer. These process resources share the specified process name.
Expand Down Expand Up @@ -885,5 +887,40 @@ private static string GetEnvironmentVariablesBlock(IDictionary<string, string> s
}

private static string GetErrorMessage(int error) => Interop.Kernel32.GetMessage(error);

/// <summary>Gets the friendly name of the process.</summary>
public string ProcessName
{
get
{
EnsureState(State.HaveNonExitedId);

if (_processName == null)
{
// If we already have the name via a populated ProcessInfo
// then use that one.
if (_processInfo?.ProcessName != null)
{
_processName = _processInfo!.ProcessName;
}
else
{

// If we don't have a populated ProcessInfo, then create and
// cache the process name.
EnsureState(State.HaveId);

_processName = ProcessManager.GetProcessName(_processId, _machineName);

if (_processName == null)
{
throw new InvalidOperationException(SR.NoProcessInfo);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you for keeping the exception consistent with existing implementation! 👍

}
}
}

return _processName;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -503,21 +503,6 @@ public int PrivateMemorySize
}
}

/// <devdoc>
/// <para>
/// Gets
/// the friendly name of the process.
/// </para>
/// </devdoc>
public string ProcessName
{
get
{
EnsureState(State.HaveProcessInfo);
return _processInfo!.ProcessName;
}
}

/// <devdoc>
/// <para>
/// Gets
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,39 @@ public static ProcessInfo[] GetProcessInfos(string machineName)
return null;
}

/// <summary>Gets the process name for the specified process ID on the specified machine.</summary>
/// <param name="processId">The process ID.</param>
/// <param name="machineName">The machine name.</param>
/// <returns>The process name for the process if it could be found; otherwise, null.</returns>
public static string? GetProcessName(int processId, string machineName)
{
if (IsRemoteMachine(machineName))
{
// remote case: we take the hit of looping through all results
ProcessInfo[] processInfos = NtProcessManager.GetProcessInfos(machineName, isRemoteMachine: true);
foreach (ProcessInfo processInfo in processInfos)
{
if (processInfo.ProcessId == processId)
{
return processInfo.ProcessName;
}
}
}
else
{
// local case: do not use performance counter and also attempt to get the matching (by pid) process only

string? processName = Interop.Kernel32.GetProcessName((uint)processId);
if (processName is not null)
{
return NtProcessInfoHelper.GetProcessShortName(processName);
}
}

return null;
}


Comment on lines +118 to +120
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: extra empty line

Suggested change
}
}

/// <summary>Gets the IDs of all processes on the specified machine.</summary>
/// <param name="machineName">The machine to examine.</param>
/// <returns>An array of process IDs from the specified machine.</returns>
Expand Down