Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explorer shell crashes when calling getDesktopNumOfWindow() for all windows on Windows 10 #12

Closed
3tmp opened this issue May 3, 2022 · 13 comments

Comments

@3tmp
Copy link

3tmp commented May 3, 2022

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

@FuPeiJiang
Copy link
Owner

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

@FuPeiJiang
Copy link
Owner

I ran your code on windows 10 vmware, no crash
by "explorer shell", do you mean explorer.exe ? with taskbar and desktop ?

@FuPeiJiang
Copy link
Owner

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

@3tmp
Copy link
Author

3tmp commented May 4, 2022

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 ?

Yes, i mean the explorer.exe
shell_crashing

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

@3tmp
Copy link
Author

3tmp commented May 4, 2022

Also, if I run the code from the global_functions branch, then the shell does not crash.

@FuPeiJiang
Copy link
Owner

FuPeiJiang commented May 4, 2022

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

@3tmp
Copy link
Author

3tmp commented May 4, 2022

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

@FuPeiJiang
Copy link
Owner

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..

@3tmp
Copy link
Author

3tmp commented May 4, 2022

Ah I see

@3tmp
Copy link
Author

3tmp commented May 4, 2022

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.

@3tmp
Copy link
Author

3tmp commented May 4, 2022

Thanks!

@FuPeiJiang
Copy link
Owner

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

@FuPeiJiang
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants