3

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 doesn't 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.

MTM
  • 919
  • 10
  • 22
Eric
  • 19,525
  • 19
  • 84
  • 147

4 Answers4

0

See this blog post about capturing both standard Out and Err from managed applications. The CLR makes is very easy to do the wrong thing and deadlock yourself.

How to use System.Diagnostics.Process correctly

josh poley
  • 7,236
  • 1
  • 25
  • 25
0

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.)

JSBձոգչ
  • 40,684
  • 18
  • 101
  • 169
0

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
  • 1,867
  • 15
  • 24
  • Thank you for your answer. However StreamWriter does work, the problem arise with StreamReader – Eric Nov 25 '08 at 21:44
0

I think that you forgot to call BeginOutputReadLine

Shay Erlichmen
  • 31,691
  • 7
  • 68
  • 87