Re: Can't read stdout of bash with GSubprocess on MSYS2
- From: Fan, Chun-wei (范君維) <fanc999 yahoo com tw>
- To: Gang Chen <gang chen cn gmail com>, gtk-list <gtk-list gnome org>
- Subject: Re: Can't read stdout of bash with GSubprocess on MSYS2
- Date: Tue, 29 Sep 2015 13:43:03 +0800
Hi Gang,
You can take a look at https://support.microsoft.com/en-US/kb/105305.
Basically, you need to have a console attached to the GUI app for your
case to work.
By running your code, and by adding AllocConsole() just before you spawn
the subprocess, I was able to get the output.
Hope this helps. With blessings.
Gang Chen 於 2015/9/28 上午 10:01 寫道:
Hi,
I'm writing a GTK+ application with MINGW-W64, which spawns a
subprocess running bash to execute some commands with GSubprocess and
reads stdout of bash (installed from MSYS2). I found that if compiled
without "-mwindows" compilation option the main process can read the
stdout, while with "-mwindows", the read function always returns 0
bytes read. If the main process runs a normal console program, or like
"mingw32/bin/gcc.exe", then it can read stdout even if compiled with
"-mwindows".
The code:
#include <glib.h>
#include <gio/gio.h>
#include <gtk/gtk.h>
#include <stdio.h>
char buffer[100];
GtkTextView *text;
void ondataread(GObject *s, GAsyncResult *r, gpointer p)
{
GError *error =NULL;
int l =g_input_stream_read_finish(G_INPUT_STREAM(s), r, &error);
if (error)
printf("read error: %s\n", error->message);
else if (l >0)
{
gtk_text_buffer_insert_at_cursor(gtk_text_view_get_buffer(text), buffer, l);
g_input_stream_read_async(G_INPUT_STREAM(s), buffer,100, G_PRIORITY_DEFAULT,NULL, ondataread,NULL);
}
}
int main(int argc,char **argv)
{
char *subpargv[8] = {NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL };
subpargv[0] = "D:\\msys64\\usr\\bin\\bash.exe";
subpargv[1] = "-c";
subpargv[2] = "echo good; sleep 10; pwd";
// subpargv[0] = "C:\\Windows\\system32\\cmd.exe";
// subpargv[1] = "/c";
// subpargv[2] = "echo good";
// subpargv[0] = "D:\\msys64\\mingw32\\bin\\gcc.exe";
// subpargv[1] = "notfile";
GSubprocessLauncher *l =g_subprocess_launcher_new(G_SUBPROCESS_FLAGS_STDOUT_PIPE |
G_SUBPROCESS_FLAGS_STDERR_PIPE);
GError *error =NULL;
GSubprocess *p =g_subprocess_launcher_spawnv(l, subpargv, &error);
if (!p)
{
printf("spawn error: %s\n", error->message);
return -1;
}
g_object_unref(l);
g_input_stream_read_async(g_subprocess_get_stdout_pipe(p), buffer,100, G_PRIORITY_DEFAULT,NULL,
ondataread,NULL);
g_input_stream_read_async(g_subprocess_get_stderr_pipe(p), buffer,100, G_PRIORITY_DEFAULT,NULL,
ondataread,NULL);
gtk_init(&argc, &argv);
GtkWidget *win =gtk_window_new(GTK_WINDOW_TOPLEVEL);
GtkWidget *t =gtk_text_view_new();
gtk_container_add(GTK_CONTAINER(win), t);
text =GTK_TEXT_VIEW(t);
gtk_widget_show_all(win);
gtk_main();
return 0;
}
How to read stdout correctly?
Thanks,
Gang
_______________________________________________
gtk-list mailing list
gtk-list gnome org
https://mail.gnome.org/mailman/listinfo/gtk-list
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]