"""Probe MSAA (IAccessible) tree of a window — some Qt/custom UIs expose this instead of UIA.""" import sys, ctypes from ctypes import wintypes, POINTER, byref import comtypes from comtypes import GUID import comtypes.client from comtypes.automation import IDispatch oleacc = ctypes.oledll.oleacc user32 = ctypes.windll.user32 OBJID_CLIENT = 0xFFFFFFFC IID_IAccessible = GUID("{618736E0-3C3D-11CF-810C-00AA00389B71}") def main(hwnd): ptr = ctypes.POINTER(IDispatch)() hr = oleacc.AccessibleObjectFromWindow(wintypes.HWND(hwnd), wintypes.DWORD(OBJID_CLIENT), byref(IID_IAccessible), byref(ptr)) print("hr:", hr, "ptr:", bool(ptr)) if not ptr: return acc = ptr.QueryInterface(comtypes.gen.Accessibility.IAccessible) if False else ptr # via IDispatch, call properties through dispinterface is messy; use oleacc helpers on the pointer # Simpler: use accessible accChildCount through vtable is unavailable; try dynamic dispatch try: dispatch = ptr.QueryInterface(IDispatch) print("dispatch ok") except Exception as exc: print("dispatch fail", exc) # Use ctypes to call vtable method 7 (get_accChildCount is 4th method after IUnknown: 3 + 4?). # IAccessible vtable: IUnknown(0-2), get_accParent(3), get_accChildCount(4), get_accChild(5)... vtable = ctypes.cast(ptr, ctypes.POINTER(ctypes.POINTER(ctypes.c_void_p))).contents fn_getcount = ctypes.WINFUNCTYPE(ctypes.HRESULT, ctypes.c_void_p, ctypes.POINTER(ctypes.c_long))(vtable[4]) count = ctypes.c_long(-1) hr = fn_getcount(ptr, byref(count)) print("accChildCount hr:", hr, "count:", count.value) if __name__ == '__main__': main(int(sys.argv[1]))