Search code examples
pythonenumerate

Enumerating child windows in python?


I have dabbled around for a year or so using c++ and decided I would try my hand at python as it has a much easier syntax and will increase productivity while I am still learning (I think!). I am trying to enumerate all child windows from a parent window of a desktop application in Windows.

import win32ui

def WindowExists(windowname):
    try:
        win32ui.FindWindow(None, windowname)

    except win32ui.error:
        return False
    else:
        return True

appFind = "Test Application"

if WindowExists(appFind):
    print ("Program is running")
    hwnd = win32ui.FindWindow(None, appFind)

else:
    print ("Program is not running")

So far I am identifying the application with no problem but I am wondering if my assignment of hwnd is working as I think it would do in a c++ environment so I would be able to pass my hwnd assignment to enumchildwindows. I am not entirely sure how I get the children from here though.

One other question I had was rather than using just the title of the application, how can I use the handle? if for example the handle was something like 00130903 of testapplication. I remember a few months I messed around with something like this in c++ and I think you can use x to replace the first set of zeros (or something similar) on the handle, but I honestly cant remember much of it so hopefully you guys can help!

Edit -

TypeError: The object is not a PyHANDLE object.

I think my assumption is right here that I am not correctly assigning a proper handle named hwnd , this is the error i get when I try to use enumchldwindows or win32con.WM_GETTEXT , any example of correctly setting a handle by title and by handle would really be appreciated!


Solution

  • hwnd = win32ui.FindWindow(None, appFind) , worked for verifying the windows existance

    hwnd = win32gui.FindWindow(None, appFind), worked to allow me to use the handle!, live and we learn!