修复 background_click=false 时 setForeground 崩溃

Browse Source
现象: 改 set_win_size 后任务在 init_game() 里直接崩, exit code 3, traceback:
  screen.py:343 _set_win -> handle.setForeground()
  -> win32gui.SetForegroundWindow(hwnd)
  -> pywintypes.error: (0, 'SetForegroundWindow', 'No error message is available')

原因: screen.py:342 在 cfg.background_click=False 时才调 setForeground, 而原生
实现(screen.py:167-171)只有一句裸调用, 没有任何兜底。Windows 不允许非前台进程
抢占前台, 失败就抛异常, 这个异常在 set_win() 里没被捕获, 整个任务崩掉。

  也就是说: 我为了修抓图黑屏把 background_click 改成 false, 反而踩出了这条
  一直存在但从没被走到的崩溃路径。

修复: install_robust_set_foreground —— 先试原生调用, 失败则用 MSDN 认可的标准
手法兜底: 把自己的线程输入队列 AttachThreadInput 到当前前台窗口线程上, 借它的
前台权限 BringWindowToTop + SetForegroundWindow, 完成后解挂。兜底也失败只记日志
不抛异常 —— 抢不到前台不该让整个任务死掉。

验证: 同样的 set_win_size=720 配置, 修复前 exit 3 崩溃, 修复后正常走到"一次镜牢"。

附带发现: 驱动级输入这次被 preflight 正确拒绝了(GetForegroundWindow=0)。
用户改用 ToDesk 连接后桌面变成独立会话(主屏是 Todesk Virtual Display Adapter),
驱动级注入在这种会话里本来就不生效 —— 拒绝并回退普通注入是正确行为。
This commit is contained in:
Codex Agent
2026-10-22 13:27:12 +08:00
parent 7eafbdd651
commit e2de126687
2 changed files with 57 additions and 2 deletions
+53
View File
@@ -203,3 +203,56 @@ def install_driver_click(auto, screen, driver=None, log=print):
setattr(auto, name, getattr(proxy, name)) setattr(auto, name, getattr(proxy, name))
log('[lc_mcp] patch: 鼠标点击已切到罗技驱动级输入') log('[lc_mcp] patch: 鼠标点击已切到罗技驱动级输入')
return drv return drv
# ---------------------------------------------------------------- 前台窗口健壮化
def install_robust_set_foreground(screen, log=print):
"""把 SetForegroundWindow 换成带 AttachThreadInput 兜底的版本。
背景(2026-10-22): cfg.background_click=False 时 screen.py:342 会调
self.handle.setForeground(), 而原生实现只有一句裸调用
win32gui.SetForegroundWindow(hwnd)。Windows 不允许非前台进程抢占前台,
失败时抛 pywintypes.error(0, 'SetForegroundWindow', ...) —— 这个异常在
set_win() 里没有捕获, 直接把任务带崩(观测: exit code 3 之前的 traceback)。
兜底手法是 MSDN 认可的标准做法: 把自己的线程输入队列挂到当前前台窗口的
线程上, 借它的前台权限完成切换, 完成后解挂。
"""
handle = getattr(screen, 'handle', None)
if handle is None or not hasattr(handle, 'setForeground'):
log('[lc_mcp] 前台窗口补丁跳过: screen.handle 没有 setForeground')
return None
original = handle.setForeground
def setForeground():
hwnd = getattr(handle, 'hwnd', 0)
if not hwnd:
return
try:
original()
return
except Exception:
pass # 落到下面的 AttachThreadInput 兜底
try:
import win32api
import win32gui
import win32process
foreground = win32gui.GetForegroundWindow()
thread_fg = win32process.GetWindowThreadProcessId(foreground)[0] if foreground else 0
thread_me = win32api.GetCurrentThreadId()
attached = False
if thread_fg and thread_fg != thread_me:
attached = bool(win32process.AttachThreadInput(thread_fg, thread_me, True))
try:
win32gui.BringWindowToTop(hwnd)
win32gui.SetForegroundWindow(hwnd)
finally:
if attached:
win32process.AttachThreadInput(thread_fg, thread_me, False)
except Exception as e:
log('[lc_mcp] setForeground 兜底也失败: %s: %s' % (type(e).__name__, e))
handle.setForeground = setForeground
log('[lc_mcp] patch: SetForegroundWindow 加 AttachThreadInput 兜底')
return handle
+3 -1
View File
@@ -11,7 +11,8 @@ import sys
from . import preflight from . import preflight
from .aalc_engine import bootstrap from .aalc_engine import bootstrap
from .aalc_patches import WindowGuard, install_driver_click, install_exact_team_match from .aalc_patches import (WindowGuard, install_driver_click, install_exact_team_match,
install_robust_set_foreground)
# 任务 -> {参数名: AALC 配置项}; 参数只在本次运行内生效 # 任务 -> {参数名: AALC 配置项}; 参数只在本次运行内生效
TASKS = { TASKS = {
@@ -82,6 +83,7 @@ def run(task, params):
return PREFLIGHT_EXIT return PREFLIGHT_EXIT
install_exact_team_match(auto, log) install_exact_team_match(auto, log)
install_robust_set_foreground(screen, log=log)
guard = WindowGuard(screen, cfg, log=log) guard = WindowGuard(screen, cfg, log=log)
guard.start() guard.start()
restore_path = os.environ.get(RESTORE_ENV) restore_path = os.environ.get(RESTORE_ENV)