我在 Windows 上有一个小型 warp
服务器项目,它监听特定端口,并在我通过 REST 向其发送命令时执行某些操作(例如:POST http://10.10 .10.1:5000/打印
)。这是一个小客户端,用于直接从另一台计算机打印 PDF/收据。
它有效。但我的问题是,当我必须打包
整个项目时,Rust 编译器会给我一个可执行文件 (.exe)。当我运行该应用程序时,它会显示一个终端窗口。我希望以某种方式隐藏此终端。
我尝试将该程序作为 Windows 服务运行(通过使用 NSSM)。它对我不起作用,因为我必须访问打印机。 Windows 不允许我的应用程序作为 Windows 服务访问任何设备或任何其他可执行文件。 (原因解释如下:How can I run an EXE program from a Windows Service using C#?)
因此,我计划将我的应用程序作为托盘图标应用程序运行,以便用户可以控制或关闭该应用程序。 (https://github.com/olback/tray-item-rs) 不幸的是,我仍然无法隐藏应用程序的终端窗口。
我发现的另一个解决方案是 hstart
( https://www.ntwind.com/software/hstart.html )。但我想将此作为“最后的手段”解决方案,因为许多防病毒/Windows 防护程序将其标记为恶意软件。
有人知道如何隐藏或摆脱它吗?
最佳答案
经过大量搜索,事实证明比我想象的要容易。只需添加
#![windows_subsystem = "windows"]
位于 main.rs
文件顶部。 (对于 Rust > 1.18)并且终端消失了。
These control the /SUBSYSTEM flag in the linker. For now, only "console" and "windows" are supported.
When is this useful? In the simplest terms, if you're developing a graphical application, and do not specify "windows", a console window would flash up upon your application's start. With this flag, it won't.
https://doc.rust-lang.org/reference/runtime.html#the-windows_subsystem-attribute
https://blog.rust-lang.org/2017/06/08/Rust-1.18.html
https://learn.microsoft.com/en-us/cpp/build/reference/subsystem-specify-subsystem?view=msvc-170
关于windows - 如何在 Windows 中的 Warp 等服务器应用程序上隐藏终端 shell?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71610964/