G
Guest
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.
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.