How to enable debug console?
Unsolved
General and Desktop
-
In my case, sometime I will need the console to show log for my QT process and C++ DLL library.
I found the some code and show console from Internet.
// detach from the current console window // if launched from a console window, that will still run waiting for the new console (below) to close // it is useful to detach from Qt Creator’s <Application output> panel FreeConsole(); // create a separate new console window AllocConsole(); // attach the new console to this application’s process AttachConsole(GetCurrentProcessId()); SetConsoleOutputCP(65001); // reopen the std I/O streams to redirect I/O to the new console freopen("CON", "w", stdout); freopen("CON", "w", stderr); freopen("CON", "r", stdin);
This is my example code
mainwindow.cpp
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); lib.setFileName("ExampleDLL.dll"); if(!lib.load()) { qDebug() << "load ExampleDLL.dll library failed. " << lib.errorString(); } else { qDebug() << "loaded ExampleDLL.dll library"; } printHelloWorldFun = (printHelloWorld)lib.resolve("printHelloWorld"); } void MainWindow::on_DebugButton_clicked() { // detach from the current console window // if launched from a console window, that will still run waiting for the new console (below) to close // it is useful to detach from Qt Creator’s <Application output> panel FreeConsole(); // create a separate new console window AllocConsole(); // attach the new console to this application’s process AttachConsole(GetCurrentProcessId()); SetConsoleOutputCP(65001); // reopen the std I/O streams to redirect I/O to the new console freopen("CON", "w", stdout); freopen("CON", "w", stderr); freopen("CON", "r", stdin); } void MainWindow::on_QTButton_clicked() { fprintf(stdout, "Hello World from QT\n"); } void MainWindow::on_DLLButton_clicked() { printHelloWorldFun(); }
My DLL example code:
void printHelloWorld() { fprintf(stdout, "Hello World from DLL\n"); }
It sometimes works and sometimes doesn't.
How to fix this issue?
Do everyone have any idea?Thanks
-
@ChiaoHuang
Just in case opening aCON
is still block-buffered, put afflush(stdout)
after yourfprintf(stdout, "...\n")
statements.