[Win32] Fix the asynchronous g_spawn* to return the process handle of the

2002-11-18  Tor Lillqvist  <tml@iki.fi>

	[Win32] Fix the asynchronous g_spawn* to return the process handle
	of the started program properly. (Note: not the process id. The
	spawn*() functions in the C runtime return the created process's
	handle. There doesn't seem to be any way to get the process id of
	a child process if you have the handle. But then, the process
	handle usually is more useful anyway.)

	* glib/gspawn-win32-helper.c (WinMain): If the spawning of the
	child process succeeded, and if asynchronous spawn (P_NOWAIT),
	write the result handle up to the parent process, waiting to read
	it in do_spawn_with_pipes().

	* glib/gspawn-win32.c (do_spawn): Use return value from spawning
	the helper. If it is -1 the helper wasn't found or couldn't be run
	for some reason. Otherwise it is the helper's process handle.

	(g_spawn_async_with_pipes): Pass the child_pid parameter on to
	do_spawn_with_pipes().

	(do_spawn_with_pipes): Take also a child_pid parameter. If
	do_spawn() returned -1, fail immediately. Otherwise make the
	handle passed to us by the helper process into a handle valid in
	this process by calling DuplicateHandle().
This commit is contained in:
Tor Lillqvist
2002-11-17 23:30:32 +00:00
committed by Tor Lillqvist
parent 1f04f2cce2
commit 0b4bcbe1d9
9 changed files with 269 additions and 44 deletions

View File

@@ -70,7 +70,9 @@ WinMain (struct HINSTANCE__ *hInstance,
int i;
int fd;
int mode;
gint zero = 0;
int handle;
int no_error = CHILD_NO_ERROR;
int zero = 0;
SETUP_DEBUG();
@@ -203,10 +205,12 @@ WinMain (struct HINSTANCE__ *hInstance,
{
debugstring = g_string_new ("");
g_string_append (debugstring,
g_strdup_printf ("calling %s on program %s, __argv: ",
g_strdup_printf ("calling %s %s mode=%s argv: ",
(__argv[ARG_USE_PATH][0] == 'y' ?
"spawnvp" : "spawnv"),
__argv[ARG_PROGRAM]));
__argv[ARG_PROGRAM],
(mode == P_WAIT ?
"P_WAIT" : "P_NOWAIT")));
i = ARG_PROGRAM+1;
while (__argv[i])
{
@@ -218,17 +222,29 @@ WinMain (struct HINSTANCE__ *hInstance,
}
if (__argv[ARG_USE_PATH][0] == 'y')
{
if (spawnvp (mode, __argv[ARG_PROGRAM], __argv+ARG_PROGRAM) < 0)
write_err_and_exit (child_err_report_fd, CHILD_SPAWN_FAILED);
}
handle = spawnvp (mode, __argv[ARG_PROGRAM], __argv+ARG_PROGRAM);
else
handle = spawnv (mode, __argv[ARG_PROGRAM], __argv+ARG_PROGRAM);
if (debug)
{
if (spawnv (mode, __argv[ARG_PROGRAM], __argv+ARG_PROGRAM) < 0)
write_err_and_exit (child_err_report_fd, CHILD_SPAWN_FAILED);
debugstring = g_string_new ("");
g_string_append (debugstring,
g_strdup_printf ("%s returned %#x",
(__argv[ARG_USE_PATH][0] == 'y' ?
"spawnvp" : "spawnv"),
handle));
MessageBox (NULL, debugstring->str, "gspawn-win32-helper", 0);
}
write (child_err_report_fd, &zero, sizeof (zero));
write (child_err_report_fd, &zero, sizeof (zero));
if (handle < 0)
write_err_and_exit (child_err_report_fd, CHILD_SPAWN_FAILED);
write (child_err_report_fd, &no_error, sizeof (no_error));
if (mode == P_NOWAIT)
write (child_err_report_fd, &handle, sizeof (handle));
else
write (child_err_report_fd, &zero, sizeof (zero));
return 0;
}