Getting the hWid of a window using the process name?

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
Eliazar
Posts: 15
Joined: 24 May 2022, 01:13

Getting the hWid of a window using the process name?

Post by Eliazar » 06 Feb 2023, 07:46

The window I'm targeting is from Microsoft To Do, when "keep on top" is enabled (it becomes small and always on top)
From here:
image.png
image.png (583.61 KiB) Viewed 2122 times
To here (this is the window that I'm targeting):
image.png
image.png (179.53 KiB) Viewed 2122 times

And it seems like WinGetList cannot see the window that I am targeting and I'm not really sure why but maybe because it is somewhat cloaked.

Here's my attempt to get the list of all windows:

Code: Select all


#Requires AutoHotkey v2.0
DetectHiddenWindows True
DetectHiddenText True

ids := WinGetList()

MyGui := Gui()
MyGui.AddListBox("h700 w600", ids)
MyGui.Show()


> note: I figured out that WinGetList does not detect that window because I checked the process ID of that using the AHK window spy, and it doesn't appear on any item in the list.

That said window doesn't have a title so my best chance to get its hWid is using its ProcessName which is "Todo.exe". With that, I was thinking of listing all the windows and then checking each of their ProcessName if it matches with "Todo.exe" but it kinda will not work, since WinGetList doesn't seem to detect that window in the first place.

Any thoughts on another way to get the hWid of a window using the process name?

User avatar
mikeyww
Posts: 26593
Joined: 09 Sep 2014, 18:38

Re: Getting the hWid of a window using the process name?

Post by mikeyww » 06 Feb 2023, 08:03

Code: Select all

#Requires AutoHotkey v2.0
MsgBox hWnd := WinExist("ahk_exe notepad.exe")

Code: Select all

#Requires AutoHotkey v2.0
gui1 := Gui()
pName := []
For each, hWnd in WinGetList()
 pName.Push(WinGetProcessName(hWnd))
gui1.AddListBox('w600 h600', pName)
gui1.Show

User avatar
Eliazar
Posts: 15
Joined: 24 May 2022, 01:13

Re: Getting the hWid of a window using the process name?

Post by Eliazar » 06 Feb 2023, 18:07

Thank you for the immediate response!

I apologize for the confusion, but I've already tried that and my problem is WinGetList and WinExist doesn't seem to detect the window that I'm targeting. So I'm asking if there's anyway to get the hwid of that window, knowing only it's ProcessName?

User avatar
Eliazar
Posts: 15
Joined: 24 May 2022, 01:13

Re: Getting the hWid of a window using the process name?

Post by Eliazar » 06 Feb 2023, 18:31

Right now I'm looking at using EnumWindows of windows api: https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-enumwindows

But I'm not so sure if it's even possible to be implemented using a DllCall() because it requires a callback function.

User avatar
mikeyww
Posts: 26593
Joined: 09 Sep 2014, 18:38

Re: Getting the hWid of a window using the process name?

Post by mikeyww » 06 Feb 2023, 19:41

Is the window visible? What does Window Spy show as the WinTitle (you can post a screenshot)? What is your script using WinExist to find the window?

User avatar
Eliazar
Posts: 15
Joined: 24 May 2022, 01:13

Re: Getting the hWid of a window using the process name?

Post by Eliazar » 06 Feb 2023, 20:51

Is the window visible
Yes, it is
What does Window Spy show as the WinTitle (you can post a screenshot)?
I'm sorry but I cannot post a screenshot at the moment. Although It shows nothing from what I can remember.
What is your script using WinExist to find the window?
Because I can't get the WinTitle, and it's hWid is not showing in the WinGetList, I just decided not to even try using WinExist.

User avatar
mikeyww
Posts: 26593
Joined: 09 Sep 2014, 18:38

Re: Getting the hWid of a window using the process name?

Post by mikeyww » 06 Feb 2023, 21:03

WinGetList and WinExist doesn't seem to detect the window. I just decided not to even try using WinExist.
Considering your contradictory statements, lack of screenshot, and no additional information, this is all certainly quite a mystery! Perhaps someone here uses To Do and will have the answer. I am unable to install the app: App Store just hangs when I try it!
Last edited by mikeyww on 06 Feb 2023, 21:16, edited 1 time in total.

User avatar
Eliazar
Posts: 15
Joined: 24 May 2022, 01:13

Re: Getting the hWid of a window using the process name?

Post by Eliazar » 06 Feb 2023, 21:10

I'm really sorry for the confusion caused, I'll try my best to clear things up with a screenshot.

User avatar
mikeyww
Posts: 26593
Joined: 09 Sep 2014, 18:38

Re: Getting the hWid of a window using the process name?

Post by mikeyww » 06 Feb 2023, 21:48

Sorry that my To Do installation failed; I'm not sure why.


User avatar
Eliazar
Posts: 15
Joined: 24 May 2022, 01:13

Re: Getting the hWid of a window using the process name?

Post by Eliazar » 09 Feb 2023, 03:56

My apologies for the late update, but I've provided now the screenshot and additional details to clear things out.
image.png
image.png (350.59 KiB) Viewed 1812 times
The truth is I haven't really tried using WinExist because I thought WinGetList and WinExist uses the same underlying process. Anyways, I've decided to try it.
#Requires AutoHotkey v2.0
DetectHiddenWindows True
DetectHiddenText True

; WinExist("Microsoft To Do")
; WinExist("ahk_class ApplicationFrameWindow")
; WinExist("ahk_exe ApplicationFrameHost.exe")
WinExist("ahk_pid " 4204)
WinExist("ahk_id " 4653868)
Although the only way it detected the window is by providing the pid (process id) or id (hwnd), which is very hard to get.

These are all the ways I tried just to find it (and only one did work but was barely viable):

1. WinExist (Code above) - [Didn't work]
- using the ahk_class and ahk_exe didn't detected it.

2. Listing all the processes and checking each if they matched the ProcessName - [Didn't work]
- since each process can contain multiple windows, therefore, multiple hwnd can lie in a single process (also I don't know how to recover the hwnd from a process)

Code: Select all

MyGui := Gui(, "Process List")
LV := MyGui.Add("ListView", "x2 y0 w400 h500", ["HWND", "Process Name","Command Line"])

for process in ComObjGet("winmgmts:").ExecQuery("Select * from Win32_Process") {
    LV.Add("", process.Handle, process.Name, process.CommandLine)
    if process.Name == "Todo.exe" {
        MsgBox "Todo process Id: " process.ProcessId
        return process.ProcessId
    }
}

MyGui.Show()
3. Iterate literally every window possible just to find it - [Didn't work]
- using a recursive function to iterate to every window/child window returned by EnumWindows and EnumChildWindows

Code: Select all

MyGui := Gui()
Lv := MyGui.Add("ListView", "h700 w600",["HWND", "Process Name"])
MyGui.Show()

DllCall("EnumWindows", "ptr", callbackCreate(EnumWindows, "F"), "uint", 0)

EnumChildWindows(hwnd) {
    try {
        pn := WinGetProcessName(hwnd)
    } catch { ; incase of access error
        return True
    }

    DllCall("EnumChildWindows", "ptr", hwnd, "ptr", callbackCreate(EnumChildWindows, "F"), "uint", 0)
    Lv.add("", hwnd, pn)

    if pn == "Todo.exe" {
        MsgBox "Match!!"
    }

    return True
}

EnumWindows(hwnd) {
    try {
        pn := WinGetProcessName(hwnd)
    } catch { ; incase of access error
        return True
    }
    
    if pn == "ApplicationFrameHost.exe" {
        DllCall("EnumChildWindows", "ptr", hwnd, "ptr", callbackCreate(EnumChildWindows, "F"), "uint", 0)
        Lv.add("", hwnd, pn)
    }

    if pn == "Todo.exe" {
        MsgBox "Match!!"
    }

    return True
}
4. Using "WindowsFromPoint" to get the hwnd of a window from a certain coordinate - [Kinda works]
- it's working, but with a major downside which is the requirement of window to be the topmost. Meaning I can't get the hwnd of a window if it's hidden, not in a certain position, or minimized.

Code: Select all

x := 1150
y := 300

hWnd := DllCall("WindowFromPoint", "UInt64", x|(y << 32), "Ptr")
MsgBox WinGetTitle(hWnd) " : " hWnd " : "
WinExist(hWnd)

User avatar
Eliazar
Posts: 15
Joined: 24 May 2022, 01:13

Re: Getting the hWid of a window using the process name?

Post by Eliazar » 09 Feb 2023, 04:13

@mikeyww
Thank you for the effort mikeyww! I understand that installing from the Microsoft store can often be a hard time.

@just me
Very useful link indeed! It gave me some clues that led me to the things I've tried above.

User avatar
Eliazar
Posts: 15
Joined: 24 May 2022, 01:13

Re: Getting the hWid of a window using the process name?

Post by Eliazar » 10 Feb 2023, 00:06

As an additional for those that might need something similar in the future, you will need to find out how exactly does WindowsFromPoint finds that window so you can implement something that is fundamentally the same with that.

just me
Posts: 9423
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Getting the hWid of a window using the process name?

Post by just me » 10 Feb 2023, 04:27

I don't have 'MS To Do' but I cannot imagine that

Code: Select all

WinExist("ahk_class ApplicationFrameWindow")
WinExist("ahk_exe ApplicationFrameHost.exe")
don't work while AHK Window Spy is reporting exactly this values:
ahk_class ApplicationFrameWindow
ahk_exe ApplicationFrameHost.exe
Are you sure?

User avatar
Eliazar
Posts: 15
Joined: 24 May 2022, 01:13

Re: Getting the hWid of a window using the process name?

Post by Eliazar » 10 Feb 2023, 06:08

I know that sounds very counterintuitive, but that's what really is happening.

Here's a screen share:

(Info according to window spy)
HWND : 13600
PID : 525632

2023-02-10 - 5246 - [18-56-50].gif
2023-02-10 - 5246 - [18-56-50].gif (1.93 MiB) Viewed 1668 times

User avatar
mikeyww
Posts: 26593
Joined: 09 Sep 2014, 18:38

Re: Getting the hWid of a window using the process name?

Post by mikeyww » 10 Feb 2023, 06:44

I did verify that the window title is not found, despite Window Spy's showing it; I'm not sure why. It must have something to do with how Windows manages the on-top window. I'm not sure whether it is also an AHK bug, as the Window Spy does not match what AHK is retrieving through the script. viewtopic.php?f=14&t=113730

Code: Select all

#Requires AutoHotkey v2.0
toDo := "ahk_exe ApplicationFrameHost.exe"
hWnd := WinExist(toDo)
MsgBox hWnd, 'HWND', 64
ws.png
Window Spy
ws.png (184.28 KiB) Viewed 1674 times
zero.png
Output
zero.png (3.56 KiB) Viewed 1674 times

just me
Posts: 9423
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Getting the hWid of a window using the process name?

Post by just me » 10 Feb 2023, 08:55

@Eliazar,
what do you want to show with your 'shared screen'? AFAICS both WinExist() calls with "ahk_class" and "ahk_exe" retrieve a HWND. Also, you've set DetectHiddenWindows, On. You shouldn't do it.

Window Spy doesn't search for a window by title. You checked Follow Mouse, so it shows information about the window under the mouse cursor.

User avatar
mikeyww
Posts: 26593
Joined: 09 Sep 2014, 18:38

Re: Getting the hWid of a window using the process name?

Post by mikeyww » 10 Feb 2023, 09:17

You are probably getting info for a different window, not this one, just me.


Post Reply

Return to “Ask for Help (v2)”