/// <summary> /// Find windows belonging to the same process (thread) as the supplied window. /// </summary> /// <param name="windowToLinkTo">InteropWindow</param> /// <returns>IEnumerable with InteropWindow</returns> public static IEnumerable <IInteropWindow> GetLinkedWindows(this IInteropWindow windowToLinkTo) { var processIdSelectedWindow = windowToLinkTo.GetProcessId(); using (var process = Process.GetProcessById(processIdSelectedWindow)) { foreach (ProcessThread thread in process.Threads) { var handles = new List <IntPtr>(); try { User32Api.EnumThreadWindows(thread.Id, (hWnd, lParam) => { handles.Add(hWnd); return(true); }, IntPtr.Zero); } finally { thread?.Dispose(); } foreach (var handle in handles) { yield return(InteropWindowFactory.CreateFor(handle)); } } } }
/// <summary> /// Find windows belonging to the same process (thread) as the process ID. /// </summary> /// <param name="processId">int with process Id</param> /// <returns>IEnumerable with IInteropWindow</returns> public static IEnumerable <IInteropWindow> GetWindowsForProcess(int processId) { using (var process = Process.GetProcessById(processId)) { foreach (ProcessThread thread in process.Threads) { var handles = User32Api.EnumThreadWindows(thread.Id); thread.Dispose(); foreach (var handle in handles) { yield return(InteropWindowFactory.CreateFor(handle)); } } } }