Example #1
0
        public static Process GetParent(this Process child)
        {
            int parentPid = 0;

            IntPtr hnd = Kernel32.CreateToolhelp32Snapshot(Kernel32.TH32CS_SNAPPROCESS, 0);

            if (hnd == IntPtr.Zero)
                return null;

            var processInfo = new Kernel32.PROCESSENTRY32
                                  {dwSize = (uint) Marshal.SizeOf(typeof (Kernel32.PROCESSENTRY32))};

            if (Kernel32.Process32First(hnd, ref processInfo) == false) return null;

            do
            {
                if (child.Id == processInfo.th32ProcessID)
                    parentPid = (int) processInfo.th32ParentProcessID;
            } while (parentPid == 0 && Kernel32.Process32Next(hnd, ref processInfo));

            if (parentPid > 0)
                return Process.GetProcessById(parentPid);
            else
                return null;
        }
        Process GetParent(Process child)
        {
            if (child == null)
            {
                throw new ArgumentNullException(nameof(child));
            }

            try
            {
                int parentPid = 0;

                IntPtr hnd = Kernel32.CreateToolhelp32Snapshot(Kernel32.TH32CS_SNAPPROCESS, 0);

                if (hnd == IntPtr.Zero)
                {
                    return(null);
                }

                var processInfo = new Kernel32.PROCESSENTRY32
                {
                    dwSize = (uint)Marshal.SizeOf(typeof(Kernel32.PROCESSENTRY32))
                };

                if (Kernel32.Process32First(hnd, ref processInfo) == false)
                {
                    return(null);
                }

                do
                {
                    if (child.Id == processInfo.th32ProcessID)
                    {
                        parentPid = (int)processInfo.th32ParentProcessID;
                    }
                } while (parentPid == 0 && Kernel32.Process32Next(hnd, ref processInfo));

                if (parentPid > 0)
                {
                    return(Process.GetProcessById(parentPid));
                }
            }
            catch (Exception ex)
            {
                _log.Error("Unable to get parent process (ignored)", ex);
            }

            return(null);
        }
Example #3
0
        public static Process GetParent([NotNull] this Process child)
        {
            if (child == null)
                throw new ArgumentNullException("child");

            try
            {
                int parentPid = 0;

                IntPtr hnd = Kernel32.CreateToolhelp32Snapshot(Kernel32.TH32CS_SNAPPROCESS, 0);

                if (hnd == IntPtr.Zero)
                    return null;

                var processInfo = new Kernel32.PROCESSENTRY32
                    {
                        dwSize = (uint)Marshal.SizeOf(typeof(Kernel32.PROCESSENTRY32))
                    };

                if (Kernel32.Process32First(hnd, ref processInfo) == false)
                    return null;

                do
                {
                    if (child.Id == processInfo.th32ProcessID)
                        parentPid = (int)processInfo.th32ParentProcessID;
                }
                while (parentPid == 0 && Kernel32.Process32Next(hnd, ref processInfo));

                if (parentPid > 0)
                    return Process.GetProcessById(parentPid);
            }
            catch (Exception ex)
            {
                _log.Error("Unable to get parent process (ignored)", ex);
            }
            return null;
        }
Example #4
0
        public static Process GetParent(this Process child)
        {
            int parentPid = 0;

            IntPtr hnd = Kernel32.CreateToolhelp32Snapshot(Kernel32.TH32CS_SNAPPROCESS, 0);

            if (hnd == IntPtr.Zero)
            {
                return(null);
            }

            var processInfo = new Kernel32.PROCESSENTRY32
            {
                dwSize = (uint)Marshal.SizeOf(typeof(Kernel32.PROCESSENTRY32))
            };

            if (Kernel32.Process32First(hnd, ref processInfo) == false)
            {
                return(null);
            }

            do
            {
                if (child.Id == processInfo.th32ProcessID)
                {
                    parentPid = (int)processInfo.th32ParentProcessID;
                }
            }while (parentPid == 0 && Kernel32.Process32Next(hnd, ref processInfo));

            if (parentPid > 0)
            {
                return(Process.GetProcessById(parentPid));
            }
            else
            {
                return(null);
            }
        }