GLib - Spawning Processes



Hi all,

I have spent some time trying to create an application that would use GLib to spawn a process. The function I want to use is "g_spawn_async_with_pipes". The following code is just simplified version of the real one, but the behavior is the same. On Linux it works but on the Windows it gives following error:

------------- output -------------
Command: C:\Windows\notepad.exe
Failed to execute helper program (Invalid argument)
Press any key to continue . . .
------------- output -------------

Thanks for help.

------------- spawn.c -------------

#include <stdio.h>
#include <stdlib.h>
#include <glib.h>

static void watch_pid (GPid pid, gint status, gpointer data);

static GMainLoop* main_loop;

int main (int argc, char *argv[]) {
 GPid pid;
 GError *error = NULL;
 gboolean status;
 gchar *args[2];

 args[0] = g_build_filename ("C:", "Windows", "notepad.exe", NULL);
 args[1] = NULL;

 status = g_spawn_async_with_pipes ( NULL,
                                     args,
                                     NULL,
                                     G_SPAWN_DO_NOT_REAP_CHILD,
                                     NULL,
                                     NULL,
                                     &pid,
                                     NULL,
                                     NULL,
                                     NULL,
                                     &error);

 g_print ("Command: %s\n", args[0]);
 if (status == FALSE) {
   g_print ("%s\n", error->message);
   g_free (args[0]);

   system("PAUSE");

   return EXIT_FAILURE;
 }

 g_child_watch_add (pid, watch_pid, (gpointer)args[0]);
 main_loop = g_main_loop_new (NULL, FALSE);
 g_main_loop_run (main_loop);

 g_free (args[0]);

 system("PAUSE");

 return EXIT_SUCCESS;
}

static void watch_pid (GPid pid, gint status, gpointer data) {
 char *cmd = (char *)data;

 if (status != 0) {
   /* error */
   g_print ("Command \'%s\' failed\n", cmd);
 }
 else {
   /* success */
   g_print ("Command \'%s\' exited\n", cmd);
 }
 g_spawn_close_pid(pid);

 g_main_loop_quit (main_loop);
}

------------- spawn.c -------------



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