Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^2: Killing a Win32 process, a return value ? (bugs)

by tye (Sage)
on Mar 03, 2007 at 03:36 UTC ( [id://603022]=note: print w/replies, xml ) Need Help??


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

    Indeed.

    Where do you want *them* to go today?
Re^3: Killing a Win32 process, a return value ? (bugs)
by abachus (Monk) on Mar 21, 2007 at 18:15 UTC
    Thanks for the output everyone gave in this thread, all has been considered ;) I have chosen to go with the code suggested by Tye with regards to testing the condition of $^E.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://603022]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-20 00:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found