如何在Visual C ++ 2017 MFC应用程序中重定向stdout并将其还原? [英] How can I redirect stdout, and restore it, in a Visual C++ 2017 MFC application?

查看:211
本文介绍了如何在Visual C ++ 2017 MFC应用程序中重定向stdout并将其还原?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够暂时将stdout重定向到文件,然后将其重新设置为原来的状态。 这是在MFC,Visual C ++ 2017应用程序中。 这一切都发生在一个进程中的一个线程上。 不涉及儿童流程。 
我不想打开控制台窗口或类似的东西。 重定向可能需要在给定的会话中发生任何次数。

I need to be able to temporarily redirect stdout to a file, and later set it back to what it had been.  This is in an MFC, Visual C++ 2017 application.  It all happens on one thread in one process.  No child processes are involved.  I don't want to open a console window or anything like that.  The redirection may need to happen any number of times in a given session.

我发现人们说你应该能够做到这一点的各种方式,但它们都不适合我。  以下是我已尝试的3种变体:



版本1

=========

I have found various ways that people say you should be able to do this, but none of them work for me.  Here are 3 variations that I have already tried:

Version 1
=========

_wfreopen(sTempFile, L"w", stdout);
....
_wfreopen("CON", L"w", stdout);

在调试版本中不向sTempFile输出。  在发布版本中,初始重定向确实有效,但如果在第二次调用_wrfeopen(可能发生)之后有任何尝试将输出发送到stdout,则会导致应用程序崩溃。 顺便说一句,
我试过"CONOUT $"而不是"CON"。 那也行不通(仅限控制台应用程序?)。¥


版本2

======= ==

Get no output to sTempFile in debug build.  In release build, the initial redirect does work, but if anything attempts to send output to stdout after the second call to _wrfeopen (which can happen) it causes the application to crash.  Incidentally I tried "CONOUT$" instead of "CON".  That also didn't work (is that a console application only thing?).

Version 2
=========

int fd = _dup(1);
FILE *pStream = NULL;
_wfopen_s(&pStream, (sTempFile, L"w");
_dup2(_fileno(*pStream ), 1);	// redirect stdout to pStream (1 == "stdout")
....
fclose(pStream);
_dup2(fd , 1); // restore stdout

此版本基于此页面上的示例:
https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/dup-dup2?view=vs-2017
。 它不会造成任何崩溃,但不会似乎在调试或发布版本中都做任何事情。 没有输出到临时文件。



版本3

=========

This version was based on an example on this page: https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/dup-dup2?view=vs-2017 .  It doesn't cause any crashes but doesn't seem to do anything at all in either debug or release build.  Nothing gets output to the temporary file.

Version 3
=========

int fd = _dup(1);
FILE *pStream = _wfreopen(sTempFile, L"w", stdout);
....
fclose(pStream);
_dup2(fd , 1); // restore stdout

两种方法的组合,但与版本1的结果相同。获取否在调试版本中输出到sTempFile。 在发布版本中,初始重定向确实有效,但是如果有任何尝试在调用_dup2之后将输出发送到stdout 
(可能会发生)导致应用程序崩溃。



Combination of the two approaches, but with same result as version 1.  Get no output to sTempFile in debug build.  In release build, the initial redirect does work, but if anything attempts to send output to stdout after the call to _dup2  (which can happen) it causes the application to crash.

我尝试过其他变种但没有他们工作。 我没有尝试使用像GetStdHandle和SetStdHandle这样的控制台API函数,因为它们似乎是为在控制台应用程序中使用而设计的,正如我所说,我的不是控制台应用程序
而且我不想打开一个控制台窗口。 



任何帮助非常感谢。

I have tried other variations but none of them work.  I haven't tried using console API functions like GetStdHandle and SetStdHandle because these appear to be designed for use in a console application, and as I say, mine is not a console application and I don't want to open a console window either. 

Any help much appreciated.

Simon

推荐答案

在windows gui应用程序中,stdin,stdout和stderr句柄是在计划启动时未由CRT初始化。 因此没有理由尝试重置句柄。

In a windows gui application the stdin, stdout, and stderr handles are not initialized by the CRT at program startup.  So there is no reason to try to reset the handles.

例如,试试这个 -

For example, try this -

void CMFCApplication12Dlg::OnBnClickedWrite()
{
	// TODO: Add your control notification handler code here Write
	FILE *fp;

	_wfreopen_s(&fp, L"Test1.txt", L"w", stdout);

	wprintf(L"First file\n");

	fclose(stdout);
}


void CMFCApplication12Dlg::OnBnClickedWrite2()
{
	// TODO: Add your control notification handler code here Append
	FILE *fp;

	_wfreopen_s(&fp, L"Test2.txt", L"w", stdout);

	wprintf(L"Second file\n");

	fclose(stdout);
}


这篇关于如何在Visual C ++ 2017 MFC应用程序中重定向stdout并将其还原?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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