Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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        


In reply to Re^2: Killing a Win32 process, a return value ? (bugs) by tye
in thread Killing a Win32 process, a return value ? by abachus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (1)
As of 2024-04-24 14:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found