简体   繁体   English

FreeConsole,然后AttachConsole无法正常工作

[英]FreeConsole then AttachConsole not working

I'm working with a C++ Console Application in Visual Studio 2013, working on Windows. 我正在Windows上使用Visual Studio 2013中的C ++控制台应用程序。

First I detached the console using FreeConsole, it works; 首先,我使用FreeConsole分离了控制台,它可以工作; then, I tried to attach it back using AttachConsole, but nothing happened -- 然后,我尝试使用AttachConsole将其附加回去,但没有任何反应-

#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. 当您调用FreeConsole()时,您的控制台将不复存在。 You cannot call AttachConsole() because there is nothing to attach to. 您无法调用AttachConsole(),因为没有要附加的内容。 You should instead use AllocConsole() to create a new console and then "attach" to it like so: 您应该改用AllocConsole()创建一个新的控制台,然后像这样“附加”到它:

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

Then to free the console later on: 然后稍后释放控制台:

fclose(f);
FreeConsole();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM