8

In C# using the process class I can get the handle to the Main Window of a process but I need access to a window that is not the main window of its process. How can I get the handle to all of the windows of a process?

Cory Charlton
  • 8,721
  • 4
  • 47
  • 66
RHicke
  • 3,304
  • 3
  • 21
  • 23

3 Answers3

5

The EnumChildWindows function might help you out. The child windows could also have children and so on.

There is also GetWindow and EnumThreadWindows

Cory Charlton
  • 8,721
  • 4
  • 47
  • 66
3

P/Invoking the EnumThreadWindows function of user32, the callback you provide would be given the handles of the windows for a given thread. Process.GetCurrentProcess().Threads should return to you all the relevant threads with which to call EnumThreadWindows.

Richard Ev
  • 51,030
  • 56
  • 187
  • 275
Rich Frank
  • 146
  • 3
3

You can P/Invoke GetWindowThreadProcessId() to get the thread ID for the UI thread that owns the main window. From there, you can find any other top-level window owned by that thread with EnumThreadWindows(). Any child windows (controls) owned by a top-level window can be found with EnumChildWindows(). Visit pinvoke.net for the necessary P/Invoke declarations.

Tobia Zambon
  • 7,310
  • 3
  • 33
  • 69
Hans Passant
  • 897,808
  • 140
  • 1,634
  • 2,455