Trying to get list of all open Windows on Windows 10 Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
downloaderfan
Posts: 34
Joined: 09 Jun 2017, 09:19

Trying to get list of all open Windows on Windows 10

24 Mar 2018, 09:05

Hi, I'm trying to get titles of all open Windows to save to a txt file to avoid data loss during BSOD. I found a script that works perfectly on Windows 7, but not on 10. Apparently modern apps on Windows 10 have these hidden containers that show up when AHK looks for open Windows. Then I found a guide that tells me to add additional code to hide the hidden containers.

I used code from these 2 places:
https://autohotkey.com/board/topic/2165 ... b-windows/
https://autohotkey.com/boards/viewtopic.php?t=13288

After adding the additional code, I came up with https://pastebin.com/NbxhD9nX , but now it's ignoring some open Windows. Not sure what else to try, can anyone help?
iseahound
Posts: 1451
Joined: 13 Aug 2016, 21:04
Contact:

Re: Trying to get list of all open Windows on Windows 10

24 Mar 2018, 13:01

It's not my code. Actually it is my code. I modified the original AltTabWindows() function to remove the call to Decimal_to_Hex() and gave it modern syntax.

[Moderator's note: The original function appears to be by ophthalmos.]

Code: Select all

AltTabWindows() {
   static WS_EX_TOPMOST :=            0x8 ; sets the Always On Top flag
   static WS_EX_APPWINDOW :=      0x40000 ; provides a taskbar button
   static WS_EX_TOOLWINDOW :=        0x80 ; removes the window from the alt-tab list
   static GW_OWNER := 4

   AltTabList := {}
   windowList := ""
   DetectHiddenWindows, Off ; makes DllCall("IsWindowVisible") unnecessary
   WinGet, windowList, List ; gather a list of running programs
   Loop, %windowList%
      {
      ownerID := windowID := windowList%A_Index%
      Loop { ;If the window we found is opened by another application or "child", let's get the hWnd of the parent
         ownerID := Format("0x{:x}",  DllCall("GetWindow", "UInt", ownerID, "UInt", GW_OWNER))
      } Until !Format("0x{:x}",  DllCall("GetWindow", "UInt", ownerID, "UInt", GW_OWNER))
      ownerID := ownerID ? ownerID : windowID

      ; only windows that are not removed from the Alt+Tab list, AND have a taskbar button, will be appended to our list.
      If (Format("0x{:x}", DllCall("GetLastActivePopup", "UInt", ownerID)) = windowID)
         {
         WinGet, es, ExStyle, ahk_id %windowID%
         If (!((es & WS_EX_TOOLWINDOW) && !(es & WS_EX_APPWINDOW)) && !IsInvisibleWin10BackgroundAppWindow(windowID))
            AltTabList.Push(windowID)
         }
      }

   ; UNCOMMENT THIS FOR TESTING
   ;WinGetClass, class1, % "ahk_id" AltTabList[1]
   ;WinGetClass, class2, % "ahk_id" AltTabList[2]
   ;WinGetClass, classF, % "ahk_id" AltTabList.pop()
   ;msgbox % "Number of Windows: " AltTabList.length() "`nFirst windowID: " class1 "`nSecond windowID: " class2 "`nFinal windowID: " classF
   return AltTabList
}

  IsInvisibleWin10BackgroundAppWindow(hWindow) {
    result := 0
    VarSetCapacity(cloakedVal, A_PtrSize) ; DWMWA_CLOAKED := 14
    hr := DllCall("DwmApi\DwmGetWindowAttribute", "Ptr", hWindow, "UInt", 14, "Ptr", &cloakedVal, "UInt", A_PtrSize)
    if !hr ; returns S_OK (which is zero) on success. Otherwise, it returns an HRESULT error code
      result := NumGet(cloakedVal) ; omitting the "&" performs better
    return result ? true : false
    }

  /*
  DWMWA_CLOAKED: If the window is cloaked, the following values explain why:
  1  The window was cloaked by its owner application (DWM_CLOAKED_APP)
  2  The window was cloaked by the Shell (DWM_CLOAKED_SHELL)
  4  The cloak value was inherited from its owner window (DWM_CLOAKED_INHERITED)
  */
Just type array := AltTabWindows() Have you uncommented my testing code? I wrote that years ago but it should give you what you need.

EDIT: Testing code will reveal the z-order of windows given by Alt-Tab order. Press Alt + Tab, and the returned "ahk_id" will match the Alt-Tab order. Use my focus function from the same post.
downloaderfan
Posts: 34
Joined: 09 Jun 2017, 09:19

Re: Trying to get list of all open Windows on Windows 10

24 Mar 2018, 14:27

iseahound wrote:It's not my code. Actually it is my code. I modified the original AltTabWindows() function to remove the call to Decimal_to_Hex() and gave it modern syntax.
Finally got it working to perfection. :D Phew! Thanks.

Code: Select all

array := AltTabWindows()

AltTabWindows() {
   static WS_EX_TOPMOST :=            0x8 ; sets the Always On Top flag
   static WS_EX_APPWINDOW :=      0x40000 ; provides a taskbar button
   static WS_EX_TOOLWINDOW :=        0x80 ; removes the window from the alt-tab list
   static GW_OWNER := 4

   AltTabList := {}
   windowList := ""
   DetectHiddenWindows, Off ; makes DllCall("IsWindowVisible") unnecessary
   WinGet, windowList, List ; gather a list of running programs
   Loop, %windowList%
      {
      ownerID := windowID := windowList%A_Index%
      Loop { ;If the window we found is opened by another application or "child", let's get the hWnd of the parent
         ownerID := Format("0x{:x}",  DllCall("GetWindow", "UInt", ownerID, "UInt", GW_OWNER))
      } Until !Format("0x{:x}",  DllCall("GetWindow", "UInt", ownerID, "UInt", GW_OWNER))
      ownerID := ownerID ? ownerID : windowID

      ; only windows that are not removed from the Alt+Tab list, AND have a taskbar button, will be appended to our list.
      If (Format("0x{:x}", DllCall("GetLastActivePopup", "UInt", ownerID)) = windowID)
         {
         WinGet, es, ExStyle, ahk_id %windowID%
         If (!((es & WS_EX_TOOLWINDOW) && !(es & WS_EX_APPWINDOW)) && !IsInvisibleWin10BackgroundAppWindow(windowID))
            AltTabList.Push(windowID)
         }
      }
Gui, Add, ListView, r20 w700, #|ID|Title
Loop, % AltTabList.length()
{
  WinGetTitle, title, % "ahk_id " AltTabList[A_Index]
  LV_Add("", A_Index, AltTabList[A_Index], title)
}
LV_ModifyCol()  ; Auto-size each column to fit its contents.
Gui, Show
return

GuiClose:
Exitapp
   return AltTabList
}

  IsInvisibleWin10BackgroundAppWindow(hWindow) {
    result := 0
    VarSetCapacity(cloakedVal, A_PtrSize) ; DWMWA_CLOAKED := 14
    hr := DllCall("DwmApi\DwmGetWindowAttribute", "Ptr", hWindow, "UInt", 14, "Ptr", &cloakedVal, "UInt", A_PtrSize)
    if !hr ; returns S_OK (which is zero) on success. Otherwise, it returns an HRESULT error code
      result := NumGet(cloakedVal) ; omitting the "&" performs better
    return result ? true : false
    }
iseahound
Posts: 1451
Joined: 13 Aug 2016, 21:04
Contact:

Re: Trying to get list of all open Windows on Windows 10  Topic is solved

24 Mar 2018, 14:39

Whoa don't put your code inside a function!

Code: Select all

windows := AltTabWindows()
Gui, Add, ListView, r20 w700, #|ID|Title
Loop, % windows.length()
{
  WinGetTitle, title, % "ahk_id " windows[A_Index]
  LV_Add("", A_Index, windows[A_Index], title)
}
LV_ModifyCol()  ; Auto-size each column to fit its contents.
Gui, Show
return

GuiClose:
Exitapp

AltTabWindows() {
   static WS_EX_TOPMOST :=            0x8 ; sets the Always On Top flag
   static WS_EX_APPWINDOW :=      0x40000 ; provides a taskbar button
   static WS_EX_TOOLWINDOW :=        0x80 ; removes the window from the alt-tab list
   static GW_OWNER := 4

   AltTabList := {}
   windowList := ""
   DetectHiddenWindows, Off ; makes DllCall("IsWindowVisible") unnecessary
   WinGet, windowList, List ; gather a list of running programs
   Loop, %windowList%
      {
      ownerID := windowID := windowList%A_Index%
      Loop { ;If the window we found is opened by another application or "child", let's get the hWnd of the parent
         ownerID := Format("0x{:x}",  DllCall("GetWindow", "UInt", ownerID, "UInt", GW_OWNER))
      } Until !Format("0x{:x}",  DllCall("GetWindow", "UInt", ownerID, "UInt", GW_OWNER))
      ownerID := ownerID ? ownerID : windowID

      ; only windows that are not removed from the Alt+Tab list, AND have a taskbar button, will be appended to our list.
      If (Format("0x{:x}", DllCall("GetLastActivePopup", "UInt", ownerID)) = windowID)
         {
         WinGet, es, ExStyle, ahk_id %windowID%
         If (!((es & WS_EX_TOOLWINDOW) && !(es & WS_EX_APPWINDOW)) && !IsInvisibleWin10BackgroundAppWindow(windowID))
            AltTabList.Push(windowID)
         }
      }

   ; UNCOMMENT THIS FOR TESTING
   ;WinGetClass, class1, % "ahk_id" AltTabList[1]
   ;WinGetClass, class2, % "ahk_id" AltTabList[2]
   ;WinGetClass, classF, % "ahk_id" AltTabList.pop()
   ;msgbox % "Number of Windows: " AltTabList.length() "`nFirst windowID: " class1 "`nSecond windowID: " class2 "`nFinal windowID: " classF
   return AltTabList
}

  IsInvisibleWin10BackgroundAppWindow(hWindow) {
    result := 0
    VarSetCapacity(cloakedVal, A_PtrSize) ; DWMWA_CLOAKED := 14
    hr := DllCall("DwmApi\DwmGetWindowAttribute", "Ptr", hWindow, "UInt", 14, "Ptr", &cloakedVal, "UInt", A_PtrSize)
    if !hr ; returns S_OK (which is zero) on success. Otherwise, it returns an HRESULT error code
      result := NumGet(cloakedVal) ; omitting the "&" performs better
    return result ? true : false
    }

  /*
  DWMWA_CLOAKED: If the window is cloaked, the following values explain why:
  1  The window was cloaked by its owner application (DWM_CLOAKED_APP)
  2  The window was cloaked by the Shell (DWM_CLOAKED_SHELL)
  4  The cloak value was inherited from its owner window (DWM_CLOAKED_INHERITED)
  */
 
tuzi
Posts: 223
Joined: 27 Apr 2016, 23:40

Re: Trying to get list of all open Windows on Windows 10

28 Jul 2020, 03:30

@iseahound
hi, iseahound
I searched "z-order" and came here.
Your code is very useful to me, now I can save z-order.
But when I tried to restore z-order, I always failed.

This is what I've tried https://www.autohotkey.com/boards/viewtopic.php?f=76&t=79148

Do you have any ideas to solve this problem?
thank you~
teadrinker
Posts: 4362
Joined: 29 Mar 2015, 09:41
Contact:

Re: Trying to get list of all open Windows on Windows 10

28 Jul 2020, 10:54

iseahound wrote: It's not my code. Actually it is my code.
For me this code not always works correctly, and looks overcomplicated.
More correct way:

Code: Select all

arr := GetAltTabWindows()
for k, v in arr {
   WinGetClass, winClass, ahk_id %v%
   WinGetTitle, title, ahk_id %v%
   altTabWindows .= A_Index . ": " . title . " ahk_class " . winClass . "`n"
}
MsgBox, % altTabWindows

GetAltTabWindows() {
   AltTabList := []
   WinGet, list, List
   Loop % list
      if IsAltTabWindow(list%A_Index%)
         AltTabList.Push(list%A_Index%)
   Return AltTabList
}

IsAltTabWindow(hWnd) {
   static GA_ROOTOWNER := 3, WS_EX_APPWINDOW := 0x40000, WS_EX_TOOLWINDOW := 0x80, DWMWA_CLOAKED := 14
   if !DllCall("IsWindowVisible", "Ptr", hWnd)
      Return false
   
   hOwner := DllCall("GetAncestor", "Ptr", hWnd, "UInt", GA_ROOTOWNER, "Ptr")
   hPopup := DllCall("GetLastActivePopup", "Ptr", hOwner, "Ptr")
   if (hOwner = hWnd && hPopup != hWnd)
      Return false
   
   WinGet, exStyles, ExStyle, ahk_id %hWnd%
   if (exStyles & WS_EX_TOOLWINDOW) && !(exStyles & WS_EX_APPWINDOW)
      Return false
      
   DllCall("DwmApi\DwmGetWindowAttribute", "Ptr", hWnd, "UInt", DWMWA_CLOAKED, "UIntP", cloaked, "UInt", 4)
   Return !cloaked
}
pplupo
Posts: 7
Joined: 30 Jan 2023, 22:41

Re: Trying to get list of all open Windows on Windows 10

16 Feb 2023, 00:48

I'm sorry to revive such an old thread, but can anyone explain this line to me, please?
Static WS_EX_TOOLWINDOW = 0x80, WS_EX_APPWINDOW = 0x40000, GW_OWNER = 4

I'm trying to use some of these functions in a script, but this is messing up with the icon I'm using on the MsgBox.

I was hoping that I could set these values at the beginning of the function and then revert them to the default values before returning, but I don't know what the default values are.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 100 guests