Explorer shell crashes when calling getDesktopNumOfWindow() for all windows on Windows 10
3tmp opened this issue · comments
When I run this code, the explorer shell crashes and restarts:
#Include _VD.ahk
VD.init()
foundProcesses := ""
; Make sure to get all windows from all virtual desktops
DetectHiddenWindows On
WinGet, id, List
Loop %id%
{
hwnd := id%A_Index%
; Only consider visible windows
If (DllCall("User32.dll\IsWindowVisible", "Ptr", hwnd))
{
d := VD.getDesktopNumOfWindow("ahk_id" hwnd)
If (d > -1)
{
WinGet, exe, ProcessName, % "ahk_id" hwnd
foundProcesses .= exe "`n"
}
}
}
MsgBox % foundProcesses
The windows that cause the problem seem to be invisible to the user, which means that the built in commands return blank strings for the process path or class name (even it the script runs as admin).
Sadly, I could not find out more details about the problem.
The problem also occurs if I run the script on a fresh Windows install
interesting..
using a debugger, you can see exactly which line causes a crash
vscode https://marketplace.visualstudio.com/items?itemName=zero-plusplus.vscode-autohotkey-debug
with https://marketplace.visualstudio.com/items?itemName=mark-wiemer.vscode-autohotkey-plus-plus
I ran on Windows 11, no crash
fresh Windows install
how do you get a fresh Windows install that easily ? Vmware ?
I shall use that to get Windows 10
it's interesting that you can use
"ahk_id" hwnd
instead of
"ahk_id " hwnd
I ran your code on windows 10 vmware, no crash
by "explorer shell", do you mean explorer.exe ? with taskbar and desktop ?
The windows that cause the problem seem to be invisible to the user, which means that the built in commands return blank strings for the process path or class name
then what's the wintitle ? VD.getDesktopNumOfWindow
should return -1 for empty "" wintitle
Maybe it's your AHK version ? as last hope ?
mine is 1.1.33.10
MsgBox % Clipboard:=A_AhkVersion
how do you get a fresh Windows install that easily ? Vmware ?
I got the fresh install using the Windows Sandbox.
And I also just tried it with vmware, same result sadly.
The ahk version is 1.1.33.11 and the Windows 10 version is 21H2
by "explorer shell", do you mean explorer.exe ? with taskbar and desktop ?
using a debugger, you can see exactly which line causes a crash
I already tried using a debugger yesterday, but I will try again as soon as I have the time to.
then what's the wintitle ?
I will try to identify the wintitle
Also, if I run the code from the global_functions
branch, then the shell does not crash.
I got it, I got the bug, now I can try to fix it (on Win11 and Win10)
ahk classes are a pain to debug
if I use If (d > -1)
instead of if (d != -1)
then the problem becomes apparent that I didn't include the class
I thought it would throw an error if the class isn't included, but nothing happens, it just runs
the class VD
isn't even included and these 2 methods run without throwing...
VD.init()
VD.getDesktopNumOfWindow("ahk_id" hwnd)
needed #Include _VD.ahk
at the top
Also, if I run the code from the global_functions branch, then the shell does not crash.
good news, then it's fixable
Windows Sandbox. ? wow I forgot about that. seeing it being used for debugging makes me want to try it
thank you
ahk classes are a pain to debug
Yeah, ahk in general is not that much fun to debug...
Ah yes, it can be really frustrating that ahk just ignores errors
I use Windows Sandbox all the time. It's really useful if you just want to try out a new program, but you don't want to install it on your main machine and it boots much faster than normal VMs. But if I remember correctly it only works on the Pro versions and not Home
Windows Shell Experience Host
ahk_class Windows.UI.Core.CoreWindow
ahk_exe ShellExperienceHost.exe
Inside of class, I was setting DetectHiddenWindows Off
, so no WinTitle..
Ah I see
I think I could locate the problem: It has to be somewhere inside of the _getFirstValidWindow(wintitle)
method, because I modified the method to just return the hwnd that it received and now there are no crashes anymore.
Thanks!
you may want to do it like this
;VD.getDesktopNumOfWindow will filter out invalid windows`
#Include ..\VD.ahk
foundProcesses := ""
; Make sure to get all windows from all virtual desktops
DetectHiddenWindows On
WinGet, id, List
Loop %id%
{
hwnd := id%A_Index%
;VD.getDesktopNumOfWindow will filter out invalid windows
desktopNum_ := VD.getDesktopNumOfWindow("ahk_id" hwnd)
If (desktopNum_ > -1) ;-1 for invalid window, 0 for "Show on all desktops", 1 for Desktop 1
{
WinGet, exe, ProcessName, % "ahk_id" hwnd
foundProcesses .= desktopNum_ " " exe "`n"
}
}
MsgBox % foundProcesses
I think I should add this code block in the README.md
https://github.com/FuPeiJiang/VD.ahk/blob/da6445cfadfce8ddef1e70751ee319507422d461/other%20examples/foundProcesses_min.ahk
https://github.com/FuPeiJiang/VD.ahk/blob/da6445cfadfce8ddef1e70751ee319507422d461/other%20examples/foundProcesses.ahk
Yeah, ahk in general is not that much fun to debug...
Ah yes, it can be really frustrating that ahk just ignores errors
oh, ahkv2 fixed it, so that's not ahk's issue, I think ahkv2 is 10x easier to debug