I'm trying to get window list for a process like task manager, but I need help...
Sample :
Do you have any idea how to get the same window list as task manager?
Thanks
In my code I have try this but the result is not the same as task manager.
Using main Handle with EnumChildWindows:
var windowHelper = new WindowHandleInfo(process.MainWindowHandle);
IEnumerable<IntPtr> windows = windowHelper.GetAllChildHandles().
Where(p_Handle => windowHelper.getPidFromWindow(p_Handle) == process.Id && windowHelper.isWindowVisible(p_Handle));
foreach (var hWnd in windows)
{
App.Log.Fatal($"Window visible : (Handle : {hWnd} - caption : {windowHelper.getWindowText(hWnd)})");
}
This approach got more windows than Task Manager.
Result :
2023-11-10 20:56:30,089 FATAL [5] CRMatrixAPI.Core.MainCore.OnCheckHMIResponding - Window visible : (Handle : 919276 - caption : )
2023-11-10 20:56:30,091 FATAL [5] CRMatrixAPI.Core.MainCore.OnCheckHMIResponding - Window visible : (Handle : 788236 - caption : Zone 1)
2023-11-10 20:56:30,092 FATAL [5] CRMatrixAPI.Core.MainCore.OnCheckHMIResponding - Window visible : (Handle : 789120 - caption : )
2023-11-10 20:56:30,092 FATAL [5] CRMatrixAPI.Core.MainCore.OnCheckHMIResponding - Window visible : (Handle : 919388 - caption : Zone 2)
For documentation, this is my WindowHandleInfo class :
public class WindowHandleInfo
{
private delegate bool EnumWindowProc(IntPtr hwnd, IntPtr lParam);
[StructLayout(LayoutKind.Sequential)]
public struct WINDOWINFO
{
public uint cbSize;
public RECT rcWindow;
public RECT rcClient;
public uint dwStyle;
public uint dwExStyle;
public uint dwWindowStatus;
public uint cxWindowBorders;
public uint cyWindowBorders;
public ushort atomWindowType;
public ushort wCreatorVersion;
public WINDOWINFO(Boolean? filler)
: this() // Allows automatic initialization of "cbSize" with "new WINDOWINFO(null/true/false)".
{
cbSize = (UInt32)(Marshal.SizeOf(typeof(WINDOWINFO)));
}
}
[DllImport("user32")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool EnumChildWindows(IntPtr hWnd, EnumWindowProc callback, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId);
[DllImport("user32.dll", CallingConvention = CallingConvention.Winapi, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool IsHungAppWindow(IntPtr hwnd);
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError = true)]
private static extern bool GetWindowInfo(IntPtr hwnd, ref WINDOWINFO pwi);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool IsWindowVisible(IntPtr hWnd);
[DllImport("user32.dll")]
static extern bool CloseWindow(IntPtr hWnd);
private IntPtr _MainHandle;
public WindowHandleInfo(IntPtr handle)
{
this._MainHandle = handle;
}
public List<IntPtr> GetAllChildHandles()
{
List<IntPtr> childHandles = new List<IntPtr>();
GCHandle gcChildhandlesList = GCHandle.Alloc(childHandles);
IntPtr pointerChildHandlesList = GCHandle.ToIntPtr(gcChildhandlesList);
try
{
EnumWindowProc childProc = new EnumWindowProc(EnumWindow);
EnumChildWindows(this._MainHandle, childProc, pointerChildHandlesList);
}
finally
{
gcChildhandlesList.Free();
}
return childHandles;
}
public int getPidFromWindow(IntPtr hWnd)
{
GetWindowThreadProcessId(hWnd, out int pid);
return pid;
}
public bool windowIsAlive(IntPtr hWnd)
{
return !IsHungAppWindow(hWnd);
}
public string getWindowText(IntPtr hWnd)
{
const int nChars = 256;
var buf = new StringBuilder(nChars);
return GetWindowText(hWnd, buf, nChars) > 0 ? buf.ToString() : string.Empty;
}
public bool closeWindow(IntPtr hWnd)
{
// Close the window using API
return CloseWindow(hWnd);
}
public bool IsMDI(IntPtr hwnd)
{
WINDOWINFO info = new WINDOWINFO();
info.cbSize = (uint)Marshal.SizeOf(info);
GetWindowInfo(hwnd, ref info);
//0x00000040L is the style for WS_EX_MDICHILD
return (info.dwExStyle & 0x00000040L) == 1;
}
public WINDOWINFO getWindowinfo(IntPtr hwnd)
{
WINDOWINFO info = new WINDOWINFO();
info.cbSize = (uint)Marshal.SizeOf(info);
GetWindowInfo(hwnd, ref info);
return info;
}
public bool isWindowVisible(IntPtr hwnd)
{
return IsWindowVisible(hwnd);
}
private bool EnumWindow(IntPtr hWnd, IntPtr lParam)
{
GCHandle gcChildhandlesList = GCHandle.FromIntPtr(lParam);
if (gcChildhandlesList == null || gcChildhandlesList.Target == null)
{
return false;
}
List<IntPtr> childHandles = gcChildhandlesList.Target as List<IntPtr>;
childHandles.Add(hWnd);
return true;
}
}