Redirecting Standard Input Interactively

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sudesh

    #1

    Redirecting Standard Input Interactively

    Hi,
    I am a newbie to C# and Im trying to redirect standard input, output and
    error of a console program written in C (MS VC 6.0) to a textbox on a form.
    The code for the redirecting looks like this:

    private System.IO.Strea mWriter c_StreamInput = null;
    private System.IO.Strea mReader c_StreamOutput = null;
    private Thread c_ThreadRead = null;
    private Process c_Process = null;

    private void ReadStdOutputTh readProc()
    {
    while(!c_Proces s.HasExited)
    {
    try
    {
    string str = c_StreamOutput. ReadLine();
    while (str != null)
    {
    c_StreamOutput. BaseStream.Flus h();
    txtboxCNF.Appen dText(str + "\r\n");
    Thread.Sleep(10 0);
    str = c_StreamOutput. ReadLine();
    }
    }
    catch (Exception) { }
    }
    }

    private void btnCNF_Click(ob ject sender, System.EventArg s e)
    {
    if(c_Process == null)
    {
    c_Process = new Process();
    ProcessStartInf o psi = new ProcessStartInf o("console.exe" );
    psi.UseShellExe cute = false;
    psi.RedirectSta ndardOutput = true;
    psi.RedirectSta ndardInput = true;
    psi.CreateNoWin dow = true;
    c_Process.Start Info = psi;
    c_Process.Start ();

    c_StreamInput = c_Process.Stand ardInput;
    c_StreamOutput = c_Process.Stand ardOutput;

    c_StreamInput.A utoFlush = true;

    c_ThreadRead = new Thread(new ThreadStart(Rea dStdOutputThrea dProc));
    c_ThreadRead.Is Background = true;
    c_ThreadRead.St art();
    }
    }

    private void Form1_Closing(o bject sender,
    System.Componen tModel.CancelEv entArgs e)
    {
    if (c_ThreadRead != null)
    {
    c_ThreadRead.Ab ort();
    c_ThreadRead.Jo in();
    }
    if(c_Process != null && !c_Process.HasE xited)
    c_Process.Kill( );
    }

    private void txtboxCNF_KeyDo wn(object sender,
    System.Windows. Forms.KeyEventA rgs e)
    {
    if(e.KeyValue == 13)
    {
    c_StreamInput.W riteLine((char) e.KeyValue+"\0\ n");
    }
    }

    The console application that I am trying to run (console.exe) is a simple C
    program that prints a line text and then calls the gets() function. The code
    is basically this:

    #include "stdafx.h"
    #include <conio.h>
    #include <string.h>

    void create_it();

    int main(int argc, char* argv[])
    {
    create_it();

    return 1;
    }

    void create_it()
    {
    char str_nodes[256], str_filename[256], str_username[256], str_serial[256];

    printf("Welcome to the console program\n");

    /* Display maximum number of nodes */
    printf ("Max Nodes : ");
    gets( str_nodes);

    /* get file name */
    printf ("Enter user configuration file name : ");
    gets (str_filename);

    /* get user name */
    printf ("User Name : ");
    gets (str_username);

    /* get serial number */
    printf ("Serial Id : ");
    gets (str_serial);
    }

    Thanks in advance for your help.
  • Sudesh

    #2
    RE: Redirecting Standard Input Interactively

    The problem Im facing is that there is not output in the textbox and the
    program seems to block and hang....

    Comment

    • Yury

      #3
      Re: Redirecting Standard Input Interactively

      I think handle OutputDataRecei ved and ErrorDataReceiv ed process events
      is better.

      Comment

      • Sudesh

        #4
        Re: Redirecting Standard Input Interactively

        =============== =============== =============== ======
        "Yury" wrote:[color=blue]
        > I think handle OutputDataRecei ved and ErrorDataReceiv ed process events
        > is better.
        >
        >[/color]
        =============== =============== =============== ======


        Hi Yury,

        I have tried this, but I keep getting the following error message at the
        call: c_Process.Begin OutputReadLine( )
        "Cannot mix synchronous and asynchronous operation on process stream.".

        My code looks like this:

        private void CNFOutputHandle r(object sendingProcess, DataReceivedEve ntArgs
        outLine)
        {
        // Collect the sort command output.
        if (!String.IsNull OrEmpty(outLine .Data))
        {
        txtboxCNF.Appen dText(outLine.D ata + "\r\n");
        }
        }

        private void btnCNF_Click(ob ject sender, System.EventArg s e)
        {
        if(c_Process == null)
        {
        c_Process = new Process();
        ProcessStartInf o psi = new
        ProcessStartInf o("C:\\XPDistri bution\\console .exe");
        psi.UseShellExe cute = false;
        psi.RedirectSta ndardOutput = true;

        ////
        c_Process.Outpu tDataReceived += new
        DataReceivedEve ntHandler(CNFOu tputHandler);
        ////

        psi.RedirectSta ndardInput = true;
        psi.CreateNoWin dow = true;
        c_Process.Start Info = psi;
        c_Process.Start ();

        c_StreamInput = c_Process.Stand ardInput;
        c_StreamOutput = c_Process.Stand ardOutput;

        c_StreamInput.A utoFlush = true;

        //c_ThreadRead = new Thread(new ThreadStart (ReadStdOutputT hreadProc));
        //c_ThreadRead.Is Background = true;
        //c_ThreadRead.St art();

        c_Process.Begin OutputReadLine( );
        }
        }

        Any ideas what I am doing wrong ?

        Thanks.

        Comment

        Working...