Skip to content

Instantly share code, notes, and snippets.

@lolp1
Created November 8, 2017 02:37
Show Gist options
  • Save lolp1/f8523572bcb973d27b1448b303d33cf0 to your computer and use it in GitHub Desktop.
Save lolp1/f8523572bcb973d27b1448b303d33cf0 to your computer and use it in GitHub Desktop.
Example of extracting info from windows on the desktop.
/* -------------------------------------
* Produces the output (partial) below
* Title: .NET-BroadcastEventWindow.4.0.0.0.3719ec0.0 Visible: False Thread: 8632 pid: 6148
* Title: Microsoft Edge Visible: True Thread: 9172 pid: 8608
* ....
*/ -------------------------------------
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
namespace ConsoleApp1
{
internal class Program
{
internal static void Main(string[] args)
{
StringBuilder stringBuilder = new StringBuilder();
IEnumerable<IntPtr> hWnds = EnumWindows();
foreach (IntPtr hWnd in hWnds)
{
var text = GetTextFromHwnd(hWnd);
var isVisible = IsWindowVisible(hWnd);
var threadIdAndPID = GetThreadAndPIDFromHWND(hWnd);
stringBuilder.AppendLine(string.IsNullOrEmpty(text) ? "TITLE: UNKNOWN" : $"Title: {text} Visible: {isVisible} Thread: {threadIdAndPID.threadID} pid: {threadIdAndPID.pid}");
}
Console.WriteLine(stringBuilder.ToString());
stringBuilder.Clear();
Console.ReadLine();
}
public static string GetTextFromHwnd(IntPtr hwnd)
{
int size = GetWindowTextLength(hwnd);
if (size++ > 0)
{
StringBuilder sb = new StringBuilder(size);
GetWindowText(hwnd, sb, size);
return sb.ToString();
}
else
{
return "";
}
}
public static IEnumerable<IntPtr> EnumWindows()
{
IntPtr tempHwnd = FindWindow(null, null);
List<IntPtr> window = new List<IntPtr>();
while (tempHwnd != IntPtr.Zero)
{
yield return tempHwnd;
tempHwnd = GetWindow(tempHwnd, GW_HWNDNEXT);
}
}
public static (uint threadID, uint pid) GetThreadAndPIDFromHWND(IntPtr hWnd)
{
uint dwThreadId = GetWindowThreadProcessId(hWnd, out uint dwPid);
return (dwThreadId, dwPid);
}
public static (uint threadID, IntPtr hwnd) GetThreadAndHWNDFromPID(int pid)
{
IntPtr hwnd = GetTopWindow(IntPtr.Zero);
while (hwnd != IntPtr.Zero)
{
uint dwThreadId = GetWindowThreadProcessId(hwnd, out uint dwPid);
if (dwPid == pid)
{
return (dwThreadId, hwnd);
}
hwnd = GetWindow(hwnd, GW_HWNDNEXT);
}
return (0, hwnd);
}
private const int GW_HWNDNEXT = 2;
[DllImport("user32.dll")]
public static extern bool IsWindowVisible(IntPtr hWnd);
[DllImport("user32.dll", SetLastError = true)]
protected static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
protected delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
protected static extern int GetWindowText(IntPtr hWnd, StringBuilder strText, int maxCount);
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
protected static extern int GetWindowTextLength(IntPtr hWnd);
[DllImport("user32.dll")]
protected static extern IntPtr GetWindow(IntPtr hwndSibling, int wFlag);
[DllImport("user32.dll")]
protected static extern IntPtr GetTopWindow(IntPtr hWnd);
[DllImport("user32.dll", SetLastError = true)]
protected static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment