Reputation: 3409
I was creating a Process using the call:
Process.Start(startInfo);
with
startInfo.WindowStyle = ProcessWindowStyle.Normal;
startInfo.FileName = "cmd.exe";
startInfo.UseShellExecute = true;
startInfo.RedirectStandardInput = false;
startInfo.RedirectStandardOutput = false;
when the process runs with these arguments, it outputs to the new process's window fine.
However, when I make UseShellExecute false without redirecting standard output, the process no longer prints to the screen. Is this the expected behavior?
If so, how can i write to the new window that the process starts?
I can manually get the data from the process using StandardOutput.Read(), but I don't know how to write that to the new window. When I create that window, it appears to have a console by default.
I can use pinvoke to call the C++ function on the new process:
[DllImport("kernel32", SetLastError = true)]
private static extern bool AttachConsole(int dwProcessId);
I think that I may be able to just write to the console of the new window using the function "writeconsole" https://msdn.microsoft.com/en-us/library/windows/desktop/ms687401%28v=vs.85%29.aspx
however, i was wondering if it's possible to do this just using .net libraries. I feel like what I want isn't so complicated that this should be necessary.
All I want is to be able to start a new process and redirect the stdout of that process to the new window that is created in that process, while retaining the ability to write to stdin.
edit: Writing using console.writeline would just write to the main console of the program. My application is a gui app, so to be honest, I don't know exactly where it writes to, but I do know that you can see this output under the tab "output" in visual studio but not in the windows of the program itself. I think that there is a separate buffer allocated by .net for the console.
Upvotes: 1
Views: 1487
Reputation: 2440
You can redirect stdout
and stdin
with freopen.
Example:
freopen ("file.txt","w", stdout); //or stdin
EDIT:
You could also use createpipe()
and read from the end of the pipe. You can tell the pipe, then, where you would like your data to be read to or stored/displayed. If this is not what you are looking for, please elaborate more in depth with a comment.
Upvotes: 1