5

Running a program in Windows by inputting this in cmd.exe.

./myapp.exe

Then the program looks like running in another thread, the different thread from cmd. How to make the program run via cmd exactly like Linux style. When the cmd terminal destroyed, the running program also terminated. While the program is running, the cmd will capture the std output on time.

2 Answers 2

6

Then the program looks like running in another thread, the different thread from cmd.

Well, the program is running in a separate process, which has its own threads.

On Windows, there are two main types of programs: console and windowed.

Console programs are typically text-mode applications that print text. These already behave the way you want: you run the program, the shell (cmd.exe) waits for it finish before returning you to an interactive prompt. If the program is long-running, forcibly terminating cmd.exe will terminate the program.

Windowed programs are typically applications that have a graphical user interface (e.g. notepad.exe, explorer.exe, firefox.exe). They don't behave the way you want: when you run them through cmd.exe, cmd.exe does not wait for them and returns you to an interactive prompt immediately. The executed program runs independently of cmd.exe, and terminating one has no effect on the other.

Note that console programs can have windows and that windowed programs can have console output. So what makes a program "console" or "windowed"?

Part of the .exe specifies whether it should be run with the "console" subsystem or with the "windows" subsystem. This is configured (often implicitly) when the program was built. This can be changed after the program was built; editbin.exe (included with Visual Studio) can modify the .exe file and change which subsystem the program should use.

If you just want to see stdout and stderr output, you don't need to modify the .exe file. Normally windowed applications don't send that output anywhere, but you can change that with pipes/redirects. For example, you can run:

SomeWindowedApplication.exe > output.txt

or, if you have cat (or an equivalent) available:

SomeWindowedApplication.exe | cat

Doing so also will cause cmd.exe to wait for the program to terminate before returning you to the interactive prompt. Note that this does not have the termination behavior you want (i.e., terminating cmd.exe in the last example will terminate cat.exe, not ShowWindowedApplication.exe).

Also see:

5
  • but GUI apps on Windows won't produce an output because they don't have a console attached when starting up, so redirecting or piping like that will never work. They can create a new console in a new window but that's not the current terminal so redirection also doesn't work
    – phuclv
    Sep 16 at 0:21
  • @phuclv If you run the GUI app from a command prompt, then, as I said, you can redirect stdout and/or stderr to a console app to see output in the current terminal.
    – jamesdlin
    Sep 16 at 2:30
  • as I said, GUI apps on Windows can't attach to a terminal so there's no output for you to see
    – phuclv
    Sep 16 at 2:37
  • @phuclv Have you tried it? This is a technique that I used for many years. You can even make a .cmd script that executes a GUI app and pipes stdout and/or stderr through a cat.exe implementation, and then the script will spawn a console window with the redirected output.
    – jamesdlin
    Sep 16 at 2:39
  • And to be clear: the GUI app is not attaching to a console. Its stdout/stderr are being redirected to a console program that is attached to a console.
    – jamesdlin
    Sep 16 at 2:54
0

Direct answer based on @jamesdlin's answer:

Run in powershell:

$ ./myapp.exe | echo

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .