in reply to Re: Killing a Win32 process, a return value ?
in thread Killing a Win32 process, a return value ?
The documentation is inaccurate and the code is buggy. From http://search.cpan.org/src/JDB/libwin32-0.26/Process/Process.xs:
BOOL KillProcess(pid, exitcode) DWORD pid unsigned int exitcode CODE: { HANDLE ph = OpenProcess(PROCESS_ALL_ACCESS, 0, pid); if (ph) { RETVAL = TerminateProcess(ph, exitcode); if (RETVAL) CloseHandle(ph); } } OUTPUT: RETVAL
So, RETVAL isn't initialized so if OpenProcess() fails, RETVAL will be random garbage from the stack (some "large number", as observed, or sometimes 0 or 1, unfortunately, I guess -- unless the "arbitrary" observation is inaccurate and it is actually a standard Win32 marker value like 0xCCCCCCCC). If OpenProcess() succeeds, then RETVAL will say whether or not TerminateProcess() was successful or not.
And exitcode is not an output parameter so it isn't set to anything. It specifies what exit code the terminated process will use (as documented in the Win32 SDK).
Luckily, both OpenProcess() and TerminateProcess() call SetLastError() (Win32 SDK again) so you can check $^E to see if they failed (if you do it fast enough). For example:
$^E= 0; Win32::Process::KillProcess( $pid, 1 ); my $err= $^E; die "Failed to kill process, $pid: $err\n" if $err;
But someone should really fix the docs and code for this module, especially since it leaks a process handle if TerminateProcess() fails.
- tye
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Killing a Win32 process, a return value ? (bugs)
by thezip (Vicar) on Mar 03, 2007 at 03:41 UTC | |
|
Re^3: Killing a Win32 process, a return value ? (bugs)
by abachus (Monk) on Mar 21, 2007 at 18:15 UTC |