Start process with window

Just a simple question, I want to see the actual window popped up when I use std::process::Command. For example:

Command::new("cmd").args(
    &["/c", "echo", "Hello", "&&", "pause"]
);

I would like to see a real cmd window popped up and paused there.

Any way to do this in Rust? Thx!

1 Like

It sounds like this Stack Overflow question may help: Create a new cmd.exe window from within another cmd.exe prompt.

The Command type just creates another process, and because cmd.exe isn't a GUI program (it uses the console subsystem), using Command::new("cmd") to start cmd.exe will just run it in the background.

The trick is to ask Windows to figure out how to run cmd.exe with the equivalent of start cmd /c .... Using start` will automatically create a new console window.

1 Like

Hi thank you for the info tho. However when I run this code:

std::process::Command::new("C:/Windows/System32/start").spawn().unwrap()

, I got

thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Os { code: 193, kind: Other, message: "%1 is not a valid Win32 application." }'

You need to tell start what to execute.

Command::new("start")
    .args(&["cmd", "/c", "echo", "Hello", "&&", "pause"])
    .spawn()
    .unwrap();

Otherwise start will start and be like "what do you want me to run?".

I have tried, but I still get The system cannot find the file specified.

I don't think start is a real Windows program. You might want to try cmd /c start cmd /c my command

2 Likes

I've tried using std::process::Command(), but still not working as I expected.

Does this work on your computer btw?

IIRC cmd /c uses non-standard quote escaping rules, which cannot be satisfied via Command. Command cannot be made work, and you will have to use appropriate Win32 APIs directly.

There's been proposal for .arg_raw, but hasn't been accepted/implemented yet.

1 Like

Could you pls elaborate on the Win32 API and the arg_raw thing ?

I believe he's referring to winapi::um::processthreadsapi::CreateProcessA. This is the underlying Windows API function that is used to start another process.

Unlike *nix where arguments are passed to the process as an array of strings, Windows will pass command-line arguments as one long string (lpCommandLine). I'm guessing arg_raw would be a Windows-specific method added to std::process::Command to set lpCommandLine.

1 Like

arg_raw was proposed here: https://github.com/rust-lang/rust/issues/29494

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.