当前位置: 首页 > news >正文

rust或tauri项目执行命令的时候,cmd窗口也会弹出显示解决方法

阻止 Tauri 执行命令时弹出 CMD 窗口

当你在 Tauri 中使用 tokio::process::Command 执行命令时弹出 CMD 窗口,这是因为 Windows 默认会为控制台程序创建可见窗口。以下是几种解决方法:

1. 使用 Windows 特有的创建标志 (推荐)

#[tauri::command]
pub async fn run_command(command: String) -> Result<String, String> {let output = if cfg!(target_os = "windows") {use std::os::windows::process::CommandExt;tokio::process::Command::new("powershell").arg("-Command").arg(&command).creation_flags(0x08000000) // CREATE_NO_WINDOW 标志.output().await.map_err(|e| e.to_string())?} else {tokio::process::Command::new("sh").arg("-c").arg(&command).output().await.map_err(|e| e.to_string())?};// 其余代码不变...
}

2. 使用 cmd.exe 的静默参数

#[tauri::command]
pub async fn run_command(command: String) -> Result<String, String> {let output = if cfg!(target_os = "windows") {tokio::process::Command::new("cmd").args(&["/C", "start", "/B", &command])  // /B 参数阻止窗口显示.output().await.map_err(|e| e.to_string())?} else {// Linux/macOS 代码不变...};// 其余代码不变...
}

3. 使用 PowerShell 的隐藏窗口参数

#[tauri::command]
pub async fn run_command(command: String) -> Result<String, String> {let output = if cfg!(target_os = "windows") {tokio::process::Command::new("powershell").args(&["-WindowStyle", "Hidden", "-Command", &command]).output().await.map_err(|e| e.to_string())?} else {// Linux/macOS 代码不变...};// 其余代码不变...
}

4. 完全隐藏所有子进程窗口 (Windows)

如果你想要确保所有子进程都不会显示窗口:

#[tauri::command]
pub async fn run_command(command: String) -> Result<String, String> {let output = if cfg!(target_os = "windows") {use std::os::windows::process::CommandExt;let mut cmd = tokio::process::Command::new("powershell");cmd.arg("-Command").arg(&command).creation_flags(0x08000000); // CREATE_NO_WINDOW// 对于可能需要 GUI 的程序if command.contains(".exe") {cmd.creation_flags(0x08000000 | 0x00000010); // CREATE_NO_WINDOW + CREATE_NEW_PROCESS_GROUP}cmd.output().await.map_err(|e| e.to_string())?} else {// Linux/macOS 代码不变...};// 其余代码不变...
}

注意事项

  1. CREATE_NO_WINDOW 标志 (0x08000000) 是最可靠的方法
  2. 某些程序可能需要窗口才能正常运行(如 GUI 程序)
  3. 在 Linux/macOS 上不需要特殊处理,因为默认就不会显示终端窗口
  4. 如果命令需要管理员权限,隐藏窗口可能会被 UAC 提示中断

最佳实践

第一种方法(使用 CREATE_NO_WINDOW 标志)是最可靠和推荐的解决方案,因为它:

  • 直接告诉 Windows 不要创建窗口
  • 不影响命令执行
  • 适用于大多数情况
  • 不需要修改命令本身

如果你仍然看到窗口弹出,可能是:

  1. 被执行的程序本身强制显示窗口
  2. 命令中包含了会启动新窗口的子命令(如 start
  3. 需要管理员权限导致 UAC 提示
http://www.lqws.cn/news/159931.html

相关文章:

  • OpenCV 图像通道的分离与合并
  • Windows+Cmake编译Opencv-python
  • 【数据结构】树形结构--二叉树(二)
  • docker 搭建php 开发环境 添加扩展redis、swoole、xdebug(2)
  • Palo Alto Networks Expedition存在命令注入漏洞(CVE-2025-0107)
  • nodejs里面的http模块介绍和使用
  • Ubuntu系统配置C++的boost库(含filesystem模块)的方法
  • ASP.NET Core使用Quartz部署到IIS资源自动被回收解决方案
  • 【.net core】.KMZ文件解压为.KML文件并解析为GEOJSON坐标数据集。附KML处理多线(LineString)闭环问题
  • 37.第二阶段x64游戏实战-封包-寻找Socket套接字
  • 最新研究揭示云端大语言模型防护机制的成效与缺陷
  • 【.net core】天地图坐标转换为高德地图坐标(WGS84 坐标转 GCJ02 坐标)
  • SpringBoot项目移动之后无法运行
  • vue-18(使用 Vuex 插件实现高级功能)
  • 03.01、三合一
  • MyBatis-Plus深度全解:从入门到企业级实战
  • 【Elasticsearch】Elasticsearch 核心技术(二):映射
  • 力扣100题之128. 最长连续序列
  • el-tabs 切换时数据不更新的问题
  • 6月5日day45
  • 群晖NAS如何在虚拟机创建飞牛NAS
  • 基于STM32的DS18B20温度远程监测LCD1602显示
  • STL优先级队列的比较函数与大堆小堆的关系
  • LoRA:大模型高效微调的低秩之道——原理解析与技术实现
  • 代码随想录算法训练营第九天| 151.翻转字符串里的单词、55.右旋转字符串 、字符串总结
  • 25.6.5学习总结
  • day47 TensorBoard学习
  • label-studio的使用教程(导入本地路径)
  • 优化学习笔记
  • 热门消息中间件汇总