0

I would like to know if it's possible to list all the GUI apps running on Windows with python.

I've been doing some research and people recommend using psutil, and it seems okay, however, it lists all the active processes, I haven't been able to find any resource on how to keep only the processes that are associated to an active GUI app (for example, Firefox, Chrome, Skype, Discord, and so on).

Is there any way to do this? Maybe not with Python (although that would be amazing), maybe through some C++? Getting the PID of the process would be enough.

EDIT: Thanks to chrisbyte and Vijaye Raji, win32gui and EnumWindows is exactly what I was looking for.

  • `win32gui` might be helpful (`pip install pywin32`). There is an example here to enumerate active windows and get the full titles https://superuser.com/a/677023 although the title is the FULL title and not the application name by itself. – chrisbyte Oct 27 '20 at 00:10
  • There is nothing in the system that would allow you to distinguish a process that will not show a GUI from a process that will. – IInspectable Oct 27 '20 at 06:30
  • There are many ways to define a GUI process: Processes that are currently showing one or more windows, processes that have at least one GUI thread (i.e., a thread with a message queue), processes whose executable image has IMAGE_SUBSYSTEM_WINDOWS_GUI in the subsystem field of the optional COFF image header, processes that do not have a console attached, etc. Most of these can be detected if you program has sufficient privilege. But @IInspectable is correct, some apps optionally create a GUI (perhaps along with a console). So some definitions are too vague. – Adrian McCarthy Oct 27 '20 at 20:09

1 Answers1

2

By GUI process I assume you mean processes that have a window attached. What you need is EnumWindows API which with a callback, and within the callback, you could use GetWindowThreadProcessId to get the process id of each window.

Here's an answer that's close enough to get you going: https://stackoverflow.com/a/21767578

slacker
  • 509
  • 2
  • 6