2

I have a small warp server project on Windows that listen to a particular port and do something whenever I send a command to it by REST (for example: POST http://10.10.10.1:5000/print). It's a small client for printing PDF / receipt directly from another computer.

It works. But my problem is when I had to package the whole project, the Rust compiler give me an executable file (.exe). The application displays a terminal window when I run it. I want this terminal to be hidden somehow.

Terminal that I want to get rid of

I try to run the program as a windows service (by using NSSM). It doesn't work for me since I had to access the printer. Windows doesn't allow my app to access any devices or any other executable as a windows service. (The reasons are explained here: How can I run an EXE program from a Windows Service using C#?)

So I plan to run my app as a tray-icon application so user can control or close the app. (https://github.com/olback/tray-item-rs) Unfortunately, I still cannot hide the app's terminal window.

Another solution that I found is hstart (https://www.ntwind.com/software/hstart.html). But I would like to use this as "the last resort" solution since many antivirus/windows defender mark it as a malware.

Try to have a tray app like this

Do anyone know how to hide or get rid of it ?

DennyHiu
  • 4,861
  • 8
  • 48
  • 80

1 Answers1

3

After lot of searching, It turns out to be easier than I thought. Just add

#![windows_subsystem = "windows"]

on top of your main.rs file. (for rust > 1.18) and the terminal is gone.

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

DennyHiu
  • 4,861
  • 8
  • 48
  • 80