Command without windows?

Hi, I'm new to Rust and is working on a light project using std::command. I'm on windows, and I intended to run the executable silently, so I prefixed it with a macro #![windows_subsystem="windows"]. However whenever it excuted commands, black windows would suddenly appear and disappear. I want to disable all windows, so I wonder if I should directly use ShellExcuteW() in winapi instead? Or are there other approaches?
part of the code:

let mut cmd1=Command::new("cmd");
cmd1.args(["/C", "taskkill -f -im Core.exe"]);
//...
println!("running...");
cmd1.status();// I've tried output(),status(),spawn()

Thank you for any thoughts!

You could use this extension trait to set the creation flags.

1 Like

Basically any platform specific behavior will be exposed through these traits, that only exist when compiling for that platform.

It's up to you to ensure you don't try to compile these on other platforms, for public crates generally with #[cfg(windows)] and #[cfg(unix)] to conditionally include code on those platforms.

Specifically the behavior you're seeing here is that by default Windows will check the process being launched to see if it's a console mode executable, and if so if it has a console to attach it to from the parent process. If not, it will allocate a new one.

I remember fiddling with this, and it can get weird, there's a difference between not having a console at all and having a hidden one, separately from what stdio is attached to, and some rare processes will care a lot about it being one or the other. If nothing else, if they in turn start their own child console process that will again by default allocate a new console if one doesn't exist, so creating with a console but with it hidden may be better. I can't remember if CREATE_NO_WINDOW does that, you might need to drop down to CreateProcess to use STARTUPINFO.wShowWindow.

Another option to try out is to allocate a console for yourself and hide it, so that's the one inherited by the child processes automatically, using AllocConsole, GetConsoleWindow and ShowWindow. This might have a quick flash though, unfortunately.

1 Like

Thank you, it works!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.