/// <summary> /// Returns the <see cref="Process"/>'s parent process, if any. /// </summary> /// <param name="handle">An <see cref="IntPtr"/> handle to the child process.</param> /// <returns>The parent <see cref="Process"/>. NULL if none or error.</returns> private static Process GetParentProcess(IntPtr handle) { var pbi = new ProcessBasicInformation(); int returnLength; var status = NativeMethods.NtQueryInformationProcess(handle, 0, ref pbi, Marshal.SizeOf(pbi), out returnLength); if (status != 0) { throw new Win32Exception(status); } try { return Process.GetProcessById(pbi.InheritedFromUniqueProcessId.ToInt32()); } catch (ArgumentException) { return null; } }
internal static extern int NtQueryInformationProcess(IntPtr processHandle, int processInformationClass, ref ProcessBasicInformation processInformation, int processInformationLength, out int returnLength);