I need to send an input from a C# program to another program and capture its process's output every time after the input is sent
Here I wrote a C program to be used as a process in C# program. The program will receive an input and print back the same string
#include <stdio.h>
#include <stdbool.h>
int main(){
while(true){
char buff[1000];
scanf(" %[^\n]s", buff);
printf("\nechoed : %s\n",buff);
}
}
This is my C# program that will send input and receive output to and from my C program (I am starting the process in another thread so process.WaitForExit() will not block the main thread)
using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
namespace MinExample
{
internal class Program
{
static StreamWriter processStreamWriter;
static string filePath = @"my_program.exe";
private static void MyProcOutputHandler(object sendingProcess,
DataReceivedEventArgs outLine)
{
if (!String.IsNullOrEmpty(outLine.Data))
{
Console.WriteLine("Data : " + outLine.Data);
}
}
//this doesn't works
static void StartProcessWithOutputRedirection()
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.FileName = filePath;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
//standard input out redirection
startInfo.RedirectStandardOutput = true;//redirect output
startInfo.RedirectStandardError = true;//redirect output
startInfo.RedirectStandardInput = true;
try
{
//Initialize process and register callback
Process process = new Process();
process.StartInfo = startInfo;
process.OutputDataReceived += new DataReceivedEventHandler(MyProcOutputHandler);
process.ErrorDataReceived += new DataReceivedEventHandler(MyProcOutputHandler);
//
process.Start();
//initialize stream writer so that the process can receive incoming input
processStreamWriter = process.StandardInput;
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
//this works
static void StartProcessNoOutputRedirection()
{
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.FileName = filePath;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
//standard input out redirection
startInfo.RedirectStandardOutput = false;
startInfo.RedirectStandardInput = true;
try
{
//Initialize process and set property
Process process = new Process();
process.StartInfo = startInfo;
//
process.Start();
//initialize stream writer so that the process can receive incoming input
processStreamWriter = process.StandardInput;
process.WaitForExit();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
static void Main(string[] args)
{
/*
using StartProcessNoOutputRedirection works but using StartProcessWithOutputRedirection doesn't
*/
ThreadStart ths = new ThreadStart(StartProcessWithOutputRedirection);
Thread th = new Thread(ths);
th.Start();
while (true)
{
Console.Write("Input :");
string input = Console.ReadLine() ?? "";
processStreamWriter.WriteLine(input);
}
}
}
}
It will print the output each time the process receives an output when I don't redirect the output at all
However when I tried to redirect the output, the Process.OutputDataReceived event isn't being called back. I have searched another post and have tried to use process.BeginOutputReadLine(); after process.Start but it hasn't solved my issue
Any advice?