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, called AllocConsole
as FreeConsole then AttachConsole not working suggested, returns true
meaning success; last, 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
bool succeed = AllocConsole(); //succeeded.
succeed = AttachConsole(winpid); // return false: failed.
if (!succeed)
LastError = GetLastError(); // Error Code 5
System Error Code 5 means:
ERROR_ACCESS_DENIED
5 (0x5)
Access is denied.
How shall I attach the console properly?