How can I enumerate the open windows (~ EnumWindows) of another user session

advertisements

I have an application that can list the opened windows of the current session. It uses the EnumWindows method from the user32.dll.

I would like to run this code from a windows service, but as the service is not attached to a user session, it returns nothing obviously.

So the question is, how can I enumerate the open windows of another user session (e.g. with a specific logon user)?

Similarly to EnumWindows, I also would like to get the foreground window of the user session as well (like GetForegroundWindow works for the current user).


As far as I'm aware, you can't access the windows of one session from another. It's also worth noting that there's not really any such thing as "the current session" - there may be multiple users logged on through terminal services, or XP's fast user switching.

One approach to this would be to add a program to each user's profile with no UI that just communicates with your service. You'd still have to cope with the fact that there could be multiple active sessions, though.