By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,732 Members | 1,388 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,732 IT Pros & Developers. It's quick & easy.

Sending StandardInput.WriteLine to a C++ Console Application

P: n/a
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

Dec 15 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
I don't think that's the issue tbh. If you have a look here,
http://msdn2.microsoft.com/en-us/lib...dardinput.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.
nautonnier wrote:
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
Dec 15 '06 #2

P: n/a
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

DeveloperX wrote:
I don't think that's the issue tbh. If you have a look here,
http://msdn2.microsoft.com/en-us/lib...dardinput.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.
nautonnier wrote:
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
Dec 15 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.