473,320 Members | 2,122 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Sending StandardInput.WriteLine to a C++ Console Application

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
2 7604
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Daniel | last post by:
after opening socket, sending data then closing socket 3000 times i get "Only one usage of each socket address" what am i doing wrong? is there some thing else i need to do to free up the socket...
3
by: Wayne | last post by:
I have a console application (ssh2.exe) that requires keystrokes to be sent to it after activating. I have worked that it needs to be started in it own thread, but capturing the process and...
1
by: Henk | last post by:
Hi, I have a problem with sending a file. I have a client application that connects to a server and sends the string "data". Then it sends a file. If the server receives "data", the method...
0
by: federico | last post by:
Hello, how can I make a program respond to another program's input request? For example, this code: Process.Start("unrar.exe", "e D:\temp\key.rar D:temp2") SendKeys.SendWait("test~") will...
4
by: Robert McNally | last post by:
Hello, I'm currently learning c# and have been trying to write a simple program with sockets. The problem is i'm trying to send an email with an attachment, (which is the program itself) by...
1
by: noel.phillips | last post by:
Hi, I have two threads in my application, one that decodes audio to a stream and one that encodes a stream to a file. If I run them as two seperate processes I can pipe the output from one to...
3
by: BuddyWork | last post by:
Hello, Could someone please explain why the Socket.Send is slow to send to the same process it sending from. Eg. Process1 calls Socket.Send which sends to the same IP address and port, the...
0
by: Buddy Home | last post by:
There is two examples of code. Example 1. Send and Receive within the same process. Put this code in a console app called SendAndReceive and run the code. using System; using...
1
by: Dv | last post by:
When our application is launched from Dos console, we need to print some message to the console. I attached our application to the console using AttachConsole(ATTACH_PARENT_PROCESS), and then call...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.