Reputation: 13356
Using C#, how can I list the unique ID's of open windows (even those without titles)? Not the process id of running programs, but the underlying unique id of the window itself.
I'm actually trying to build nothing more complex than simply listing the unique ID's of the open windows, to assist our IT office with a few issues they're experiencing in a virtual application environment.
Thank you in advance!
Update
As per the answers and the comments, I did in fact mean Window's Handle
, not unique ID.
Upvotes: 0
Views: 496
Reputation: 4873
If by window's unique id you mean the window's HANDLE
, here's a simple example to do that:
[DllImport("user32.dll")]
private static extern bool EnumDesktopWindows(IntPtr hDesktop, EnumWindowsProc ewp, int lParam);
[DllImport("user32.dll")]
private static extern bool IsWindowVisible(IntPtr hwnd);
public delegate bool EnumWindowsProc(IntPtr hWnd, int lParam);
private static void Main(string[] args)
{
var collection = new Collection<IntPtr>();
EnumWindowsProc enumerateHandle = delegate(IntPtr hWnd, int lParam)
{
if (IsWindowVisible(hWnd)) // remove to include hidden windows
collection.Add(hWnd);
return true;
};
if (EnumDesktopWindows(IntPtr.Zero, enumerateHandle, 0)) {
foreach (var item in collection) {
Console.WriteLine(item);
}
}
Console.Read();
}
Upvotes: 1