Search code examples
c++winapiwindow-handles

How to get window title from it's process path


I have a path of a process, I would like to get it's window title in C++.

Let's assume that this process has only one window.

So for example my process path is :

C:\Program Files\My Program\program.exe which is a running process's path, and I would like to get it's windows title.

Thanks.


Solution

  • Assuming there is only 1 window in the process, and assuming there is only one copy of the executable running at a time, then you can use EnumWindows() to enumerate top-level windows, calling GetWindowThreadProcessId(), OpenProcess() and GetModuleFileNameEx() on each window to get its owning process's path, and if you find a matching path then you can use GetWindowText() to get that window's title and stop the enumeration.