Esempio n. 1
0
        /// <summary>
        /// Determines if a process has a window.
        /// </summary>
        /// <param name="process">The process to check.</param>
        /// <returns>A value indicating whether or not the given process has a window.</returns>
        public static Boolean IsProcessWindowed(Process process)
        {
            if (WindowsProcessInfo.WindowedProcessCache.Contains(process.Id))
            {
                return(true);
            }

            if (WindowsProcessInfo.NoWindowProcessCache.Contains(process.Id))
            {
                return(false);
            }

            // Check if the window handle is set
            if (process.MainWindowHandle != IntPtr.Zero)
            {
                WindowsProcessInfo.WindowedProcessCache.Add(process.Id);
                return(true);
            }

            // Ignore system processes
            if (WindowsProcessInfo.IsProcessSystemProcess(process))
            {
                WindowsProcessInfo.NoWindowProcessCache.Add(process.Id);
                return(false);
            }

            // Window handle was not set, so to be certain we must enumerate the process threads, looking for window threads
            foreach (ProcessThread threadInfo in process.Threads)
            {
                if (NativeMethods.EnumWindows((IntPtr hWnd, Int32 lParam) =>
                {
                    if (NativeMethods.GetWindowThreadProcessId(hWnd, out _) == lParam)
                    {
                        if (NativeMethods.IsWindowVisible(hWnd))
                        {
                            WindowsProcessInfo.WindowedProcessCache.Add(process.Id);
                            return(true);
                        }
                    }

                    return(false);
                }, threadInfo.Id))
                {
                    return(true);
                }
            }

            WindowsProcessInfo.NoWindowProcessCache.Add(process.Id);
            return(false);
        }
Esempio n. 2
0
        /// <summary>
        /// Determines if a process has a window.
        /// </summary>
        /// <param name="process">The process to check.</param>
        /// <returns>A value indicating whether or not the given process has a window.</returns>
        public static Boolean IsProcessWindowed(Process process)
        {
            if (WindowsProcessInfo.WindowedProcessCache.Contains(process.Id))
            {
                return(true);
            }

            if (WindowsProcessInfo.NoWindowProcessCache.Contains(process.Id))
            {
                return(false);
            }

            // Check if the window handle is set
            if (process.MainWindowHandle != IntPtr.Zero)
            {
                WindowsProcessInfo.WindowedProcessCache.Add(process.Id);
                return(true);
            }

            // Ignore system processes
            if (WindowsProcessInfo.IsProcessSystemProcess(process))
            {
                WindowsProcessInfo.NoWindowProcessCache.Add(process.Id);
                return(false);
            }

            // Window handle was not set, so to be certain we must enumerate the process threads, looking for window threads
            foreach (ProcessThread threadInfo in process.Threads)
            {
                IntPtr[] windows = WindowsProcessInfo.GetWindowHandlesForThread(threadInfo.Id);

                if (windows != null)
                {
                    foreach (IntPtr handle in windows)
                    {
                        if (NativeMethods.IsWindowVisible(handle))
                        {
                            WindowsProcessInfo.WindowedProcessCache.Add(process.Id);
                            return(true);
                        }
                    }
                }
            }

            WindowsProcessInfo.NoWindowProcessCache.Add(process.Id);
            return(false);
        }
Esempio n. 3
0
        /// <summary>
        /// Fetches the icon associated with the provided process.
        /// </summary>
        /// <param name="process">The process to check.</param>
        /// <returns>An Icon associated with the given process. Returns null if there is no icon.</returns>
        public static Icon GetIcon(Process process)
        {
            Icon icon = null;

            if (WindowsProcessInfo.NoIconProcessCache.Contains(process.Id))
            {
                return(WindowsProcessInfo.NoIcon);
            }

            if (WindowsProcessInfo.IconCache.TryGetValue(process.Id, out icon))
            {
                return(icon);
            }

            if (WindowsProcessInfo.IsProcessSystemProcess(process))
            {
                WindowsProcessInfo.NoIconProcessCache.Add(process.Id);
                return(WindowsProcessInfo.NoIcon);
            }

            try
            {
                IntPtr iconHandle = NativeMethods.ExtractIcon(process.Handle, process.MainModule.FileName, 0);

                if (iconHandle == IntPtr.Zero)
                {
                    WindowsProcessInfo.NoIconProcessCache.Add(process.Id);
                    return(WindowsProcessInfo.NoIcon);
                }

                icon = Icon.FromHandle(iconHandle);
                WindowsProcessInfo.IconCache.Add(process.Id, icon);

                return(icon);
            }
            catch
            {
                WindowsProcessInfo.NoIconProcessCache.Add(process.Id);
                return(WindowsProcessInfo.NoIcon);
            }
        }