diff --git a/packages/flutter_tools/templates/app/windows.tmpl/Runner.vcxproj.filters b/packages/flutter_tools/templates/app/windows.tmpl/Runner.vcxproj.filters index aff51229de..761db86287 100644 --- a/packages/flutter_tools/templates/app/windows.tmpl/Runner.vcxproj.filters +++ b/packages/flutter_tools/templates/app/windows.tmpl/Runner.vcxproj.filters @@ -27,6 +27,9 @@ Source Files + + Source Files + Source Files @@ -53,6 +56,9 @@ Header Files + + Header Files + Header Files diff --git a/packages/flutter_tools/templates/app/windows.tmpl/Runner.vcxproj.tmpl b/packages/flutter_tools/templates/app/windows.tmpl/Runner.vcxproj.tmpl index 6a0b3fb796..884b512772 100644 --- a/packages/flutter_tools/templates/app/windows.tmpl/Runner.vcxproj.tmpl +++ b/packages/flutter_tools/templates/app/windows.tmpl/Runner.vcxproj.tmpl @@ -227,6 +227,7 @@ + @@ -238,6 +239,7 @@ + diff --git a/packages/flutter_tools/templates/app/windows.tmpl/runner/main.cpp b/packages/flutter_tools/templates/app/windows.tmpl/runner/main.cpp index c428b4b3f5..11b48e9573 100644 --- a/packages/flutter_tools/templates/app/windows.tmpl/runner/main.cpp +++ b/packages/flutter_tools/templates/app/windows.tmpl/runner/main.cpp @@ -4,19 +4,19 @@ #include "flutter_window.h" #include "run_loop.h" +#include "utils.h" #include "window_configuration.h" -int APIENTRY wWinMain(_In_ HINSTANCE instance, - _In_opt_ HINSTANCE prev, - _In_ wchar_t* command_line, - _In_ int show_command) { +int APIENTRY wWinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE prev, + _In_ wchar_t *command_line, _In_ int show_command) { // Attach to console when present (e.g., 'flutter run') or create a // new console when running with a debugger. if (!::AttachConsole(ATTACH_PARENT_PROCESS) && ::IsDebuggerPresent()) { - ::AllocConsole(); + CreateAndAttachConsole(); } - // Initialize COM, so that it is available for use in the library and/or plugins. + // Initialize COM, so that it is available for use in the library and/or + // plugins. ::CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED); RunLoop run_loop; diff --git a/packages/flutter_tools/templates/app/windows.tmpl/runner/utils.cpp b/packages/flutter_tools/templates/app/windows.tmpl/runner/utils.cpp new file mode 100644 index 0000000000..37501e5db7 --- /dev/null +++ b/packages/flutter_tools/templates/app/windows.tmpl/runner/utils.cpp @@ -0,0 +1,22 @@ +#include "utils.h" + +#include +#include +#include +#include + +#include + +void CreateAndAttachConsole() { + if (::AllocConsole()) { + FILE *unused; + if (freopen_s(&unused, "CONOUT$", "w", stdout)) { + _dup2(_fileno(stdout), 1); + } + if (freopen_s(&unused, "CONOUT$", "w", stderr)) { + _dup2(_fileno(stdout), 2); + } + std::ios::sync_with_stdio(); + FlutterDesktopResyncOutputStreams(); + } +} diff --git a/packages/flutter_tools/templates/app/windows.tmpl/runner/utils.h b/packages/flutter_tools/templates/app/windows.tmpl/runner/utils.h new file mode 100644 index 0000000000..d247a66308 --- /dev/null +++ b/packages/flutter_tools/templates/app/windows.tmpl/runner/utils.h @@ -0,0 +1,8 @@ +#ifndef CONSOLE_UTILS_H_ +#define CONSOLE_UTILS_H_ + +// Creates a console for the process, and redirects stdout and stderr to +// it for both the runner and the Flutter library. +void CreateAndAttachConsole(); + +#endif // CONSOLE_UTILS_H_