WSLg桌面集成

在 WSL2 环境中通过 WSLg 运行 GUI 应用(Chrome headed、微信),实现 Windows 桌面集成和跨环境截图操作。

概述

Surface WSL Bot 运行在 WSL2 (Ubuntu 22.04, ARM64) 环境中,通过 WSLg 将 Linux GUI 应用映射到 Windows 桌面。主要用于:

  1. agent-browser headed 模式展示浏览器自动化测试过程
  2. 微信 Linux 版安装与截图监控实验
  3. PowerShell 跨环境窗口截图

环境配置

必需环境变量

WSL 重启后必须重设,否则 GUI 应用无法显示:

export DISPLAY=:0
export WAYLAND_DISPLAY=wayland-0
export XDG_RUNTIME_DIR=/mnt/wslg/runtime-dir

Chrome headed 启动参数

DISPLAY=:0 agent-browser open <url> --headed --no-sandbox

Chrome 在 WSLg 中需要额外参数:

  • --no-sandbox — WSL2 namespace 限制
  • --disable-gpu — GPU 加速兼容性
  • --disable-dev-shm-usage — 共享内存限制

验证 WSLg 状态

# 检查 X11 socket
ls -l /tmp/.X11-unix
# 检查 Wayland
echo $WAYLAND_DISPLAY
# 简单 GUI 测试
xeyes &

微信 Linux 版实验

安装

# 下载 ARM64 deb 包 (179MB)
wget https://dldir1v6.qq.com/weixin/Universal/Linux/WeChatLinux_arm64.deb
sudo dpkg -i WeChatLinux_arm64.deb
sudo apt-get install -f  # 安装依赖
DISPLAY=:0 wechat &

截图方案对比

方案可行性说明
WSL scrot/grimWSLg Wayland 不支持 screencopy
WSL xwd只截到黑屏
PowerShell 全屏截图CopyFromScreen,需要窗口在前台
PowerShell PrintWindow后台截取,不需要在前台
AT-SPI 无障碍接口WSLg 里看不到应用
数据库直读SQLCipher 加密,key 提取失败
inotifywait 监控感知新消息到达(但看不到内容)

关键发现

  • PrintWindow API 可以后台截取微信窗口,不需要前台显示
  • WSLg 输入限制:Windows SendInput/mouse_event 无法操作 WSLg 窗口内部(RDP 通道转发限制)
  • 微信数据库:SQLCipher 加密,PyWxDump 等工具已被律师函禁用

踩坑记录

  1. WSL 重启后 GUI 消失:环境变量在 gateway 进程中丢失,需要在 session 中手动 export
  2. Chrome 闪退:DBus 报错 Could not parse server address,需要加 --no-sandbox --disable-gpu
  3. xeyes 验证有效:如果 xeyes 能显示但 Chrome 不行,通常是 Chrome 参数问题
  4. WSLg RDP 输入:WSLg 窗口通过 RDP 映射,xdotool 在 Wayland 下也找不到微信窗口

相关页面