Embedding a DOS console in a windows form

前端 未结 3 1269
盖世英雄少女心
盖世英雄少女心 2020-11-27 16:34

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 b

相关标签:
3条回答
  • 2020-11-27 16:44

    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.

    0 讨论(0)
  • 2020-11-27 16:51

    Concerning your question on how to display the DOS app inside the Windows app.

    There are a few solutions.

    • The first one is to simply not display the DOS app (with CreateNoWindow) and "simulate" the UI of the DOS app in your Windows application by reading and writing to the streams.

    • The other solution would be to use the Win32API, get the Windows Handle (Whnd) of the Console/DOS application window and set its parent to your form. I'm currently not at home and since it has been ages since I've done this, I can't remember by heart how it is done. If I'm not mistaken you'll need to use the following Win32 API calls:

      • FindWindow
      • GetWindow
      • SetParent

    If I have some time left later today, I'll see if I can find better samples.

    0 讨论(0)
  • 2020-11-27 16:59

    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.

    0 讨论(0)
提交回复
热议问题