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); }
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); }
public static int?GetParentProcessId(int pid) { return(ProcessDetails.GetParentProcess(pid)?.Id); }