"""Read a fresh LM tree's verification icon on a private desktop; no input events.""" import json import subprocess import time import sys from pathlib import Path import win32con import win32gui import win32process import win32service from pywinauto.controls.common_controls import TreeViewWrapper from pywinauto.remote_memory_block import RemoteMemoryBlock from pywinauto import win32structures, win32defines library = str(Path(sys.argv[1]).resolve()) name = sys.argv[2] desktop = win32service.CreateDesktop('CbbLibraryReadProbe', 0, win32con.GENERIC_ALL, None) startup = win32process.STARTUPINFO();startup.lpDesktop = r'winsta0\CbbLibraryReadProbe' exe = r'D:\Mentor\7.9.5EE\SDD_HOME\lm\win32\bin\LibraryManager.exe' hp, ht, pid, tid = win32process.CreateProcess(exe, subprocess.list2cmdline([exe, library]), None, None, False, 0, None, None, startup) ht.Close() last_error = '' for attempt in range(120): try: windows = [int(h) for h in desktop.EnumDesktopWindows()] for hwnd in windows: if not win32gui.GetWindowText(hwnd).startswith('Library Manager:'):continue children = [] win32gui.EnumChildWindows(hwnd, lambda c, _: children.append(c), None) trees = [c for c in children if 'TreeView' in win32gui.GetClassName(c)] if not trees:continue tree = TreeViewWrapper(trees[0]) item = tree.get_item(['Central Libraries', Path(library).name, 'Reusable Blocks', 'Logical-Physical', name], exact=True) mem = RemoteMemoryBlock(tree) value = win32structures.TVITEMW32();value.mask = 2|32;value.hItem=item.elem mem.Write(value);tree.send_message(win32defines.TVM_GETITEMW,0,mem);mem.Read(value) print(json.dumps({'library':library, 'name':item.text(), 'image':value.iImage, 'selected_image':value.iSelectedImage, 'pid':pid}),flush=True) del mem win32gui.PostMessage(hwnd, win32con.WM_CLOSE, 0, 0) sys.exit(0) except Exception as error: last_error = str(error) time.sleep(.5) raise RuntimeError(last_error or 'LM tree was not ready')