如何将标准输出重定向到 Windows 应用程序中的某些可见显示? [英] How can I redirect stdout to some visible display in a Windows Application?

查看:24
本文介绍了如何将标准输出重定向到 Windows 应用程序中的某些可见显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以访问提供好东西"的第三方库.它向标准输出发出状态和进度消息.在控制台应用程序中,我可以很好地看到这些消息.在 Windows 应用程序中,它们只是进入比特桶.

I have access to a third party library that does "good stuff." It issues status and progress messages to stdout. In a Console application I can see these messages just fine. In a Windows application they just go to the bit bucket.

是否有一种相当简单的方法可以将 stdout 和 stderr 重定向到文本控件或其他可见位置.理想情况下,这不需要对第三方代码进行任何重新编译.它只会拦截低水平的蒸汽.我想要一个解决方案,我只是 #include 标题,调用初始化函数并链接库,如...

Is there a fairly simple way to redirect stdout and stderr to a text control or other visible place. Ideally, this would not require any recompiles of the third party code. It would just intercept the steams at a low level. I'd like a solution where I just #include the header, call the initialization function and link the library as in...

#include "redirectStdFiles.h"

void function(args...)
{
  TextControl* text = new TextControl(args...);
  initializeRedirectLibrary(text, ...);

  printf("Message that will show up in the TextControl
");
  std::cout << "Another message that also shows up in TextControl
";
}

如果它使用了一些我可以覆盖的界面,这样它就不会绑定到任何特定的 GUI 库,那就更好了.

Even better would be if it used some interface that I could override so it is not tied to any particular GUI library.

class StdFilesRedirector
{
  public:
    writeStdout(std::string const& message) = 0;
    writeStderr(std::string const& errorMessage) = 0;
    readStdin(std::string &putReadStringHere) = 0;
};

我只是在做梦吗?或者有人知道可以做这样的事情吗?

Am I just dreaming? Or does anyone know of something that can do something like this?

在两个答案后我认为使用 freopen 重定向文件是一个很好的第一步.对于完整的解决方案,需要创建一个新线程来读取文件并显示输出.对于调试,在 cygwin shell 窗口中执行tail -f"就足够了.对于更完善的应用程序......这就是我想要写的......创建线程等会有一些额外的工作.

Edit after two answers: I think using freopen to redirect the files is a good first step. For a complete solution there would need to be a new thread created to read the file and display the output. For debugging, doing a 'tail -f' in a cygwin shell window would be enough. For a more polished application... Which is what I want to write... there would be some extra work to create the thread, etc.

推荐答案

您需要创建管道(使用 CreatePipe()),然后使用 SetStdHandle(),然后您可以使用 ReadFile() 并将你从那里得到的文本放在任何你喜欢的地方.

You need to create pipe (with CreatePipe()), then attach stdout to it's write end with SetStdHandle(), then you can read from pipe's read end with ReadFile() and put text you get from there anywhere you like.

这篇关于如何将标准输出重定向到 Windows 应用程序中的某些可见显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆