尝试在Windows上重定向stdout和stderr-_fileno(stdout)返回-2 [英] Trying to redirect stdout and stderr on Windows - _fileno(stdout) returning -2

查看:427
本文介绍了尝试在Windows上重定向stdout和stderr-_fileno(stdout)返回-2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的第一个问题!

我打算将一些可以正常工作的C ++代码从UNIX移植到Windows,这将通过管道将stdout和stderr重定向到自定义GUI组件.我需要它来显示来自第三方库的反馈,该库仅在GUI上将消息输出到stdout.

I'm about to port some piece of fine working C++ code from UNIX to Windows, which redirects stdout and stderr to a custom GUI Component through pipes. I need it to display feedback from a third party library that only outputs messages to stdout on my GUI.

根据此答案 https://stackoverflow.com/a/617158 ,这应该可以工作.实际上,来自链接的代码在使用Visual Studio 2017构建的全新命令行应用程序中按预期工作.但是,在我现有的GUI应用程序中,对_fileno(stdout)_fileno(stderr)的调用均返回-2而不是预期值1和2,所以什么也没有发生.对相关功能进行硬编码1和2也不起作用.

According to this answer https://stackoverflow.com/a/617158 this should work. In fact the code from the link works as expected in a fresh command line application built with Visual Studio 2017. However in my existing GUI application the call to _fileno(stdout) as well as to _fileno(stderr) both return -2 instead of the expected values 1 and 2, so just nothing is happening. Hard coding 1 and 2 to the relevant functions doesn't make it working either.

所以任何人都可以解释

  • 什么返回值-2可以确切表示? (对此,Google进行了大量搜索-没有成功)
  • 在没有控制台的情况下,是否有可能在Windows上实现我想要的功能?
  • 如果可能的话,有什么相关的步骤使它起作用?

为您提供信息,到目前为止,我已经收集了我在Linux和Mac OS上进行编程的主要经验,因此对于有经验的Windows用户而言,这可能是显而易见的. GUI应用程序基于JUCE框架,因此我使用的是JUCE工具Projucer创建的自动生成的Visual Studio项目

For your information, I gathered my main experience on programming until now on Linux and Mac OS, so this might be something obvious for experienced Windows guys. The GUI application is based on the JUCE framework, so I'm using an auto-generated Visual Studio project created by the JUCE tool Projucer

推荐答案

与Unix类型的系统不同,在Windows中,控制台应用程序和GUI应用程序是两个不同的东西. GUI应用程序没有stdin,stdout或stderr的概念-它们仅了解Windows,文本框等.

Unlike Unix-type systems, in Windows a console application and a GUI application are two different things. GUI applications don't have the concept of stdin, stdout or stderr - they only know about windows, textboxes etc.

文档_fileno说:

如果stdout或stderr与输出流不关联(对于 例如,在没有控制台窗口的Windows应用程序中,该文件 返回的描述符是-2.

If stdout or stderr is not associated with an output stream (for example, in a Windows application without a console window), the file descriptor returned is -2.

因此,如果要使用stdout和stderr,则需要将应用程序构建为控制台应用程序(仍然可以使用GUI).

So if you want to use stdout and stderr, you need to build your application as a console application (you can still use a GUI).

另一种方法是继续将应用程序构建为GUI,但可以通过调用 AllocConsole()在应用程序的开始(即,您在WinMain()中所做的第一件事),然后将stdout和stderr文件关联:

An alternative method is to carry on building the application as a GUI, but allocate a console by calling AllocConsole() at the very start of your application (i.e. the first thing you do in WinMain()) and then associate the stdout and stderr files:

if(AllocConsole()){
    freopen("CONOUT$", "w", stdout);
    freopen("CONOUT$", "w", stderr);
}

这篇关于尝试在Windows上重定向stdout和stderr-_fileno(stdout)返回-2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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