如何获取我的应用程序的父进程PID

32

我的winform应用程序是由另一个应用程序(父应用程序)启动的,我需要使用C#确定启动我的应用程序的应用程序的PID。


1
参见 https://dev59.com/HnRC5IYBdhLWcg3wJNqf - hortman
4个回答

69

在C#中,WMI是更容易实现此操作的方法。Win32_Process类具有ParentProcessId属性。以下是一个示例:

using System;
using System.Management;  // <=== Add Reference required!!
using System.Diagnostics;

class Program {
    public static void Main() {
        var myId = Process.GetCurrentProcess().Id;
        var query = string.Format("SELECT ParentProcessId FROM Win32_Process WHERE ProcessId = {0}", myId);
        var search = new ManagementObjectSearcher("root\\CIMV2", query);
        var results = search.Get().GetEnumerator();
        results.MoveNext();
        var queryObj = results.Current;
        var parentId = (uint)queryObj["ParentProcessId"];
        var parent = Process.GetProcessById((int)parentId);
        Console.WriteLine("I was started by {0}", parent.ProcessName);
        Console.ReadLine();
    }
}

从Visual Studio运行时的输出:

我是由devenv启动的


11

参考 Brian R. Bondy 的答案、PInvoke.net 上的内容以及一些 Reflector 输出,我编写了以下代码,用于在 LinqPad 中使用:MyExtensions

using Microsoft.Win32.SafeHandles;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.ConstrainedExecution;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Permissions;

static class ProcessExtensions
{
    public static int ParentProcessId(this Process process)
    {
      return ParentProcessId(process.Id);
    }
    public static int ParentProcessId(int Id)
    {
        PROCESSENTRY32 pe32 = new PROCESSENTRY32{};
        pe32.dwSize = (uint)Marshal.SizeOf(typeof(PROCESSENTRY32));
        using(var hSnapshot = CreateToolhelp32Snapshot(SnapshotFlags.Process, (uint)Id))
        {
            if(hSnapshot.IsInvalid)
                throw new Win32Exception();

            if(!Process32First(hSnapshot, ref pe32))
            {
                int errno = Marshal.GetLastWin32Error();
                if(errno == ERROR_NO_MORE_FILES)
                    return -1;
                throw new Win32Exception(errno);
            }    
            do {
                    if(pe32.th32ProcessID == (uint)Id)
                        return (int)pe32.th32ParentProcessID;
            } while(Process32Next(hSnapshot, ref pe32));
        }
        return -1;
    }
    private const int ERROR_NO_MORE_FILES = 0x12;
    [DllImport("kernel32.dll", SetLastError=true)]
    private static extern SafeSnapshotHandle CreateToolhelp32Snapshot(SnapshotFlags flags, uint id);
    [DllImport("kernel32.dll", SetLastError=true)]
    private static extern bool Process32First(SafeSnapshotHandle hSnapshot, ref PROCESSENTRY32 lppe);
    [DllImport("kernel32.dll", SetLastError=true)]
    private static extern bool Process32Next(SafeSnapshotHandle hSnapshot, ref PROCESSENTRY32 lppe);

    [Flags]
    private enum SnapshotFlags : uint
    {
        HeapList = 0x00000001,
        Process  = 0x00000002,
        Thread   = 0x00000004,
        Module   = 0x00000008,
        Module32 = 0x00000010,
        All      = (HeapList | Process | Thread | Module),
        Inherit  = 0x80000000,
        NoHeaps  = 0x40000000
    }
    [StructLayout(LayoutKind.Sequential)]
    private struct PROCESSENTRY32 
    { 
      public uint dwSize; 
      public uint cntUsage; 
      public uint th32ProcessID; 
      public IntPtr th32DefaultHeapID; 
      public uint th32ModuleID; 
      public uint cntThreads; 
      public uint th32ParentProcessID; 
      public int pcPriClassBase; 
      public uint dwFlags; 
      [MarshalAs(UnmanagedType.ByValTStr, SizeConst=260)] public string szExeFile; 
    };
    [SuppressUnmanagedCodeSecurity, HostProtection(SecurityAction.LinkDemand, MayLeakOnAbort=true)]
    internal sealed class SafeSnapshotHandle : SafeHandleMinusOneIsInvalid
    {
        internal SafeSnapshotHandle() : base(true)
        {
        }

        [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode=true)]
        internal SafeSnapshotHandle(IntPtr handle) : base(true)
        {
            base.SetHandle(handle);
        }

        protected override bool ReleaseHandle()
        {
            return CloseHandle(base.handle);
        }

        [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success), DllImport("kernel32.dll", CharSet=CharSet.Auto, SetLastError=true, ExactSpelling=true)]
        private static extern bool CloseHandle(IntPtr handle);
    }
}

7
如果您有控制父应用程序的权限,您可以修改父应用程序,在启动进程时将其PID传递给子进程。

2

检查CreateToolhelp32Snapshot枚举的th32ParentProcessID成员。

有关如何执行此操作的示例,请参见此处的我的帖子

但由于您正在使用C#,因此需要使用DllImports。在链接的帖子中,每个所需函数的MSDN页面都有。在每个MSDN页面的底部,它们都有用于C#的DllImport代码。

还有一种更简单的方法只使用托管代码,但如果不同应用程序启动了多个进程名称,则无法正常工作。


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