示例#1
0
        public static int?GetNthParentPid(int pid, int order)
        {
            int?nullable;

            for (nullable = new int?(pid); order > 0 && nullable.HasValue; --order)
            {
                nullable = ProcessDetails.GetParentProcessId(nullable.Value);
            }
            return(nullable);
        }
示例#2
0
        public static Process GetParentProcess(int pid)
        {
            Process process = (Process)null;
            int     id      = Process.GetCurrentProcess().Id;
            IntPtr  num     = IntPtr.Zero;

            try
            {
                ProcessDetails.PROCESSENTRY32 lppe = new ProcessDetails.PROCESSENTRY32()
                {
                    dwSize = (uint)Marshal.SizeOf(typeof(ProcessDetails.PROCESSENTRY32))
                };
                num = ProcessDetails.CreateToolhelp32Snapshot(2U, 0U);
                if (ProcessDetails.Process32First(num, ref lppe))
                {
                    while ((long)pid != (long)lppe.th32ProcessID)
                    {
                        if (!ProcessDetails.Process32Next(num, ref lppe))
                        {
                            goto label_8;
                        }
                    }
                    process = Process.GetProcessById((int)lppe.th32ParentProcessID);
                }
                else
                {
                    throw new ApplicationException(string.Format((IFormatProvider)CultureInfo.InvariantCulture, "Failed with win32 error code {0}", (object)Marshal.GetLastWin32Error()));
                }
            }
            catch (Exception ex)
            {
                Logger.Error("Can't get the process. Ex: {0}", (object)ex.ToString());
            }
            finally
            {
                ProcessDetails.CloseHandle(num);
            }
label_8:
            return(process);
        }
示例#3
0
 public static int?GetParentProcessId(int pid)
 {
     return(ProcessDetails.GetParentProcess(pid)?.Id);
 }