[2.0.11] Error 87 by ControlGetFocus("A") when cmd.exe active Topic is solved

Report problems with documented functionality
riyah96662
Posts: 5
Joined: 17 Mar 2024, 14:04

[2.0.11] Error 87 by ControlGetFocus("A") when cmd.exe active

17 Mar 2024, 14:36

Code: Select all

F1::Tooltip ControlGetFocus("A")
Somehow an OSError 87 is thrown when passing a cmd.exe window to ControlGetFocus().
It yields the same result by calling ControlGetFocus("ahk_exe cmd.exe").
BTW, it works fine (returning 0) with powershell.exe.
Paste.jpg
Paste.jpg (42.84 KiB) Viewed 348 times
gregster
Posts: 9035
Joined: 30 Sep 2013, 06:48

Re: [2.0.11] Error 87 by ControlGetFocus("A") when cmd.exe active

17 Mar 2024, 15:09

On my Win11 version (Version 10.0.22621.3296), WindowSpy shows ahk_exe WindowsTerminal.exe for the cmd.exe window. Did you check?

With ControlGetFocus("A") I get no error message - it works like expected. But when I look at your error message above, the double quotes look wrong - too slanted.
quotes.png
quotes.png (5.17 KiB) Viewed 342 times

Perhaps this depends on system settings, but on my computer, in error messages, the quotes look like regular straight ones. I would check if you really used this code with the regular double quotes:

Code: Select all

F1::Tooltip ControlGetFocus("A")

PS: I see now that you used the administrator command prompt. To interact with that, your script would need similar rights, afaik.

Edit:
Ah, the admin cmd prompt actually has ahk_exe cmd.exe; and when I run the script with ControlGetFocus("A") elevated, I can actually reproduce the error message you showed.
riyah96662
Posts: 5
Joined: 17 Mar 2024, 14:04

Re: [2.0.11] Error 87 by ControlGetFocus("A") when cmd.exe active

17 Mar 2024, 15:33

You can choose between WindowsTerminal.exe or conhost.exe as the GUI for cmd/powershell.
It happens only when you use cmd with conhost.exe.
lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: [2.0.11] Error 87 by ControlGetFocus("A") when cmd.exe active

21 Mar 2024, 03:26

As documented...
An OSError is thrown if there is a problem determining the focus.
The target window belongs to a thread which does not have a message queue; in other words, a non-GUI thread. GetGUIThreadInfo can't be used to retrieve the focus (and other info) as it is not a GUI thread and has no focus. I suppose this is only possible for console windows, and that it would be reasonable to return 0, which is what happens when a top-level window is focused. However, it is not safe to assume that error 87 means the window is a console window.
lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: [2.0.11] Error 87 by ControlGetFocus("A") when cmd.exe active  Topic is solved

22 Mar 2024, 20:19

In v2.0.12, ControlGetFocus returns 0 for console windows.

Return to “Bug Reports”

Who is online

Users browsing this forum: No registered users and 56 guests