Example #1
0
        internal static Process GetParentProcess(Process process)
        {
            int     pid              = process.Id;
            Process parentProc       = null;
            IntPtr  handleToSnapshot = IntPtr.Zero;

            try
            {
                NM.ProcessEntry32 procEntry = new NM.ProcessEntry32();
                procEntry.dwSize = (uint)Marshal.SizeOf(typeof(NM.ProcessEntry32));
                handleToSnapshot = NM.CreateToolhelp32Snapshot(NM.ToolHelpCreateSnapshotFlags.Process, 0);
                if (NM.Process32First(handleToSnapshot, ref procEntry))
                {
                    do
                    {
                        if (pid == procEntry.th32ProcessID)
                        {
                            parentProc = Process.GetProcessById((int)procEntry.th32ParentProcessID);
                            break;
                        }
                    }while (NM.Process32Next(handleToSnapshot, ref procEntry));
                }
                else
                {
                    throw GUI.ApeException("Failed with win32 error code " + Marshal.GetLastWin32Error().ToString());
                }
            }
            finally
            {
                NM.CloseHandle(handleToSnapshot);
            }
            return(parentProc);
        }