FreeConsole then AttachConsole not working

跟風遠走 提交于 2019-12-25 07:59:07

问题


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?


回答1:


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();


来源:https://stackoverflow.com/questions/40059618/freeconsole-then-attachconsole-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!