[gtk/wip/otte/win32-cleanup: 63/63] win32: Make prints reach the console




commit c3781b05f197c116cba6541e9c8f06b100c5836a
Author: Benjamin Otte <otte redhat com>
Date:   Wed Jul 28 01:57:49 2021 +0200

    win32: Make prints reach the console
    
    GUI apps on Windows don't have a console and console apps open a command
    prompt when not launched from a terminal.
    
    This code keeps the app as a GUI app not opening a command prompt but
    tries to reattach to the parent, so that stdout and stderr reach it.

 demos/widget-factory/widget-factory.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
---
diff --git a/demos/widget-factory/widget-factory.c b/demos/widget-factory/widget-factory.c
index 0c4b21c81c..2a6c0a5e62 100644
--- a/demos/widget-factory/widget-factory.c
+++ b/demos/widget-factory/widget-factory.c
@@ -27,6 +27,10 @@
 
 #include "demo_conf.h"
 
+#ifdef GDK_WINDOWING_WIN32
+#include <windows.h>
+#endif
+
 static void
 change_dark_state (GSimpleAction *action,
                     GVariant      *state,
@@ -2464,6 +2468,15 @@ main (int argc, char *argv[])
   };
   int status;
 
+#ifdef GDK_WINDOWING_WIN32
+  if (AttachConsole (ATTACH_PARENT_PROCESS))
+    {
+      freopen("CONOUT$", "w", stdout);
+      freopen("CONOUT$", "w", stderr);
+      freopen("CONIN$", "r", stdin);
+    }
+#endif
+
   app = gtk_application_new ("org.gtk.WidgetFactory4", G_APPLICATION_NON_UNIQUE);
 
   g_action_map_add_action_entries (G_ACTION_MAP (app),


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]