Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embedding a DOS console in a windows form

Is it possible to embed a DOS console in a Windows Form or User Control in C# 2.0?

We have a legacy DOS product that my Windows app has to interact with, and it's been requested that an instance of the legacy product should run within the Windows application.

At the moment, I'm using the user32.dll to locate the window that the DOS product is running in, minimising then maximising the window, and typing characters into the window. This isn't a very good solution to the problem, as it means my application has to store the window name in application settings, and requires that the user returns to the correct page of the DOS app before using the interaction function.

EDIT: Some more information

The legacy app needs to be visible to the user, but not in a separate window.

I've tried TimothyP's answer and it works very well, but is it possible to achieve the same functionality, but with the DOS window visually embedded in a windows form or user control instead of popping up in it's own window? Preferably in a ShowDialog() way so that the user cannot interact with the app wile they are in 'Legacy Mode', so to speak.

like image 696
Mark Withers Avatar asked Dec 10 '08 11:12

Mark Withers


People also ask

How do I add a console application to Windows form?

You need to create a Windows application. Then right click the project and go to properties and set the Output Type for the application to Console Application. This will then not only show your form but also the Console Window. So now you can use the Console class to read in or display messages to that console Window.

How do I add a component to Windows form?

Add the control by drawing Select the control by clicking on it. In your form, drag-select a region. The control will be placed to fit the size of the region you selected.


2 Answers

It's possible to redirect the standard input/output of console/dos applications using the Process class. It might look something like this:

var processStartInfo = new ProcessStartInfo("someoldapp.exe", "-p someparameters");

processStartInfo.UseShellExecute = false;
processStartInfo.ErrorDialog = false;

processStartInfo.RedirectStandardError = true;
processStartInfo.RedirectStandardInput = true;
processStartInfo.RedirectStandardOutput = true;
processStartInfo.CreateNoWindow = true;

Process process = new Process();
process.StartInfo = processStartInfo;
bool processStarted = process.Start();

StreamWriter inputWriter = process.StandardInput;
StreamReader outputReader = process.StandardOutput;
StreamReader errorReader = process.StandardError;
process.WaitForExit();

You can now use the streams to interact with the application. By setting processStartInfo.CreateNoWindow to true the original application will be hidden.

I hope this helps.

like image 55
TimothyP Avatar answered Nov 16 '22 08:11

TimothyP


You can use the CreateProcess function and the hStdInput, Output, and Error members of the STARTUPINFO argument, this will allow you to intercept standard input and output of the application.

like image 28
Frans-Willem Avatar answered Nov 16 '22 07:11

Frans-Willem