跨平台获取父进程ID的方法

6
使用 .Net Standard,是否有一种跨平台的方法来获取进程的父进程 ID?
几个 Stack Overflow 的问题和答案介绍了如何在 Windows 中以特定方式(例如 How to get parent process in .NET in managed way)获取这些信息,通常使用 WMI 的 Win32_Process、PInvoke 或 PerformanceCounters。然而,在 .Net Standard 的跨平台世界中,这些方法都不起作用。
更新
目前似乎没有办法做到这一点。我创建了一些 GitHub 问题,建议将相关功能添加到 .Net Standard 中。
2个回答

0
总结在github上的讨论,有一个观点:在netCore中没有办法获取父进程的ID。
我很难过。

-1
public static int GetParentId(this Process process)
{
    PropertyInfo? parentPropertyInfo;

    try
    {
        // Property is available on most platforms, including Windows and Linux, but not on .NET Framework
        parentPropertyInfo = typeof(Process).GetProperty("ParentProcessId", BindingFlags.Instance | BindingFlags.NonPublic);
    }
    catch (AmbiguousMatchException)
    {
        parentPropertyInfo = null;
    }

    if (parentPropertyInfo != null)
    {
        try
        {
            return (int)parentPropertyInfo.GetValue(process);
        }
        catch (TargetInvocationException ex)
        {
            throw ex.InnerException ?? ex;
        }
    }

    if (Environment.OSVersion.Platform == PlatformID.Win32NT)
    {
        // Works on any Windows platform independent from .NET version
        return ProcessBasicInformation.GetParentProcessId(process);
    }

    throw new NotSupportedException("Your platform does not support querying the parent process id");
}

public class ProcessBasicInformation
{
    // Class for native method: Do NOT add or remove fields. Do NOT change the order of the fields
    private readonly IntPtr exitStatus;
    private readonly IntPtr processEnvironmentBlockBaseAddress;
    private readonly IntPtr affinityMask;
    private readonly IntPtr basePriority;
    private readonly IntPtr processId;
    private readonly IntPtr parentProcessId;

    public static int GetParentProcessId(Process process)
    {
        return FillFields(process).parentProcessId.ToInt32();
    }

    private static ProcessBasicInformation FillFields(Process process)
    {
        var processBasicInformation = new ProcessBasicInformation();
        NativeMethods.NtQueryInformationProcess(process, processBasicInformation);
        return processBasicInformation;
    }
}

public class NativeMethods
{
    public static void NtQueryInformationProcess(Process process, ProcessBasicInformation processInformation)
    {
        if (Environment.OSVersion.Platform != PlatformID.Win32NT)
        {
            throw new NotSupportedException($"{nameof(NtQueryInformationProcess)} only works on Windows");
        }

        var nativeStructureSize = Marshal.SizeOf<ProcessBasicInformation>();

        var status = TryNtQueryInformationProcess(process.Handle, 0, processInformation, nativeStructureSize, out var returnLength);

        if (status != 0)
        {
            throw new Win32Exception(status);
        }

        if (returnLength != nativeStructureSize)
        {
            throw new NotSupportedException("Your Windows version does not support getting the parent process id");
        }
    }

    [DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
    [DllImport("ntdll.dll", EntryPoint = "NtQueryInformationProcess")]
    private static extern int TryNtQueryInformationProcess(IntPtr processHandle, int processInformationClass, ProcessBasicInformation processInformation, int processInformationLength, out int returnLength);

这不是问题要求的。它仍然只适用于Windows。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接