AllocConsole ();
Freopen (" CONOUT $", "w", stdout);
In MFC window procedure, the above two sentences to open a console window associated with the program, and to cout output stream to redirect to the console
Two lines of code above in OnInitDialog (), you can open the console, cout, printf is normal output
If (m_consoleOpend)
{
HWND hconsole=GetConsoleWindow ();
FreeConsole ();
: : PostMessage (hconsole WM_CLOSE, 0, 0);
M_consoleOpend=false;
}
The else
{
AllocConsole ();
The FILE * f=freopen (" CONOUT $", "w", stdout);
If (f==NULL)
{
MessageBox (" redirect failed ");
}
M_consoleOpend=true;
Cout & lt; <"Open the console" & lt;Printf (" open the Console ");
}
In the interface button click event in the above code, you can open and close the console, printf can normal output, cout cannot output, the if (f==NULL) is false;
If first in OnInitDialog () open the console, and then click the button to close, click the button again to open, so cout printf can output
Why is that? How can I click on the button cout can also output to the console?