I'm currently trying to get all window handles from a process started by subprocess.Popen
looking like this:
def openMP():
mp_p = os.path.abspath("C:\Program Files (x86)\Mobile Partner\Mobile Partner.exe")
mp = subprocess.Popen(mp_p, stdin=None, stdout=None, stderr=None, close_fds=True)
return mp.pid
pid = openMP()
wins = get_hwnds_for_pid(pid)
for win in wins:
print(win, "=>", win32gui.GetWindowText(win))
this is using this function (found here):
def get_hwnds_for_pid (pid):
def callback (hwnd, hwnds):
if win32gui.IsWindowVisible (hwnd) and win32gui.IsWindowEnabled (hwnd):
_, found_pid = win32process.GetWindowThreadProcessId (hwnd)
if found_pid == pid:
hwnds.append (hwnd)
return True
hwnds = []
win32gui.EnumWindows (callback, hwnds)
return hwnds
This kind of works but not in the way i would like it too.
it returns a single hwnd
and i feel it is missing existing child windows.
I've tested with the same pid
using AutoIts WinList
+ a simple regex looking like this:
;THIS IS AUTOIT CODE
Local $aWinList = WinList("[REGEXPTITLE:(?i)(.*Mobile Partner.*)]")
and it indeed returns three instead of one hwnd
.
I would like my python code to get these three Handles aswell.
I've tested around with win32.guiEnumChildWindows
but cant get it to work it seems:
parent = hwnd
childs = []
def callback(hwnd,isx):
print(hwnd)
print(isx)
childs.append(hwnd)
ax = win32gui.EnumChildWindows(parent,callback,None)
print(ax)
for child in childs:
print(child)
sadly this just returns:
pywintypes.error: (2, 'EnumChildWindows', 'The system cannot find the file specified.')
i don't really know why. i'm aware that this is quite complicated but maybe you bright minds can help me.
To sum it up again:
I'm looking for a way to find each and every window handle spawned by a known process id using python (doesn't have to be nativ) on a windows 10 machine.
It should return the same amount of handles AutoIts WinList
does.