/// <returns>Returns a list of PIDs corresponding to all running processes</returns>
internal static unsafe int[] ListAllPids()
{
- int numProcesses = 0;
int[] pids;
- kinfo_proc * entries = null;
+ kinfo_proc * entries = GetProcInfo(0, false, out int numProcesses);
int idx;
try
{
- entries = GetProcInfo(0, false, out numProcesses);
- if (entries == null || numProcesses <= 0)
+ if (numProcesses <= 0)
{
throw new Win32Exception(SR.CantGetAllPids);
}
/// </returns>
public static unsafe ProcessInfo GetProcessInfoById(int pid)
{
- kinfo_proc* kinfo = null;
- int count;
- ProcessInfo info;
-
// Negative PIDs are invalid
if (pid < 0)
{
throw new ArgumentOutOfRangeException(nameof(pid));
}
+ kinfo_proc* kinfo = GetProcInfo(pid, true, out int count);
+ ProcessInfo info;
+
try
{
- kinfo = GetProcInfo(pid, true, out count);
- if (kinfo == null || count < 1)
+ if (count < 1)
{
throw new ArgumentOutOfRangeException(nameof(pid));
}
using System.Collections.Generic;
using System.ComponentModel;
+using System.Runtime.InteropServices;
namespace System.Diagnostics
{
}
/// <summary>Gets parent process ID</summary>
- private int ParentProcessId =>
- throw new PlatformNotSupportedException();
+ private unsafe int ParentProcessId
+ {
+ get
+ {
+ EnsureState(State.HaveNonExitedId);
+
+ Interop.Process.kinfo_proc* processInfo = Interop.Process.GetProcInfo(_processId, false, out int count);
+ try
+ {
+ if (count <= 0)
+ {
+ throw new Win32Exception(SR.ProcessInformationUnavailable);
+ }
+
+ return processInfo->ki_ppid;
+ }
+ finally
+ {
+ Marshal.FreeHGlobal((IntPtr) processInfo);
+ }
+ }
+ }
// <summary>Gets execution path</summary>
private string GetPathToOpenFile()