Sending StandardInput.WriteLine to a C++ Console Application

N

nautonnier

Hello,

I have a C# app that spawns several processes each containing a console
app written by another developer in C++. The console app was written
first to be just like a console app: it starts, you type a command it
does a command and returns a message. I thought I would be able to
consume it in my C# app by using the Process.StandardInput in
conjunction with the Process.StartInfo.RedirectStandardOutput. The
output works fine and I can receive messages. But for some reason the
C++ app doesn't seem to be getting the messages. Here is what my side
of the code started out as:

private void StreamInput(string text)
{
encoderProcess.StandardInput.WriteLine(text);
encoderProcess.StandardInput.Flush();
}

But it wasn't getting the message. I read on another post that the C++
app would be expecting ASCII instead of the .NET's default Unicode so I
rewrote it like so:

private void StreamInput(string text)
{
byte[] byteData = Encoding.ASCII.GetBytes(text);
char[] charData = Encoding.ASCII.GetChars(byteData);
encoderProcess.StandardInput.WriteLine(charData);
encoderProcess.StandardInput.Flush();
}

But it *still* doesn't seem to be getting the message. Is there
something obvious that I'm not doing correctly or does the other
developer need to dig in to his side? INO, what does it take for both
side to play well with each other?

TIA,

nautonnier
 
D

DeveloperX

I don't think that's the issue tbh. If you have a look here,
http://msdn2.microsoft.com/en-us/library/system.diagnostics.process.standardinput.aspx
you will notice that besides using a streamwriter, they don't fiddle
with the encoding and it still works fine with sort.exe which is old as
the hills.
Do you have access to the C++ source? It might be worth paring down one
of the app's and have it just write what ever it gets on stdin to a
file.
Another test you can do is to pipe output from your app into the other
app which you could do with a little test harness app. Pick one message
you would want to send, write a console app that simply does

Console.WriteLine("message");

then run the app from a dos box as dotnettestapp.exe | cppApp.exe and
see what happens.
 
N

nautonnier

Thanks DevX. You were right. I don't think it was the way I was sending
the message. I think it was more to with the way the c++ program was
accepting the standard stream. I made a test app in c++ and I was able
to send stuff.

However, now I'm having another (much less important) issue in my c++
test app. It sends messages saying "working" via cout every second
(after a thread Sleep command) while another thread is waiting on the
cin. For the c# side, I have a test harness using the RedirectConsole
app that's out there (basically a window with a textbox for sending
commands and a textbox for receiving messages over the standardouput
thread). It's pretty well multithreaded but I never get the "working"
messages. Any ideas?

tia,

nautonnier
I don't think that's the issue tbh. If you have a look here,
http://msdn2.microsoft.com/en-us/library/system.diagnostics.process.standardinput.aspx
you will notice that besides using a streamwriter, they don't fiddle
with the encoding and it still works fine with sort.exe which is old as
the hills.
Do you have access to the C++ source? It might be worth paring down one
of the app's and have it just write what ever it gets on stdin to a
file.
Another test you can do is to pipe output from your app into the other
app which you could do with a little test harness app. Pick one message
you would want to send, write a console app that simply does

Console.WriteLine("message");

then run the app from a dos box as dotnettestapp.exe | cppApp.exe and
see what happens.

Hello,

I have a C# app that spawns several processes each containing a console
app written by another developer in C++. The console app was written
first to be just like a console app: it starts, you type a command it
does a command and returns a message. I thought I would be able to
consume it in my C# app by using the Process.StandardInput in
conjunction with the Process.StartInfo.RedirectStandardOutput. The
output works fine and I can receive messages. But for some reason the
C++ app doesn't seem to be getting the messages. Here is what my side
of the code started out as:

private void StreamInput(string text)
{
encoderProcess.StandardInput.WriteLine(text);
encoderProcess.StandardInput.Flush();
}

But it wasn't getting the message. I read on another post that the C++
app would be expecting ASCII instead of the .NET's default Unicode so I
rewrote it like so:

private void StreamInput(string text)
{
byte[] byteData = Encoding.ASCII.GetBytes(text);
char[] charData = Encoding.ASCII.GetChars(byteData);
encoderProcess.StandardInput.WriteLine(charData);
encoderProcess.StandardInput.Flush();
}

But it *still* doesn't seem to be getting the message. Is there
something obvious that I'm not doing correctly or does the other
developer need to dig in to his side? INO, what does it take for both
side to play well with each other?

TIA,

nautonnier
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top