本文档描述了 PowerToys 中用于协调主运行程序和设置 UI 应用程序的进程间通信 (IPC) 系统。这包括基于命名管道的通信机制、JSON 消息协议以及进程生命周期管理。
有关常规设置管理和配置存储的信息,请参阅 设置管理。有关模块加载系统详细信息,请参阅 模块加载系统。
PowerToys 使用双向命名管道 IPC 系统,以实现主 PowerToys 运行程序 (PowerToys.exe) 和设置 UI 应用程序 (PowerToys.Settings.exe) 之间的实时通信。此架构允许设置 UI 作为单独的进程运行,同时与正在运行的 PowerToys 模块保持同步的配置状态。
来源: src/runner/settings_window.cpp34-38 src/runner/settings_window.cpp489-490 src/runner/main.cpp84-94
IPC 系统使用 UUID 创建两个命名管道,动态生成名称,以确保多个 PowerToys 实例之间的唯一性。
管道名称遵循以下模式:
\\.\pipe\powertoys_runner_{uuid}\\.\pipe\powertoys_settings_{uuid}来源: src/runner/settings_window.cpp344-365 src/runner/settings_window.cpp409-421
核心 IPC 功能通过 TwoWayPipeMessageIPC 类实现,该类负责管理双向通信。
来源: src/runner/settings_window.cpp489-491 src/runner/settings_window.cpp520-527
进程间的通信使用 JSON 消息,其中包含特定的消息类型,这些类型会触发接收进程中的不同行为。
| 消息类型 | 方向 | 目的 | 处理函数 |
|---|---|---|---|
general | 设置→运行程序 | 应用常规设置更改 | apply_general_settings() |
powertoys | 设置→运行程序 | 更新模块配置 | dispatch_json_config_to_modules() |
refresh | 设置→运行程序 | 请求当前设置状态 | get_all_settings() |
action | 设置→运行程序 | 执行系统操作 | dispatch_json_action_to_module() |
bugreport | 设置→运行程序 | 启动 bug 报告工具 | launch_bug_report() |
killrunner | 设置→运行程序 | 终止 PowerToys 进程 | 窗口关闭消息 |
来源: src/runner/settings_window.cpp185-244
来源: src/runner/settings_window.cpp57-64 src/runner/settings_window.cpp170-246
设置 UI 进程是按需启动的,并与运行程序保持持久通信,直到用户关闭设置窗口。
来源: src/runner/settings_window.cpp324-535 src/runner/tray_icon.cpp243-247
设置进程从运行程序接收以下参数:
system / dark)true / false)true / false)来源: src/runner/settings_window.cpp409-435
IPC 系统在主 UI 线程上处理消息,以确保线程安全地更新 PowerToys 模块和 UI 状态。
来源: src/runner/settings_window.cpp248-259 src/runner/tray_icon.cpp49-62
PowerToys 还实现了更简单的 IPC 机制,用于通过 Windows 消息在多个 PowerToys 实例之间进行通信。
来源: src/runner/main.cpp84-94 src/runner/main.cpp392-397
IPC 系统包含多项安全措施和错误处理机制,以确保稳健运行。
来源: src/runner/settings_window.cpp482-533 src/runner/settings_window.cpp174-178 src/runner/settings_window.cpp35-36