c# - 从控制台窗口(C++ 应用程序)重定向到文本框(C# 应用程序)

标签 c# c++ winapi findwindow

我正在创建需要运行另一个应用程序的应用程序 (C#)。这个。应用程序是游戏(C++、Directx7、GDI,我没有源代码),它显示用于从 dll(静态)调试的控制台窗口。 对于显示控制台窗口,此 dll 具有以下行:

AllocConsole();
freopen("CONIN$","rb",stdin);   
freopen("CONOUT$","wb",stdout);
freopen("CONOUT$","wb",stderr);

在我的 C# 应用程序中。我想隐藏控制台窗口并将文本从控制台窗口重定向到文本框。 对于隐藏控制台窗口,我使用的是 winapi FindWindowShowWindow 不是问题。但是我如何将文本(输出)从控制台窗口重定向到文本框?

最佳答案

您可以使用以下代码运行您的游戏:

     Process process = new Process();
     process.StartInfo.FileName = "\"" + pathToGame + "\"";
     //process.StartInfo.Arguments = args;
     process.StartInfo.RedirectStandardOutput = true;
     process.StartInfo.RedirectStandardError = true;
     process.StartInfo.UseShellExecute = false;
     process.OutputDataReceived += new DataReceivedEventHandler(ReadOutput);
     process.ErrorDataReceived += new DataReceivedEventHandler(ReadOutput);

     process.Start();
     process.BeginOutputReadLine();
     process.BeginErrorReadLine();

     //process.WaitForExit();

CL 输出和错误将放在此处

  private static void ReadOutput(object sender, DataReceivedEventArgs e)
  {
     if (e.Data != null)
     {
        //your output here
     }
  }

关于c# - 从控制台窗口(C++ 应用程序)重定向到文本框(C# 应用程序),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14171363/

相关文章:

c++ - Win32 : How to hide 3rd party windows in taskbar by hWnd

c - ReadFile COM 全批

c# - 在单元测试期间显示 winform

c# - app.config 中的 FTP 连接

c++ - 在字符串中使用删除函数是否会使迭代器无效

c++ - 两个 Sprite 在单独列表中的碰撞

c - 在Windows上用C编写自修改代码,无法编写补丁

c# - CreateUserWithEmailAndPasswordAsync 错误 "Object reference not set to an instance of an object."

c# - 使用 iText 阅读 PDF 批注

c++ - ReSharper C++ 代码分析不起作用