Launch an Exe/Process with Stdin Stdout and Stderr

How do I correctly launch a process and forward stdin/stdout/stderr?

Popuplating the STARTUPINFO as shown by the OP works fine if you make sure to not pass the CREATE_NO_WINDOW argument in the dwFlags argument of CreateProcess.

Allow application only stdin, stdout and stderr

Run it under virtual machine (VMWare, VirtualBox etc.) with no hardware enabled. Another option is to use something like Molebox which "virtualizes" an application within real system.

How to redirect stderr on launch *without* redirecting stdin and stdout?

This should work:

STARTUPINFO.hStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
STARTUPINFO.hStdInput = GetStdHandle(STD_INPUT_HANDLE);

Alternatively, if you can't give the parent process a console, you could set hStdOutput and hStdInput to INVALID_HANDLE_VALUE and call through cmd.exe as follows:

cmd /c "subprocess.exe > con < con"

Note that SHGetFileInfo with SHGFI_EXETYPE will tell you whether the executable is a console application or not if you don't already know.

Where are stdin, stdout files located in Windows?

On a Linux kernel, the stdin, stdout and stderr streams have corresponding entries in /proc. That /proc filesystem is an informational structure which provides visibility into the system; it is not the implementation of these streams.

Firstly, stdout is a C concept: an instance of a FILE * I/O stream. The operating system kernel (whether it be Linux or Windows) doesn't know anything about this. These streams hold operating system file descriptors/handles. A Linux or Windows program has a stdout stream due to being linked to a C library, which may not be true of a program that is not written in C, or a C-based language that uses a C run-time.

A process in an a Unix-like operating system has numbered file descriptors, starting at zero. The first three—0, 1 and 2—are, by convention, input, output and error.

In Microsoft Windows, there is a similar concept. A process has three handles of type HANDLE which serve the same purpose. When you create a process using CreateProcess, they are specified in the STARTUPINFO structure which has these members:

HANDLE hStdInput;
HANDLE hStdOutput;
HANDLE hStdError;

which are meaningful if the STARTF_USESTDHANDLES flag is specified.

Microsoft Windows doesn't have a /proc filesystem. It has API-based mechanisms for inspecting various system states. System utilities are written to these API's. For instance, the Handle program can be used for inspecting what processes have what files open.
A similar application on Linux would traverse /proc under the hood.

Transparent logging of child process standard streams (Windows)

A simple version would indeed use pipes. In a native win32 app you would create some inheritable pipes and set them and the STARTF_USESTDHANDLES flag in the STARTUPINFO struct passed to CreateProcess. There is example code for this on MSDN.

You cannot however use this to replace cmd.exe because cmd.exe also uses the special Windows console API in interactive mode to implement features like the F7 history "dialog". You could take a look at some of the free terminal replacements like Console2 to see how they do it.

how to run an application in c++ and write to its stdin and read from its stdout in windows

You can spawn a child process and then gain access to its stdin and stdout pipes. You have to use WinAPI to achieve this. See example here: http://msdn.microsoft.com/en-us/library/ms682499(v=vs.85).aspx

You may use Qt and it's QProcess class to read/write child process output/input.

How to interact with a subprocess through its stdin, stdout, stderr in Smalltalk?

Let me start with that the PipeableOsProcess is probably broken on Windows. I have tried it and it just opened a command line and nothing else (it does not freeze my Pharo 8). The whole OSProcess does not work correctly in my eyes.

So I took a shot at LibC which is supposed to not work with Windows.

I’m a module defining access to standard LibC. I’m available under Linux and OSX, but not under Windows for obvious reasons :)

Next is to say that Python's Windows support is probably much better than Pharo's.

The solution, which is more like a workaround using files, is to use LibC and #runCommand: (I tried to come up with a similar example as you had shown above):

| count command result outputFile errorFile  |

count := 9+1. "The counting"
command := 'echo ', count asString. "command run at the command line"

outputFile := 'output'. "a file into which the output is redirected"
errorFile := 'error'. "a file where the error output is redirected "

result := LibC runCommand: command, "run the command "
' >', outputFile, "redirect the output to output file"
' 2>', errorFile.

"reading back the value from output file"
outputFile asFileReference contents lines.
"reading back the value from the error file - which is empty in this case"
errorFile asFileReference contents lines.


Related Topics



Leave a reply



Submit