/// <summary> /// Enumerate window handles. /// </summary> /// <param name="desktop">Desktop containing the Windows. Optional.</param> /// <param name="parent">The parent Window. Optional.</param> /// <param name="enum_children">True to enumerate child Windows.</param> /// <param name="hide_immersive">Hide immersive Windows.</param> /// <param name="thread_id">The thread ID that owns the Window.</param> /// <param name="throw_on_error">True to throw on error.</param> /// <returns>The enumerated Window Handles.</returns> public static NtResult <IEnumerable <NtWindow> > GetWindows(NtDesktop desktop, NtWindow parent, bool enum_children, bool hide_immersive, int thread_id, bool throw_on_error) { int count = 64; while (true) { IntPtr[] handles = new IntPtr[count]; NtStatus status = NtSystemCalls.NtUserBuildHwndList(desktop.GetHandle(), parent.Handle, enum_children, hide_immersive, thread_id, handles.Length, handles, out int required_count); if (status.IsSuccess()) { return(handles.Take(required_count).Select(i => new NtWindow(i)).CreateResult()); } if (status != NtStatus.STATUS_BUFFER_TOO_SMALL || count > required_count) { return(status.CreateResultFromError <IEnumerable <NtWindow> >(throw_on_error)); } count = required_count; } }
/// <summary> /// Enumerate window handles. /// </summary> /// <param name="desktop">Desktop containing the Windows. Optional.</param> /// <param name="parent">The parent Window. Optional.</param> /// <param name="enum_children">True to enumerate child Windows.</param> /// <param name="hide_immersive">Hide immersive Windows.</param> /// <param name="thread_id">The thread ID that owns the Window.</param> /// <returns>The enumerated Window Handles.</returns> public static IEnumerable <NtWindow> GetWindows(NtDesktop desktop, NtWindow parent, bool enum_children, bool hide_immersive, int thread_id) { return(GetWindows(desktop, parent, enum_children, hide_immersive, thread_id, true).Result); }