I create a MFC dialog based application. In VS2013, I could create a console window and output message. When I upgraded to VS2017, execute same code, the console window is created but no message output. Below is my code:
bool Initialize(void)
{
HWND hWnd = GetConsoleWindow();
if (NULL != hWnd)
{
return true;
}
if (!AllocConsole())
{
return false;
}
HANDLE m_hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
if (INVALID_HANDLE_VALUE == m_hStdOut)
{
return false;
}
int m_hCrt = _open_osfhandle((intptr_t)m_hStdOut, _O_TEXT);
if (-1 == m_hCrt)
{
return false;
}
FILE* m_pCrtFile = _fdopen(m_hCrt, "w");
*stdout = *m_pCrtFile;
int ret = setvbuf(stdout, NULL, _IONBF, 0);
if (-1 == ret)
{
return false;
}
return true;
}
void WriteLine(LPCTSTR lpszText)
{
Initialize();
std::wcout << lpszText;
std::wcout << std::endl;
std::wcout.flush();
system("pause");
}
BOOL CMFCApplication1App::InitInstance()
{
WriteLine(_T("test"));
...
}
Question: How should I modify my code to output message to console in VS2017?