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