/// <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> public static Process GetParentProcess(this IntPtr handle) { var pbi = new ProcessBasicInformation(); var returnLength = 0; var status = NtQueryInformationProcess( handle, 0, ref pbi, pbi.Size, out returnLength); if (status != 0) { throw new Win32Exception(status); } try { return(Process.GetProcessById( pbi.InheritedFromUniqueProcessId.ToInt32())); } catch (ArgumentException) { // not found return(null); } }
private static extern int NtQueryInformationProcess( IntPtr processHandle, int processInformationClass, ref ProcessBasicInformation 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> public static Process GetParentProcess(this IntPtr handle) { var pbi = new ProcessBasicInformation(); var returnLength = 0; var status = NtQueryInformationProcess( handle, 0, ref pbi, pbi.Size, out returnLength); if (status != 0) { throw new Win32Exception(status); } try { return Process.GetProcessById( pbi.InheritedFromUniqueProcessId.ToInt32()); } catch (ArgumentException) { // not found return null; } }