Ejemplo n.º 1
0
            public WindowEnumerator()
            {
                callback = EnumWindowsCallback;

#if !NETFRAMEWORK
                currentProcessId = (uint)Environment.ProcessId;
#else
                currentProcessId = (uint)System.Diagnostics.Process.GetCurrentProcess().Id;
#endif
            }
Ejemplo n.º 2
0
        public static IEnumerable <IntPtr> GetTopLevelWindows()
        {
            var topLevelWindows = new List <IntPtr>();

            var enumFunc = new User32.WNDENUMPROC((hWnd, lParam) => {
                topLevelWindows.Add(hWnd);
                return(true);
            });

            User32.EnumWindows(enumFunc, IntPtr.Zero);

            return(topLevelWindows);
        }
Ejemplo n.º 3
0
        public List <IntPtr> FindDesktopProcess()
        {
            var list = new List <IntPtr>();

            User32.WNDENUMPROC WNDENUMPROC = (handle, handleParams) =>
            {
                if (!User32.IsWindowVisible(handle))
                {
                    return(true);
                }

                if (!User32.IsWindow(handle))
                {
                    return(true);
                }

                string windowtext;

                if (!TryGetWindowText(handle, out windowtext))
                {
                    return(true);
                }

                list.Add(handle);

                return(true);
            };

            try
            {
                using (var safeDesktopHandle = new User32.SafeDesktopHandle())
                {
                    User32.EnumDesktopWindows(safeDesktopHandle, WNDENUMPROC, default(IntPtr));
                }
            }
            finally
            {
                // C++ is resident evil
                GC.KeepAlive(WNDENUMPROC);
            }

            return(list);
        }
Ejemplo n.º 4
0
        public static IEnumerable <IntPtr> GetTopLevelWindows()
        {
            var topLevelWindows = new List <IntPtr>();

            var enumFunc = new User32.WNDENUMPROC((hWnd, lParam) => {
                topLevelWindows.Add(hWnd);
                return(true);
            });

            var success = User32.EnumWindows(enumFunc, IntPtr.Zero);

            if (!success)
            {
                var hresult = Marshal.GetHRForLastWin32Error();
                Marshal.ThrowExceptionForHR(hresult);
            }

            return(topLevelWindows);
        }