Write to stdin of a running process on windows
I want to write data to an existing process STDIN
from external processes on Windows and found a similar question for linux:
How do I write data to the STDIN of an existing process from an external process?
How do you pass data to STDIN of a program from different local / remote processes in Python?
https://serverfault.com/questions/443297/write-to-stdin-of-a-running-process-using-pipe
and so on, but now I want to know how to do this on Windows?
I am trying with this code, but I got an error!
also i try to run the program and send stdin to this cod but again error!
In CMD:
type my_input_string | app.exe -start
my_input_string | app.exe -start
app.exe -start < pas.txt
In python:
p = subprocess.Popen('"C:\app.exe" -start',
stdin=subprocess.PIPE, universal_newlines=True, shell=True)
grep_stdout = p.communicate(input='my_input_string')[0]
Mistake:
ReadConsole () error: The descriptor is invalid.
And in C #:
try
{
var startInfo = new ProcessStartInfo();
startInfo.RedirectStandardInput = true;
startInfo.FileName = textBox1.Text;
startInfo.Arguments = textBox2.Text;
startInfo.UseShellExecute = false;
var process = new Process();
process.StartInfo = startInfo;
process.Start();
Thread.Sleep(1000);
var streamWriter = process.StandardInput;
streamWriter.WriteLine("1");
}
catch (Exception ex)
{
textBox4.Text = ex.Message+"\r\n"+ex.Source;
}
In C # with this code App.exe
(command line app starting with new process) crashed! but in a C # application I have no exception at all!
and I think for UseShellExecute = false;
Also, when I use C#
, and if not run the application in the background , I can find the process and use from sendkeys
to submit my_input_string
, but this is not a good idea because the user sees the command line when using the GUI!
how can I submit STDIN
without errors with CMD only, or create a script in python or C #!
Any ideas???
Sincerely.
source to share
If you run and then supply the C # input, you can do something like this:
var startInfo = new ProcessStartInfo("path/to/executable");
startInfo.RedirectStandardInput = true;
startInfo.UseShellExecute = false;
var process = new Process();
process.StartInfo = startInfo;
process.Start();
var streamWriter = process.StandardInput;
streamWriter.WriteLine("I'm supplying input!");
If you need to write to the stdin of an already running application, I doubt it's easy to do with .net classes, since the Process class won't give you StandardInput
(instead it will throw out InvalidOperationException
)
Edit : Added parameterProcessStartInfo()
source to share