2
votes

I have a console application (Host.exe) that is written in Delphi. It accepts stdin readln and responses to stdout by writeln. Now, I want to use Host.exe in C# application in a way that C# gives input to Host.exe and gets the output from Host.exe Ideally, I write the code below but it doesn't work: it hangs somewhere in the outputReader.ReadLine();

System.IO.File.WriteAllText(tmp, vbs);

Process pProcess = new Process();

pProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal;

//strCommand is path and file name of command to run
pProcess.StartInfo.FileName = @"Host.exe";
pProcess.StartInfo.Arguments = "\"runa " + tmp +"\"";
// runs script file tmp in background
pProcess.StartInfo.CreateNoWindow = true;
pProcess.StartInfo.UseShellExecute = false;
pProcess.StartInfo.RedirectStandardOutput = true;
pProcess.StartInfo.RedirectStandardInput = true;
pProcess.StartInfo.RedirectStandardError = true;

pProcess.Start();

StreamWriter inputWriter = pProcess.StandardInput;
StreamReader outputReader = pProcess.StandardOutput;

while (true)
{
    inputWriter.WriteLine("getmsg");
    inputWriter.Flush();
    string s = outputReader.ReadLine(); // then do something with it

    inputWriter.WriteLine("progressglobal");
    inputWriter.Flush();

    string p = outputReader.ReadLine();
    if (p == "100")
    {
        break;
    }
    Application.DoEvents();
    Thread.Sleep(1000);
}
inputWriter.WriteLine("exit");
inputWriter.Flush();                    
pProcess.WaitForExit();

Many thanks for any suggestions in advance !

1
Does your Delphi application return a string with a new line symbol? If not, you application is waiting forever. Or maybe you should debug the Delphi app, what does is it do when the request comes in? - The_Fox
Is this a text encoding issue? Are you writing UTF-16 in C#, but expecting ANSI on the other side of the pipe? - David Heffernan
Yes, my Delphi application uses writeln to output string with new line every time... - justyy
not sure how to change encoding issue in C# and Delphi? - justyy
The Delphi code uses ANSI almost certainly. That's what Writeln and Readln in Delphi do, even in Unicode enabled Delphi. What did you see when you ran the Delphi app under the debugger? - David Heffernan

1 Answers

0
votes

You read the line twice:

string s = outputReader.ReadLine();

and

string p = outputReader.ReadLine();

It seems you only need the last line as the variable s is not used.