Exemple #1
0
        static unsafe bool TryFindChildProcessByNameWindows(Process process, string childName, out Process child)
        {
            IntPtr snapshot = Kernel32.CreateToolhelp32Snapshot(Kernel32.Toolhelp32Flags.TH32CS_SNAPPROCESS, 0);

            if (snapshot == IntPtr.Zero)
            {
                child = null;
                return(false);
            }

            try
            {
                int ppid = process.Id;

                var processEntry = new Kernel32.ProcessEntry32W {
                    Size = sizeof(Kernel32.ProcessEntry32W)
                };

                bool success = Kernel32.Process32FirstW(snapshot, ref processEntry);
                while (success)
                {
                    if (processEntry.ParentProcessID == ppid)
                    {
                        try
                        {
                            Process c = Process.GetProcessById(processEntry.ProcessID);
                            if (c.ProcessName.Equals(childName, StringComparison.OrdinalIgnoreCase))
                            {
                                child = c;
                                return(true);
                            }
                            c.Dispose();
                        }
                        catch {}
                    }

                    success = Kernel32.Process32NextW(snapshot, ref processEntry);
                }

                child = null;
                return(false);
            }
            finally
            {
                Kernel32.CloseHandle(snapshot);
            }
        }
Exemple #2
0
        private unsafe static IEnumerable <Process> Windows_GetChildren(Process process)
        {
            var    children = new List <Process>();
            IntPtr snapshot = Kernel32.CreateToolhelp32Snapshot(Kernel32.Toolhelp32Flags.TH32CS_SNAPPROCESS, 0);

            if (snapshot != IntPtr.Zero && snapshot.ToInt64() != Kernel32.INVALID_HANDLE)
            {
                try
                {
                    children = new List <Process>();
                    int ppid = process.Id;

                    var processEntry = new Kernel32.ProcessEntry32W {
                        Size = sizeof(Kernel32.ProcessEntry32W)
                    };

                    bool success = Kernel32.Process32FirstW(snapshot, ref processEntry);
                    while (success)
                    {
                        if (processEntry.ParentProcessID == ppid)
                        {
                            try
                            {
                                children.Add(Process.GetProcessById(processEntry.ProcessID));
                            }
                            catch {}
                        }

                        success = Kernel32.Process32NextW(snapshot, ref processEntry);
                    }
                }
                finally
                {
                    Kernel32.CloseHandle(snapshot);
                }
            }

            return(children);
        }