我怎么能标准输出重定向到一个Windows应用程序的一些可见的显示? [英] How can I redirect stdout to some visible display in a Windows Application?

查看:117
本文介绍了我怎么能标准输出重定向到一个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重定向到一个文本控制或其他可见的地方。理想情况下,这不会要求第三方code中的任何重新编译。它只是截取水蒸气在较低水平。我想一个解决方案,我只是#包含头,调用初始化函数,并...

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\n");
  std::cout << "Another message that also shows up in TextControl\n";
}

更妙的是,如果使用的一些接口,我可以覆盖所以它不依赖于任何特定的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窗口做一个'尾巴-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天全站免登陆