Portable process killer with GLib

Since Windows API (and MinGW) doesn’t have fork() / exec() calls, I love GLib’s g_spawn_sync() and its friends to spawn child process in portable (and Unix like) way.
However, GLib provides nothing like kill() to terminate processes. So I have to find a way to kill a process by myself. As a portable process identifier, g_spawn_sync() gives GPid. This is defined in GLIB_PREFIX/lib/glib-2.0/include/glibconfig.h as:
– Unix: typedef int GPid;
– Win32: typedef void * GPid;
The one for Unix is equal to pid, and the one for Win32 is equal to HANDLE (defined in winnt.h, as “typedef void *HANDLE;”).
So, a process launched by g_spawn_sync() may be killed by:
kill(GPid p, 15);
TerminateProcess(GPid p, UINT return_code);
MSDN document says “The state of global data maintained by dynamic-link libraries (DLLs) may be compromised if TerminateProcess is used rather than ExitProcess.”
Source code of a Gimp plugin gives a good example, but I haven’t tested it yet. Hope this works…

コメントを残す