Process I/O re-direction

G

Guest

I work in a group that uses a variety of third party tools to control an
embedded system. I need to integrate the functionality of these tools
(mostly command prompt) into a single interface.

I followed the examples on MSDN and other sites to create a class that
spawns a System.Diagnostics.Process with re-directed StdIn, StdOut, and
StdErr. I read the user input from a textbox and write the output to another
textbox. This approach works for some of the tools but I've hit a brick wall.

I'd like to be able to use this approach for all applications, basically
integrate a "cmd" shell into the bottom of my application.

We use telnet and an application called rshell quite a bit. I can spawn a
"cmd" process into the window and do simple commands like "dir". However, I
cannot start telnet from within the my fake terminal window.

When I try to directly start a telnet process and redirecting the IO no
output is displayed and my ProcessExit handler is fired with a return code of
-1. I found some references on the web that stated that Telnet does not use
the standard IO pipes and as such can't be re-directed in this manner.

Thanks in advance. Here is the code:

<code>
{
private ProcessStartInfo m_psi;
private Process m_p;

m_psi.UseShellExecute = false;
m_psi.RedirectStandardOutput = true;
m_psi.RedirectStandardError = true;
m_psi.RedirectStandardInput = true;
m_psi.CreateNoWindow = true;
m_p.StartInfo = m_psi;

m_p.Start();
m_p.OutputDataReceived += new
DataReceivedEventHandler(ProcessOutputHandler);

m_p.ErrorDataReceived += new
DataReceivedEventHandler(ProcessErrorHandler);

m_p.Exited += new EventHandler(m_p_Exited);

m_p.BeginOutputReadLine();
m_p.BeginErrorReadLine();
m_p.EnableRaisingEvents = true;

StdIn = m_p.StandardInput;
StdIn.AutoFlush = true;
}
</code>

<code>

private void ProcessOutputHandler(Object sender, DataReceivedEventArgs
line)
{
if (!String.IsNullOrEmpty(line.Data))
{

String append = line.Data + System.Environment.NewLine;

OutputString.Append(append);


//Update output textbox if it exists
if (OutputBox != null)
{
if (OutputBox.InvokeRequired)
{
DataReceivedEventHandler d =
new
DataReceivedEventHandler(ProcessOutputHandler);
OutputBox.Invoke(d, new object[] { sender, line });
}
else
{
OutputBox.AppendText(append);
}
}
}
}



private void ProcessErrorHandler(Object sender, DataReceivedEventArgs
line)
{
if (!String.IsNullOrEmpty(line.Data))
{
String append = System.Environment.NewLine + line.Data;

ErrorString.Append(append);

if (OutputBox != null)
{
//Update output textbox if it exists
if (OutputBox.InvokeRequired)
{
DataReceivedEventHandler d =
new DataReceivedEventHandler(ProcessErrorHandler);
OutputBox.Invoke(d, new object[] { sender, line });
}
else
{
OutputBox.AppendText(append);
}
}
}
}
</code>
 
G

Guest

a_wahoo said:
I work in a group that uses a variety of third party tools to control an
embedded system. I need to integrate the functionality of these tools
(mostly command prompt) into a single interface.

I followed the examples on MSDN and other sites to create a class that
spawns a System.Diagnostics.Process with re-directed StdIn, StdOut, and
StdErr. I read the user input from a textbox and write the output to another
textbox. This approach works for some of the tools but I've hit a brick wall.

I'd like to be able to use this approach for all applications, basically
integrate a "cmd" shell into the bottom of my application.

We use telnet and an application called rshell quite a bit. I can spawn a
"cmd" process into the window and do simple commands like "dir". However, I
cannot start telnet from within the my fake terminal window.

When I try to directly start a telnet process and redirecting the IO no
output is displayed and my ProcessExit handler is fired with a return code of
-1. I found some references on the web that stated that Telnet does not use
the standard IO pipes and as such can't be re-directed in this manner.

That sounds correct.

Consider if you telnet into a system and start using a full screen
text editor.

Telnet allows you to run console apps but it is not a console
app itself.

You need a telnet/terminal emulator library.

Check what is available on the net, including:

http://dotnettelnet.sourceforge.net/
http://www.thoughtproject.com/Libraries/Telnet/
http://www.codeproject.com/cs/internet/terminalcontrol.asp

Arne
 
G

Guest

Thanks Arne but that doesn't quite give me what I need. I knew I could use a
third party library to handle just telnet. The problem is I need to create
something that is robust enough to handle telnet in addition to a wide
variety of third party console apps.

As I've been researching this problem I've come up with a couple of
different potential solutions but haven't been able to test them out yet.
One is creating a host environment for Windows Powershell. That seems to
have some promise.

The other option involves spawning a cmd window, hiding it, and then using
the Windows API to get data from it. I only read about that particular
option on a forum and haven't been able to find any follow up information or
examples regarding it.

Any other ideas would be greatly appreciated.

Thanks
 
G

Guest

a_wahoo said:
Thanks Arne but that doesn't quite give me what I need. I knew I could use a
third party library to handle just telnet. The problem is I need to create
something that is robust enough to handle telnet in addition to a wide
variety of third party console apps.

A telnet client and a way of describing expected output from the
telnet window and what to send to it.

Similar to Kermit scripts (if you are old enough to remember such).

I think I would just code a little script interpreter in C#
and then write scripts for each app.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top