public WindowEnumerator() { callback = EnumWindowsCallback; #if !NETFRAMEWORK currentProcessId = (uint)Environment.ProcessId; #else currentProcessId = (uint)System.Diagnostics.Process.GetCurrentProcess().Id; #endif }
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); }
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); }
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); }