Search code examples
c++windowspidfindwindow

How to find PID of window containing X


I can use FindWindow, but the name of the window changes every time I open it. So my question is, how can I either:

A) Find the PID of a window that contains 'x'

B) Find the PID a window with the name of the exe file?

Sorry if the question is obvious, new to C++. Thanks in advance!


Solution

    1. Try to use EnumWindows to get all windows handles
    2. Check state of windows handle by IsWindowVisible. Because some invisible windows can hang the call to GetWindowText
    3. Get the title of each windows by GetWindowText. Then check title contains 'X' which characters or string you want.
    4. Get pid of it by GetWindowThreadProcessId

    About B/, you can get executable file name by GetWindowModuleFileName via its windows handle.