问题
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