how to terminate a process on win32?

Tim Peters tim.one at comcast.net
Wed May 26 22:58:12 EDT 2004


[Joe Wong]
> But calling TerminateProcess will stop the application right away. I need
> someway that the process being killed get notified and thus to carry out
> some procedure before shut down. On Linux, I can use kill(pid, signum) to
> achive this but on windows?

There is no way guaranteed to work on Windows.  This MS article explains the
*intended* way, but many apps (and especially console apps) don't play this
game:

    http://support.microsoft.com/default.aspx?scid=kb;EN-US;q178893

The short course is that native Win32 programs with a GUI "should" be
running a message pump, and their top-level window should respond to a
WM_CLOSE message by shutting down the app cleanly.  If they're not coded
that way, then no, you have in general no way to force them to shut down
cleanly.  Googling will turn up many approximations that more-or-less work
for different kinds of apps.  Bottom line is really that life is sheer hell
unless the process you're trying to shut down *advertises* a way to force
that from outside (like responding to WM_CLOSE, or the state of a global
semaphore, or a particular string of bytes sent to an agreed-upon socket,
etc).






More information about the Python-list mailing list