"""Probe the only route proven to open LM's real CBB menu: real input while the private desktop is the input desktop. Message-based triggering cannot work on this LM build because the navigator resolves the right-click target from the physical cursor, and SetCursorPos fails on a non-input desktop. This probe measures the practical cost: switch to the private desktop -> real right-click on the CBB -> UIA invoke of the Verify item -> switch straight back -> confirm ExpeditionPCB launched. Results land in build/input-switch.json. The switch window is kept as short as possible; the desktop is restored in a finally block. """ import ctypes, ctypes.wintypes, json, subprocess, time from pathlib import Path import faulthandler import win32api, win32con, win32gui, win32process, win32service faulthandler.dump_traceback_later(180, exit=True) def step(message): print(f'[{time.strftime("%H:%M:%S")}] {message}', flush=True) ROOT = Path(__file__).resolve().parents[1] LIBRARY = ROOT/'build'/'background-fixture'/'Mentor_lib_EE7.9'/'mentor_lib.lmc' NAME = 'Audio_D2128_ESD' OUTPUT = ROOT/'build'/'input-switch.json' DESKTOP = 'CbbInputSwitchProbe' result = {'library': str(LIBRARY), 'block': NAME, 'steps': []} def record(**entry): entry['time'] = time.strftime('%H:%M:%S') result['steps'].append(entry) OUTPUT.write_text(json.dumps(result, ensure_ascii=False, indent=2), encoding='utf-8') desktop = win32service.CreateDesktop(DESKTOP, 0, win32con.GENERIC_ALL, None) desktop.SetThreadDesktop() from pywinauto import Desktop from pywinauto.controls.common_controls import TreeViewWrapper from pywinauto import win32defines startup = win32process.STARTUPINFO(); startup.lpDesktop = 'winsta0\\' + DESKTOP exe = r'D:\Mentor\7.9.5EE\SDD_HOME\lm\win32\bin\LibraryManager.exe' hp, ht, pid, tid = win32process.CreateProcess(exe, subprocess.list2cmdline([exe, str(LIBRARY)]), None, None, False, 0, None, None, startup) ht.Close() lm = None for _ in range(120): windows = [w for w in Desktop(backend='win32').windows() if w.is_visible() and w.window_text().startswith('Library Manager:') and w.process_id() == pid] if len(windows) == 1: lm = windows[0] break time.sleep(.5) if lm is None: raise SystemExit('Library Manager did not open') tree = TreeViewWrapper([w for w in lm.descendants() if 'TreeView' in w.class_name()][0].handle) item = tree.get_item(['Central Libraries', LIBRARY.name, 'Reusable Blocks', 'Logical-Physical', NAME], exact=True) item.ensure_visible() target_tid = win32process.GetWindowThreadProcessId(tree.handle)[0] our_tid = win32api.GetCurrentThreadId() ctypes.windll.user32.AttachThreadInput(our_tid, target_tid, True) try: ctypes.windll.user32.SetFocus(tree.handle) finally: ctypes.windll.user32.AttachThreadInput(our_tid, target_tid, False) tree.send_message(win32defines.TVM_SELECTITEM, win32defines.TVGN_CARET, item.elem) step(f'selected={item.is_selected()} tree={tree.handle}') record(event='selected', selected=bool(item.is_selected())) ui = Desktop(backend='uia').window(handle=lm.handle) hits = [entry for entry in ui.descendants(control_type='TreeItem') if entry.window_text() == NAME] rect = hits[0].rectangle() screen_xy = (rect.mid_point().x, rect.mid_point().y) step(f'item screen point={screen_xy}') record(event='item_point', screen=list(screen_xy)) def pcb_commands(): service = __import__('win32com.client').client.GetObject('winmgmts:') return [(int(p.ProcessId), str(p.CommandLine or '')) for p in service.ExecQuery("SELECT ProcessId, CommandLine FROM Win32_Process WHERE Name='ExpeditionPCB.exe'")] baseline = {pid_ for pid_, _ in pcb_commands()} record(event='baseline_pcb', pids=sorted(baseline)) input_desktop = win32service.OpenInputDesktop(0, False, win32con.GENERIC_ALL) step(f'input desktop handle={input_desktop}') switch_desktop = ctypes.windll.user32.SwitchDesktop switch_desktop.restype = ctypes.wintypes.BOOL switch_desktop.argtypes = [ctypes.wintypes.HANDLE] switched_in = False try: switched_in = bool(switch_desktop(int(desktop))) step(f'SwitchDesktop(private) = {switched_in}') record(event='switch_in', ok=switched_in) if switched_in: moved = bool(ctypes.windll.user32.SetCursorPos(*screen_xy)) pos = ctypes.wintypes.POINT() ctypes.windll.user32.GetCursorPos(ctypes.byref(pos)) step(f'SetCursorPos={moved} cursor=({pos.x},{pos.y})') record(event='cursor', moved=moved, pos=[pos.x, pos.y]) t0 = time.monotonic() win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0) win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0) time.sleep(.5) menus = [m for m in Desktop(backend='uia').windows(control_type='Menu') if m.is_visible()] items = [[{'name': e.window_text(), 'automation_id': str(e.element_info.automation_id or ''), 'enabled': bool(e.is_enabled())} for e in menu.descendants(control_type='MenuItem')] for menu in menus] step(f'menus after real right-click: {json.dumps(items, ensure_ascii=False)[:300]}') record(event='menu_after_rclick', menus=items) verify = [e for menu in menus for e in menu.descendants(control_type='MenuItem') if str(e.element_info.automation_id or '') == '194'] if len(verify) == 1 and verify[0].is_enabled(): verify[0].invoke() record(event='invoked', automation_id='194', elapsed_ms=int((time.monotonic()-t0)*1000)) step('invoked Verify via UIA') else: record(event='invoke_skipped', found=len(verify)) time.sleep(.4) finally: back = False try: back = bool(switch_desktop(int(input_desktop))) except Exception as error: record(event='switch_back_error', error=str(error)) step(f'SwitchDesktop(back) = {back}') record(event='switch_back', ok=back) for _ in range(40): new = [(pid_, cmd) for pid_, cmd in pcb_commands() if pid_ not in baseline] if new: step(f'PCB launched: {new}') record(event='pcb_launched', pcb=[[pid_, cmd] for pid_, cmd in new]) break time.sleep(.5) else: record(event='pcb_launched', pcb=[]) step('cleanup') subprocess.run(['taskkill', '/PID', str(pid), '/T', '/F'], capture_output=True) for pid_, _ in pcb_commands(): subprocess.run(['taskkill', '/PID', str(pid_), '/T', '/F'], capture_output=True) step('written ' + str(OUTPUT))