1

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!

Paul R
  • 208,748
  • 37
  • 389
  • 560
Tanner H.
  • 304
  • 1
  • 13

1 Answers1

3
  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.

Community
  • 1
  • 1
GSP
  • 574
  • 3
  • 7
  • 34
  • Step 2 (`IsWindow()`) is not necessary inside the enumeration callback. Maybe you are thinking of `IsWindowVisible()` instead? – Remy Lebeau Jan 20 '17 at 04:57
  • 2
    Your update is [quoting someone else's comment](http://stackoverflow.com/questions/797967/how-to-stop-enumwindows-running-infinitely-win32/798003#comment14193164_798003) with no basis behind it. A window's visibility does not affect `GetWindowText()`. And in fact, if a window belongs to another process, [`GetWindowText()` **cant** hang](https://blogs.msdn.microsoft.com/oldnewthing/20030821-00/?p=42833), by design. The only way it can hang is if the window belongs to your own process and the window's message queue is not being processed. That would be a bug in your own code. – Remy Lebeau Jan 20 '17 at 09:04