Following is a simple Console application which will enumerate over the open windows and display the handle and caption for each. If you need to do something more complex then you will probably be able to call another API using the provided hWnd parameter in the WindowEnumerator method.
The sample calls three API calls to get a result - GetWindowTextLength and GetWindowText to get the caption of the window and EnumWindows to actually get the list of windows.
using System;
using System.Text;
using System.Runtime.InteropServices;
namespace EnumerateWindowSample
{
///
/// Summary description for Class1.
///
class Class1
{
[DllImport("user32.dll", SetLastError=true, CharSet=CharSet.Auto)]
static extern int GetWindowTextLength(IntPtr hWnd);
[DllImport("user32.dll", SetLastError=true, CharSet=CharSet.Auto)]
static extern int GetWindowText(IntPtr hWnd, [Out] StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll")]
static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);
delegate int EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
static int WindowEnumerator(IntPtr hWnd, IntPtr lParam)
{
int length = GetWindowTextLength(hWnd);
StringBuilder sb = new StringBuilder(length + 1);
GetWindowText(hWnd, sb, sb.Capacity);
Console.WriteLine("Handle {0}, \t Caption {1}", hWnd, sb.ToString());
return -1;
}
///
/// The main entry point for the application.
///
[sTAThread]
static void Main(string[] args)
{
EnumWindowsProc wp = new EnumWindowsProc(WindowEnumerator);
EnumWindows(wp, IntPtr.Zero);
Console.ReadLine();
}
}
}