How to retrieve IDs of all associated windows

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

How to retrieve IDs of all associated windows

08 Apr 2019, 08:22

Hi!

On https://autohotkey.com/board/topic/20850-retrieve-win-ids-of-all-associated-windows-and-child-windows/ 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%
ExitApp
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?
spookynutz
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: Ben, Bing [Bot], kdservices, ManualColdSwitch, Ploaie, vsub and 143 guests