FreeConsole,然后AttachConsole无法正常工作 [英] FreeConsole then AttachConsole not working

查看:243
本文介绍了FreeConsole,然后AttachConsole无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Windows上使用Visual Studio 2013中的C ++控制台应用程序工作。



首先,我使用FreeConsole分离了控制台,它可以正常工作;然后,我尝试使用AttachConsole将其附加回去,但是什么也没发生-

  #include< psapi.h> 

DWORD winpid = GetCurrentProcessId(); //获得pid
std :: cout<< winpid; //它可以正常工作
FreeConsole(); //控制台丢失了
std :: cout<< 丢到位桶; //没有任何反应,正如预期的那样
AttachConsole(winpid); //尝试找到控制台....
std :: cout<< C; // ...但是失败了

我怎么能找回丢失的控制台?

解决方案

调用FreeConsole()时,控制台将不复存在。您无法调用AttachConsole(),因为没有要附加的内容。您应该改为使用AllocConsole()创建一个新的控制台,然后像这样附加它:

  AllocConsole(); 
文件* f;
freopen_s(& f, CONOUT $, w,标准输出);

然后稍后释放控制台:



< pre class = lang-c ++ prettyprint-override> fclose(f);
FreeConsole();


I'm working with a C++ Console Application in Visual Studio 2013, working on Windows.

First I detached the console using FreeConsole, it works; then, I tried to attach it back using AttachConsole, but nothing happened --

#include <psapi.h>

DWORD winpid = GetCurrentProcessId(); // get pid
std::cout << winpid; // it works    
FreeConsole(); // console lost
std::cout << "Lost to the bit bucket"; //nothing happen, as expected
AttachConsole(winpid); // try find the console back....
std::cout << "c"; // ... but failed

How could I find the lost Console back?

解决方案

When you called FreeConsole(), your console ceases to exist. You cannot call AttachConsole() because there is nothing to attach to. You should instead use AllocConsole() to create a new console and then "attach" to it like so:

AllocConsole();
FILE* f;
freopen_s(&f, "CONOUT$", "w", stdout);

Then to free the console later on:

fclose(f);
FreeConsole();

这篇关于FreeConsole,然后AttachConsole无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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