C# StandardInput.WriteLine() is sending strange characters to external console application - SOLVED.

Hi,

I am attempting to use StandardInput.WriteLine (System.Diagnostics NameSpace) to send commands to a chess engine exe. It works great except that for some reason some gibberish (nonsense) characters are send with the actual commands which shouldn’t be there. I tested the code in Visual Studio and it works just fine but when running it with Unity the input string seems to obtain some extra baggage. I thought it might be a problem with the chess engine but it does the same when sending strings to the standard windows console and like I said, the code works flawlessly in Visual Studio. The weird characters look like this ∩╗┐.

Any ideas?

Edit - I stumbled upon a workaround. If I use BaseStream.Write instead, send “\n” afterwords and then do StandardInput.Write(KeyCode.Return) it actually works. No gibberish! It’s not pretty but it works for now. I’d still like to find the root of the problem though. It looks like whenever StandardInput tries to send it’s own newline string something gets mangled.

Sounds like an encoding issue. I would like to know the results of writing your string with StandardInput.Write(stringHere) and then StandardInput.Write(Environment.NewLine) immediately after.

Thanks for replying Jim.

Environment.Newline produces the same unwanted characters. The only option that is working right now is per the code below.

            Process UciEngine = new Process();
            UciEngine.StartInfo.FileName = path;
            UciEngine.StartInfo.CreateNoWindow = false;
            UciEngine.StartInfo.UseShellExecute = false;
            UciEngine.StartInfo.RedirectStandardInput = true;
            UciEngine.StartInfo.RedirectStandardOutput = false;
            UciEngine.StartInfo.RedirectStandardError = true;

                     
            if (UciEngine.Start()){

                byte[] buffer = System.Text.Encoding.ASCII.GetBytes("uci");
                UciEngine.StandardInput.BaseStream.Write(buffer, 0, buffer.Length);
                buffer = System.Text.Encoding.ASCII.GetBytes("\n");
                UciEngine.StandardInput.BaseStream.Write(buffer, 0, buffer.Length);
                UciEngine.StandardInput.Write(KeyCode.Return);

            }

If I try do do the same using StandardInput.Write instead I again get the same bad result. StandardOutput was not redirected so I could see what ends up getting send because I wasn’t getting the expected response to the “uci” command. Like I said, it’s odd that the problem only seems to occur in Unity (MonoDevelop) as I have no problems when using the StandardInput.Write command in Visual Studio.

What is the result of StandardInput.Encoding.EncodingName? You probably just need to set the Console.OutputEncoding to match with StandardInput.

StandardInput.Encoding.EncodingName returns UTF-8. I tried setting Console.OutputEncoding to match but the problem remains.

That’s quite odd… I decided to do a little more research on the problem but came up empty. However, the MSDN suggests using a StreamWriter for redirecting the StandardInput stream.

StreamWriter s = process.StandardInput;

s.WriteLine("line");

Thanks Jim, your last post helped to solve the problem. What I needed to do was:

StreamWriter myStream = new StreamWriter(UciEngine.StandardInput.BaseStream, Encoding.ASCII);
myStream.WriteLine("uci");
myStream.Close();

So it was as you expected, and encoding issue. Now I can write pretty code instead of butcher code. :stuck_out_tongue:

1 Like

ASCII it is, thanks a lot