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