From 9913736b41f311af71200577ca7bc0d03d5c91a4 Mon Sep 17 00:00:00 2001 From: SoftFever Date: Mon, 28 Apr 2025 23:27:20 +0800 Subject: [PATCH] Fixed a bug that console window was created when --datadir parameter is used (#9506) * attach console for Windows when command line is used * fix version check failer in command line mode * attach console only when we need to print help info --- src/OrcaSlicer.cpp | 83 ++++++++++++++++++++++++---------------------- 1 file changed, 43 insertions(+), 40 deletions(-) diff --git a/src/OrcaSlicer.cpp b/src/OrcaSlicer.cpp index bc3a18500a..4ed29f3790 100644 --- a/src/OrcaSlicer.cpp +++ b/src/OrcaSlicer.cpp @@ -6032,8 +6032,51 @@ bool CLI::setup(int argc, char **argv) return true; } +void attach_console_on_demand(){ + static bool console_attached = false; + + if (!console_attached) { + // Try attaching to the parent console first + if (AttachConsole(ATTACH_PARENT_PROCESS)) { + console_attached = true; + } else if (GetLastError() == ERROR_ACCESS_DENIED) { + // Already has a console (maybe attached by debugger) + console_attached = true; + } else { + // No parent console found, try allocating a new one + if (AllocConsole()) { + console_attached = true; + } + } + + if (console_attached) { + FILE* fp = nullptr; + // Redirect standard C streams to the console + if (freopen_s(&fp, "CONOUT$", "w", stdout) == 0) { + setvbuf(stdout, NULL, _IONBF, 0); // Optional: Disable buffering + } + if (freopen_s(&fp, "CONOUT$", "w", stderr) == 0) { + setvbuf(stderr, NULL, _IONBF, 0); // Optional: Disable buffering + } + if (freopen_s(&fp, "CONIN$", "r", stdin) == 0) { + // Input redirection successful + } + // Sync C++ streams with C streams after redirection + std::ios::sync_with_stdio(true); + // Clear potential error states from C++ streams + std::cout.clear(); + std::cerr.clear(); + std::cin.clear(); + boost::nowide::cout.clear(); + boost::nowide::cerr.clear(); + boost::nowide::cin.clear(); + } + } +} void CLI::print_help(bool include_print_options, PrinterTechnology printer_technology) const { + attach_console_on_demand(); + boost::nowide::cout << SLIC3R_APP_KEY <<"-"<< SoftFever_VERSION << ":" << std::endl @@ -6298,46 +6341,6 @@ extern "C" { int *a = nullptr; *a = 0; }); - bool cli_mode = argc > 1; // Simple check for CLI mode - bool console_attached = false; - - if (cli_mode) { - // Try attaching to the parent console first - if (AttachConsole(ATTACH_PARENT_PROCESS)) { - console_attached = true; - } else if (GetLastError() == ERROR_ACCESS_DENIED) { - // Already has a console (maybe attached by debugger) - console_attached = true; - } else { - // No parent console found, try allocating a new one - if (AllocConsole()) { - console_attached = true; - } - } - - if (console_attached) { - FILE* fp = nullptr; - // Redirect standard C streams to the console - if (freopen_s(&fp, "CONOUT$", "w", stdout) == 0) { - setvbuf(stdout, NULL, _IONBF, 0); // Optional: Disable buffering - } - if (freopen_s(&fp, "CONOUT$", "w", stderr) == 0) { - setvbuf(stderr, NULL, _IONBF, 0); // Optional: Disable buffering - } - if (freopen_s(&fp, "CONIN$", "r", stdin) == 0) { - // Input redirection successful - } - // Sync C++ streams with C streams after redirection - std::ios::sync_with_stdio(true); - // Clear potential error states from C++ streams - std::cout.clear(); - std::cerr.clear(); - std::cin.clear(); - boost::nowide::cout.clear(); - boost::nowide::cerr.clear(); - boost::nowide::cin.clear(); - } - } // Call the UTF8 main. return CLI().run(argc, argv_ptrs.data()); }