/// <summary> /// Gets the parent process of a specified process. /// </summary> /// <param name="handle">The process handle.</param> /// <returns>An instance of the Process class or null if an error occurred.</returns> public static Process GetParentProcess(IntPtr handle) { ParentProcessUtils pbi = new ParentProcessUtils(); int returnLength; int status = NativeMethods.NtQueryInformationProcess(handle, 0, ref pbi, Marshal.SizeOf(pbi), out returnLength); if (status != 0) { return(null); } try { return(Process.GetProcessById(pbi.InheritedFromUniqueProcessId.ToInt32())); } catch (ArgumentException) { // not found return(null); } }
internal static extern int NtQueryInformationProcess(IntPtr processHandle, int processInformationClass, ref ParentProcessUtils processInformation, int processInformationLength, out int returnLength);