"""Probe whether the Verify command can be executed by WM_COMMAND without a menu. AutomationId 194 on the context menu item is likely the MFC command id. If any window in the navigator's command chain accepts WM_COMMAND 194, LM starts the Verify flow (observable as an ExpeditionPCB.exe launch) with no mouse involved. Only command 194 is ever sent; no other command id is tested. Results land in build/wmcommand-broadcast.json. """ 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'/'wmcommand-broadcast.json' DESKTOP = 'CbbCommandBroadcast' VERIFY_COMMAND_ID = 194 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() tree.send_message(win32defines.TVM_SELECTITEM, win32defines.TVGN_CARET, item.elem) step(f'selected={item.is_selected()}') def focus_window(hwnd): target_tid = win32process.GetWindowThreadProcessId(hwnd)[0] our_tid = win32api.GetCurrentThreadId() attached = bool(ctypes.windll.user32.AttachThreadInput(our_tid, target_tid, True)) try: return bool(ctypes.windll.user32.SetFocus(hwnd)), attached finally: ctypes.windll.user32.AttachThreadInput(our_tid, target_tid, False) def all_windows(root_pid): """Every top-level and child window of the LM process, with its class name.""" found = [] def visit(h, _): if win32process.GetWindowThreadProcessId(h)[1] == root_pid: found.append((h, win32gui.GetClassName(h), win32gui.GetWindowText(h))) def child(c, _2): if win32process.GetWindowThreadProcessId(c)[1] == root_pid: found.append((c, win32gui.GetClassName(c), win32gui.GetWindowText(c))) return True win32gui.EnumChildWindows(h, child, None) return True win32gui.EnumWindows(visit, None) return found def pcb_pids(): service = __import__('win32com.client').client.GetObject('winmgmts:') return [int(p.ProcessId) for p in service.ExecQuery("SELECT ProcessId FROM Win32_Process WHERE Name='ExpeditionPCB.exe'")] baseline = set(pcb_pids()) windows = all_windows(pid) step(f'LM windows: {[(hex(h), c) for h, c, _ in windows]}') result = {'library': str(LIBRARY), 'block': NAME, 'verify_command_id': VERIFY_COMMAND_ID, 'windows': [[int(h), c] for h, c, _ in windows], 'attempts': []} def check(label, hwnd): time.sleep(1.0) new = sorted(set(pcb_pids()) - baseline) entry = {'target': int(hwnd), 'class': win32gui.GetClassName(hwnd), 'new_pcb': new} result['attempts'].append(entry) OUTPUT.write_text(json.dumps(result, ensure_ascii=False, indent=2), encoding='utf-8') step(f'{label} hwnd={hex(hwnd)} class={entry["class"]} -> new_pcb={new}') return bool(new) # Focus the navigator tree (input-independent) then broadcast only command 194. focus_ok, attached = focus_window(tree.handle) step(f'focus tree ok={focus_ok} attached={attached}') try: frame_hwnd = lm.handle for hwnd, cls, title in windows: win32gui.PostMessage(hwnd, win32con.WM_COMMAND, VERIFY_COMMAND_ID, 0) if check('broadcast-all-windows', frame_hwnd): raise SystemExit(0) # Retry with the pane/view focused explicitly. for hwnd, cls, title in windows: if cls.startswith('Afx') or cls.startswith('ATL'): focus_window(hwnd) win32gui.PostMessage(hwnd, win32con.WM_COMMAND, VERIFY_COMMAND_ID, 0) if check(f'command-after-focus-{cls}', hwnd): raise SystemExit(0) finally: step('written ' + str(OUTPUT)) subprocess.run(['taskkill', '/PID', str(pid), '/T', '/F'], capture_output=True) for p in pcb_pids(): subprocess.run(['taskkill', '/PID', str(p), '/T', '/F'], capture_output=True)