Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is a solution. It uses p/invoke, but seems to work well, 32 or 64 cpu:</p> <pre><code> /// &lt;summary&gt; /// A utility class to determine a process parent. /// &lt;/summary&gt; [StructLayout(LayoutKind.Sequential)] public struct ParentProcessUtilities { // These members must match PROCESS_BASIC_INFORMATION internal IntPtr Reserved1; internal IntPtr PebBaseAddress; internal IntPtr Reserved2_0; internal IntPtr Reserved2_1; internal IntPtr UniqueProcessId; internal IntPtr InheritedFromUniqueProcessId; [DllImport("ntdll.dll")] private static extern int NtQueryInformationProcess(IntPtr processHandle, int processInformationClass, ref ParentProcessUtilities processInformation, int processInformationLength, out int returnLength); /// &lt;summary&gt; /// Gets the parent process of the current process. /// &lt;/summary&gt; /// &lt;returns&gt;An instance of the Process class.&lt;/returns&gt; public static Process GetParentProcess() { return GetParentProcess(Process.GetCurrentProcess().Handle); } /// &lt;summary&gt; /// Gets the parent process of specified process. /// &lt;/summary&gt; /// &lt;param name="id"&gt;The process id.&lt;/param&gt; /// &lt;returns&gt;An instance of the Process class.&lt;/returns&gt; public static Process GetParentProcess(int id) { Process process = Process.GetProcessById(id); return GetParentProcess(process.Handle); } /// &lt;summary&gt; /// Gets the parent process of a specified process. /// &lt;/summary&gt; /// &lt;param name="handle"&gt;The process handle.&lt;/param&gt; /// &lt;returns&gt;An instance of the Process class.&lt;/returns&gt; public static Process GetParentProcess(IntPtr handle) { ParentProcessUtilities pbi = new ParentProcessUtilities(); int returnLength; int status = 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) { // not found return null; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload