public static int GetParentProcessId() { var pbi = new ProcessBasicInformation(); //Get a handle to our own process var hProc = OpenProcess((ProcessAccess)0x001F0FFF, false, Process.GetCurrentProcess().Id); try { var queryStatus = NtQueryInformationProcess(hProc, 0, ref pbi, pbi.Size, out var sizeInfoReturned); } finally { if (!hProc.Equals(IntPtr.Zero)) { //Close handle and free allocated memory CloseHandle(hProc); hProc = IntPtr.Zero; } } return((int)pbi.InheritedFromUniqueProcessId); }
internal static extern int NtQueryInformationProcess(IntPtr hProcess, ProcessInfoClass pic, ref ProcessBasicInformation pbi, int cb, out int pSize);