How to retrieve IDs of all associated windows

Get help with using AutoHotkey and its commands and hotkeys
Posts: 444
Joined: 24 Aug 2016, 03:34

How to retrieve IDs of all associated windows

08 Apr 2019, 08:22


On there is a script (Lexikos) retrieving the IDs of all windows associated with the running AHK script:

Code: Select all

DetectHiddenWindows, On

Gui, 1: Show, w200 h200, SHowHidden Window
Sleep, 2000

Process, Exist         ; , AutoHotkey.exe			; ***
WinGet, id, List, ahk_pid %ErrorLevel%
Loop, %id%
    id := id%A_Index%
    WinGetClass, cl, ahk_id %id%
    WinGetTitle, ti, ahk_id %id%
    text .= ti " ahk_class " cl "`n"
MsgBox %text%
If the PIDOrName in the Process command (line ***) is omitted, the windows of the actual script are retrieved.

If AutoHotkey.exe is specified for the PIDOrName, the result may refer to another running AHK script, but it is unclear on which of the running scripts the choice falls.

How to retrieve the windows of a specific other running script, e.g. a script launched by C:\Test\AutoHotkey.exe?
Posts: 12
Joined: 09 Apr 2019, 12:48

Re: How to retrieve IDs of all associated windows

09 Apr 2019, 13:02

Using WMI?

Code: Select all

DetectHiddenWindows, On

search_string := "C:\Test\AutoHotkey.exe"

wmi := ComObjGet("winmgmts:{impersonationLevel=impersonate,(debug)}!\\.\root\cimv2")
col_item := wmi.ExecQuery("SELECT ProcessId FROM Win32_Process Where CommandLine Like '%" . search_string . "%'")._NewEnum()
while col_item[obj_item] {
	WinGetTitle, win_title, % "ahk_pid" obj_item["ProcessId"]
	MsgBox % win_title	

The search string could be script name, script path, executable name or path, examples:
search_string := "C:\scripts\New AutoHotkey Script.ahk"
search_string := "New AutoHotkey Script.ahk"
search_string := "AutoHotKey.exe"
search_string := "C:\Test\"
search_string := "C:\Test\AutoHotkey.exe"

Return to “Ask For Help”

Who is online

Users browsing this forum: AHKStudent, barbaque, c7aesa7r, Chiefkes, Google [Bot], Jrdixon_ and 308 guests