How do I enumerate another process' windows in Delphi?

delphi, winapi, windows

Solution

- Call `EnumWindows` to enumerate the top level windows.

- Pass each top level window handle to `GetWindowThreadProcessId` to find out which process ID it is associated with.

- When you find a top level window that matches your process ID, check that the window is the main window of the app, presumably by checking its class name.

- Finally, call `EnumChildWindows` on that main window to enumerate all children of that main window.

Problem

I have a process `foo.exe` that creates a process `bar.exe` with the `CreateProcess` function. I want (in `foo.exe`) to enumerate the controls of a window created in `bar.exe` and for that I (assume that I) need the window HWND. I know all the window classes in `bar.exe`, and that `bar.exe` only creates one window for each class at a time, so I can use the class names to find the window I want. But what function should I use to enumerate the windows in another process? I'm looking for something that take a process handle or PID (both returned by the `CreateProcess` function) and an `EnumProc` callback procedure. Should I find `bar.exe`'s thread ID (it is a single-threaded application) and use that with the `EnumThreadWindows` function?

Original source