473,480 Members | 2,123 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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.StreamWriter c_StreamInput = null;
private System.IO.StreamReader c_StreamOutput = null;
private Thread c_ThreadRead = null;
private Process c_Process = null;

private void ReadStdOutputThreadProc()
{
while(!c_Process.HasExited)
{
try
{
string str = c_StreamOutput.ReadLine();
while (str != null)
{
c_StreamOutput.BaseStream.Flush();
txtboxCNF.AppendText(str + "\r\n");
Thread.Sleep(100);
str = c_StreamOutput.ReadLine();
}
}
catch (Exception) { }
}
}

private void btnCNF_Click(object sender, System.EventArgs e)
{
if(c_Process == null)
{
c_Process = new Process();
ProcessStartInfo psi = new ProcessStartInfo("console.exe");
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.RedirectStandardInput = true;
psi.CreateNoWindow = true;
c_Process.StartInfo = psi;
c_Process.Start();

c_StreamInput = c_Process.StandardInput;
c_StreamOutput = c_Process.StandardOutput;

c_StreamInput.AutoFlush = true;

c_ThreadRead = new Thread(new ThreadStart(ReadStdOutputThreadProc));
c_ThreadRead.IsBackground = true;
c_ThreadRead.Start();
}
}

private void Form1_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
if (c_ThreadRead != null)
{
c_ThreadRead.Abort();
c_ThreadRead.Join();
}
if(c_Process != null && !c_Process.HasExited)
c_Process.Kill();
}

private void txtboxCNF_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)
{
if(e.KeyValue == 13)
{
c_StreamInput.WriteLine((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.
Feb 28 '06 #1
3 8445
The problem Im facing is that there is not output in the textbox and the
program seems to block and hang....
Feb 28 '06 #2
I think handle OutputDataReceived and ErrorDataReceived process events
is better.

Feb 28 '06 #3
================================================== =
"Yury" wrote:
I think handle OutputDataReceived and ErrorDataReceived process events
is better.

================================================== =
Hi Yury,

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

My code looks like this:

private void CNFOutputHandler(object sendingProcess, DataReceivedEventArgs
outLine)
{
// Collect the sort command output.
if (!String.IsNullOrEmpty(outLine.Data))
{
txtboxCNF.AppendText(outLine.Data + "\r\n");
}
}

private void btnCNF_Click(object sender, System.EventArgs e)
{
if(c_Process == null)
{
c_Process = new Process();
ProcessStartInfo psi = new
ProcessStartInfo("C:\\XPDistribution\\console.exe" );
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;

////
c_Process.OutputDataReceived += new
DataReceivedEventHandler(CNFOutputHandler);
////

psi.RedirectStandardInput = true;
psi.CreateNoWindow = true;
c_Process.StartInfo = psi;
c_Process.Start();

c_StreamInput = c_Process.StandardInput;
c_StreamOutput = c_Process.StandardOutput;

c_StreamInput.AutoFlush = true;

//c_ThreadRead = new Thread(new ThreadStart (ReadStdOutputThreadProc));
//c_ThreadRead.IsBackground = true;
//c_ThreadRead.Start();

c_Process.BeginOutputReadLine();
}
}

Any ideas what I am doing wrong ?

Thanks.
Mar 2 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2551
by: Christophe HELFER | last post by:
hi, I have some problem with redirecting input and output from a process. I can only use VB language (sorry...) Situation: I have to use the Cisco Network Registrar (DNS And DHCP server) ...
1
2250
by: Gerrit Hulleman | last post by:
Is it possible to redirect the standard input for an external application? Something like: ifstream in (...); std::streambufx oldbuf = cin.rdbuf(in.rdbuf()); system(...); cin.rdbuf(oldbuf); ...
15
3063
by: Matt Burland | last post by:
I'm having a problem with redirecting the StandardOutput of a process that I use to run a DOS program in my application. The problem is that I when I start my process (which I do in a separate...
6
4154
by: Christophe Helfer | last post by:
hi, I have some problem with redirecting input and output from a process. Situation: I have to use the Cisco Network Registrar (DNS And DHCP server) command line utility as redirecting its...
3
2371
by: Jan Danielsson | last post by:
Hello, I thought I'd write a program to collect information from pf (packet filter) and insert it into a postgresql database for review on a web page. First I checked if this has been done already,...
116
4504
by: dmoran21 | last post by:
Hi All, I am working on a program to take input from a txt file, do some calculations, and then output the results to another txt file. The program that I've written compiles fine for me, however,...
0
1320
by: carl.dhalluin | last post by:
Hi I want a simple way to interactively remote debug a running python script which has no tty terminal attached to it. The scripts are running standalone (i.e. they are automatically started and...
0
2544
by: kreismaler | last post by:
I have some problems to understand the difference of using the STDOUT and using "anonymous pipes" as shown below: using System; using System.Diagnostics; using System.IO; namespace...
9
1925
by: joe | last post by:
Hello, I have a website set up on our server that is especially for errors. When another website encounters an error, it will redirect to this site with error details in the querystring. The...
0
7048
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7050
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7091
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
6966
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5344
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4488
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2999
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1303
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.