Call AllocConsole() at the beginning of your application. Only one console can be allocated for a process.
Next, call WriteConsole to show a text.
void CMyApp::LogDebug(LPCSTR p) { if(m_bAppDebug) { DWORD d; WriteConsoleA( GetStdHandle(STD_OUTPUT_HANDLE), (CONST VOID *)p, strlen(p), &d, NULL ); WriteConsoleA( GetStdHandle(STD_OUTPUT_HANDLE), (CONST VOID *)"\r\n", 2, &d, NULL ); } } |
I don’t know why but WriteConsole has Anscii and Unicode definition. Is there any differences?
I didn’t do a research for that now.
And at the end of your application, call FreeConsole().
You can also call WriteFile instead of WriteConsole, in this case the function can be used for both the console and a file.
In both cases, there is a chance standard handle is not as what you expected because the calling process can inherit or put a security on it. For example, if the application using above code was run from cygwin, the GetStdHandle() would fail.