views:

442

answers:

5

I'm using the following code

System::Diagnostics::Process^ p = gcnew System::Diagnostics::Process();
p->StartInfo->FileName =  "tnccmd.exe";
p->StartInfo->UseShellExecute = false;
p->StartInfo->RedirectStandardInput = true;
p->StartInfo->RedirectStandardOutput = true; 
p->Start();
System::IO::StreamWriter^ tnc_stdin = p->StandardInput;
System::IO::StreamReader^ tnc_stdout = p->StandardOutput;

tnc_stdin->WriteLine("connect i 127.0.0.1");
String^ prg_output = tnc_stdout->ReadToEnd();

My problem is that I cannot read stdout correctly. I can easily write to stdin however, but now I'm trying to implement some error checking code and it doesnt work.

The program I'm using doesn't seem to write to stdout even if it is made to run in command line. I can reproduce the "bug" with ftp.exe which comes with windows xp by default. If you change the ->FileName with "ftp.exe" the command prompt ftp.exe usually gives "ftp>" will not show up in prg_output.

Now I know that the prompt must use some kind of "windows shell curses" and I may be mixing up problems.

Normaly just after the "connect i 127.0.0.1" instruction I'm supposed to received "connecting to 127.0.0.1..." but I receive nothing.

Any hint on what I'm doing wrong? Is there another kind of "stdout" that I'm not aware of?

EDIT

I cannot use arguments because I have multiple lines to write, much like with ftp.exe. Also, ftp.exe does output when you type commands like dir. At least it outputs when you write unknown commands, it complains about "Invalid command."

A: 

I suspect that you're trying to send to stdin what should actually be a command-line argument. How would you normally invoke tnccmd.exe? Something like this?

tnccmd.exe connect i 127.0.0.1

If that's the case, then "connect i 127.0.0.1" shouldn't go on stdin, but should be passed via p->StartInfo->Arguments.

(The problem with ftp.exe isn't with your program, but rather ftp.exe itself, which finds out if its stdout is the console. If its output is not on the console, then it does not output the "ftp>" prompt. It's also possible that the program you're trying to script does the same thing.)

JSBangs
A: 

maybe it is an issue with buffering.

What happens if you try to flush tnc_stdin ? try something like this:

tnc_stdin->WriteLine("connect i 127.0.0.1");
tnc_stdin->Flush();

Edit: Checked the ctor of StreamWriter that you are using(Reflector rules!) According to it, the default buffer size is 1024 bytes... so you need to flush :-) or you can define a smaller buffer.

    public StreamWriter(string path) : 
this(path, false, new UTF8Encoding(false, true), 0x400)
    {
    }
Asher
Thank you for your answer.However StreamWriter does work, the problem arise with StreamReader
Eric
A: 

I think that you forgot to call BeginOutputReadLine

Shay Erlichmen
A: 

I am trying to do exactly the same thing (In C) with tnccmd.exe and have the same problem. I think it's related to some kind of buffer on stdout because if you run it from cygwin at the command like this

"C:/Program Files/HEIDENHAIN/TNCremoNT/tnccmd.exe" > xx.txt

and then type loads of

? ?

eventually the help text starts to appear in xx.txt but not all of it. Then when you type EXIT the final text appears as the buffer is cleared. Did you ever solve this as I'd like to know how so I can implement it in C.

Chris Hobson
I did not solve it. I handled the errors with a timeout instead i think. I since left that job =/
Eric
A: 

so pathetic, u are noobs i cant really find answer to that.

problem is that ftp.exe use WriteConsole (instead of WriteFile wich will use WriteConsole only is stdout = console handle, otherwise NtWriteFile) to access screenbuffer directly, bypassing stdout/err.

So, do you have any idea how to redirect console? Like telnet do that?

rgerg