7

I want to write data to an existing process's stdin from an external process in Windows.

I found similar questions for Linux, but I want to know how I can do the same in Windows.

How to write data to existing process's STDIN from external process?

How do you stream data into the 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

I tried with this code but I got an error. I also tried running the program, sending stdin to that with this code, but again, it errored.

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]

The error I get is:

ReadConsole() failed: The handle 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;
}

Screenshot of windows, front-most says "client.exe has stopped working".

In C#, with the code above, App.exe (command line application which starts the other process) crashes, but in C# application I don't have any exception. I think that is because of UseShellExecute = false.

If I choose "don't run app in background" when I run the C# app, I can find the process and use sendkeys to send my_input_string to it, but this isn't a good idea because the user can see the commandline when it's running the GUI.

How can I send stdin, only using CMD, a python script, or C#, without any errors?

CarenRose
  • 1,266
  • 1
  • 12
  • 24
Sara
  • 209
  • 1
  • 3
  • 10
  • Uh, I'm sorry, but I can't figure out...who is writing to who? Python to C#? C# to Python? Do you want the general case? – NightShadeQueen Jul 01 '15 at 21:03
  • @NightShadeQueen I want a way to write to stdin from python or C# i try for write script in python and run that from C#. – Sara Jul 01 '15 at 21:07
  • Check out the `errorwindow.py` module in [this answer](http://stackoverflow.com/a/18091356/355230) of mine because it does what I think you're trying to do. The major difference is that output from one process is sent to the other by printing it (or writing it to `stderr`). The same module was also used to accomplish something similar in this [other answer](http://stackoverflow.com/a/27932575/355230) of mine to a related question. – martineau Jul 01 '15 at 22:51

2 Answers2

7

If you're launching, and then supplying the input from c#, 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 standard input of an application already running, I doubt that is easy to do with .net classes, as the Process class will not give you the StandardInput (it will instead throw an InvalidOperationException)

Edit: Added parameter to ProcessStartInfo()

Julien Roncaglia
  • 17,397
  • 4
  • 57
  • 75
willaien
  • 2,647
  • 15
  • 24
  • Thnx for your answer buy I try and when i use command line program crashed ! and also no any exception ! do you have any other idea ? – Sara Jul 01 '15 at 21:43
  • Run it in the debugger and give me the exception info, please. – willaien Jul 01 '15 at 21:44
  • Updated question, and add pic, and in C# i dont have any exception ! – Sara Jul 01 '15 at 21:56
  • Oh dear, sorry. I left out a rather important step. See edit. – willaien Jul 01 '15 at 21:57
  • I see and add `startinfo.filename` and `startinfo.Arguments` my exception i in other process after start stoped `Unhandled exception at 0x70FD6ECC (libxml2-2.dll) in client.exe: 0xC0000005: Access violation reading location 0x0000001C.` – Sara Jul 01 '15 at 22:03
-1

This question has been around for a while, but I hope it helps with the answer.

I needed to do something similar, using the aircrack-ng application suite, this suite receives a string through STDIN, the solution I found is available at this link: C# output string to aircrack-ng

Basically what you need is to start your python application, as a sub-process of the C# application.

Assuming that the python app is waiting for STDIN writing, it would look something like this:

public void WriteToAnotherProcess(string textToWrite)
{
    try
    {
        using (System.Diagnostics.Process FirstProcess = new System.Diagnostics.Process())
        {
            var startInfo = new ProcessStartInfo();

            startInfo.RedirectStandardOutput = true;
            startInfo.RedirectStandardInput = true;
            startInfo.FileName = textBox1.Text; // here the full path to the app that is waiting for the STDIN input
            startInfo.Arguments = textBox2.Text; //Arguments for second app
            startInfo.UseShellExecute = false;

            FirstProcess.StartInfo = startInfo;
            FirstProcess.Start();

            StreamWriter FirstProcessWriter = FirstProcess.StandardInput;
            StreamReader FirstProcessReader = FirstProcess.StandardOutput;

            FirstProcess.WriteLine(textToWrite);
            FirstProcess.Close();
            FirstProcess.WaitForExit();

            textBox4.Text = $"The return(STDOUT) from second app is: {FirstProcessReader.ReadToEnd()}";
        }
    }
    catch (Exception ex)
    {
        textBox4.Text = ex.Message + "\r\n" + ex.Source;
    }
}

If your application is crashing during execution, as you mentioned, you can use the property: RedirectStandardError available in ProcessStartInfo.

Getting the error return directly from the python application, so you will be able to understand what is really happening