Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enumerate all windows belonging to a particular process using .NET?

Tags:

c#

.net

windows

How i can find all the windows created by a particular process using c#?

UPDATE

i need enumerate all the windows belonging to an particular process using the PID (process ID) of the an application.

like image 908
Salvador Avatar asked Mar 28 '10 03:03

Salvador


1 Answers

delegate bool EnumThreadDelegate(IntPtr hWnd, IntPtr lParam);  [DllImport("user32.dll")] static extern bool EnumThreadWindows(int dwThreadId, EnumThreadDelegate lpfn,     IntPtr lParam);  static IEnumerable<IntPtr> EnumerateProcessWindowHandles(int processId) {     var handles = new List<IntPtr>();      foreach (ProcessThread thread in Process.GetProcessById(processId).Threads)         EnumThreadWindows(thread.Id,              (hWnd, lParam) => { handles.Add(hWnd); return true; }, IntPtr.Zero);      return handles; } 

and sample usage:

private const uint WM_GETTEXT = 0x000D;  [DllImport("user32.dll", CharSet = CharSet.Auto)] static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam,      StringBuilder lParam);  [STAThread] static void Main(string[] args) {     foreach (var handle in EnumerateProcessWindowHandles(         Process.GetProcessesByName("explorer").First().Id))     {         StringBuilder message = new StringBuilder(1000);         SendMessage(handle, WM_GETTEXT, message.Capacity, message);         Console.WriteLine(message);     } } 
like image 118
Konstantin Spirin Avatar answered Sep 27 '22 22:09

Konstantin Spirin