0

Is there a way to get the hwnd-object from a specific window by only providing the executable path or name?

Like GetForegroundWindow() from win32gui but not just the foreground window.

Id imagine something like this: GetWindowByPath("C:/mypath/Spotify.exe") or GetWindowByName("Spotify"). Maybe with ahk? (I have no clue about AHK)

I've now read other questions and docs for hours and havn't found a way. Thanks in advance :)

martineau
  • 119,623
  • 25
  • 170
  • 301
Nx2
  • 19
  • 3
  • This [answer of mine](https://stackoverflow.com/a/42178357/355230) shows how to do it. – martineau Feb 12 '22 at 17:45
  • 1
    It's easier to just know the classname of the application's primary HWND in advance. This is easily obtained with the Visual Studio tool Spy++ and inspecting its HWND properties. Then use EnumWindows and similar APIs to find the window you want. – selbie Feb 12 '22 at 17:49

1 Answers1

0

There is no direct way, you need to approach it from each end and meet in the middle which is the process id.

If this was native code you could use the Toolhelp functions to enumerate all the processes and look at the filename of each. When you find a match, remember the process id.

Then you can call EnumWindows to find all top-level windows. In the callback you would call GetWindowThreadProcessId on each window until you find one that matches the process id you found earlier.

Anders
  • 97,548
  • 12
  • 110
  • 164