_open_osfhandle always returns error when passing GetStdHandle(STD_OUTPUT_HANDLE) to it. However, STD_ERROR_HANDLE works fine.
full code snippet:
HANDLE output_handle = GetStdHandle(STD_OUTPUT_HANDLE);
if (output_handle==INVALID_HANDLE_VALUE)
{
std::stringstream ss;
ss << "GetStdHandle return " << GetLastError();
OutputDebugString(ss.str().c_str());
}
else
{
std::cout.sync_with_stdio(true);
int m_nCRTOut= _open_osfhandle( (intptr_t)output_handle, 0 );
if (m_nCRTOut==-1)
{
std::stringstream ss;
ss << "_open_osfhandle(" <<(int) output_handle << ") return " << errno << ", "
<< strerror( errno);
OutputDebugString(ss.str().c_str()); // _open_osfhandle(7) return 9, Bad file descriptor
}
else
{
FILE* m_fpCRTOut = _fdopen( m_nCRTOut, "w" );
if (!m_fpCRTOut)
{
std::stringstream ss;
ss << "_fdopen(" <<(int) m_nCRTOut << ") return " << errno << ", "
<< strerror( errno);
OutputDebugString(ss.str().c_str());
}
else
{
*stdout = *m_fpCRTOut;
setvbuf( stdout, NULL, _IONBF, 0 );
}
}
}
std::cout.clear();
Btw, a VK_RETURN key event needs to be programmatically fired in order to detach from console.
if (m_attach_console)
{
FreeConsole();
m_attach_console = FALSE;
//simulate return pressing
INPUT input;
memset(&input,0,sizeof(input));
input.type=INPUT_KEYBOARD;
input.ki.wVk=VK_RETURN;
SendInput(1,&input,sizeof(input));
input.ki.dwFlags=KEYEVENTF_KEYUP;
SendInput(1,&input,sizeof(input));
}