Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to terminate a process on win32?

175 views
Skip to first unread message

Joe Wong

unread,
May 21, 2004, 9:18:33 PM5/21/04
to pytho...@python.org
Hi,
 
 I am developing two app on Windows, one is a GUI using wxPython and the other is like a daemon process. The GUI app will have a start/stop button to control the excution of the daemon. While I know how to use os.popen2() to start my daemon, I can't figure out a way to stop it 'nicely'... Is there anything that I can do?
 
Regards,
 
-- Wong
 

David Fraser

unread,
May 24, 2004, 10:38:02 AM5/24/04
to

You can use the win32process.TerminateProcess function to kill a
process... see the win32 docs

David

Hung Jung Lu

unread,
May 24, 2004, 6:11:49 PM5/24/04
to

It depends on your OS. I remember once upon a time, in order to kill
processes sucessfully for all Windows platforms (from Win98 to NT to
2000/XP), I had to resort to using tlist.exe and kill.exe, plus
w9xpopen.exe for pipes in Windows 98. It was kind of annoying... I
seem to remember short name and long names were problem, too. At the
end, the killing logic became a small program in itself!

regards,

Hung Jung

David Stockwell

unread,
May 25, 2004, 7:03:37 AM5/25/04
to hungj...@yahoo.com, pytho...@python.org
Just my 2cents....

There is an undocumented feature..
UINT uExitCode <--

I don't remember all the details, but I've used it in the past.

if you set the exit code to a certain number (i don't recall what but it is
possibly -1, 0, or 1... maybe 2 or -2) then the OS will force a 'shut'
beyond the normal call to terminate process.

When the OS is 'shutting down' it calls TerminateProcess on each process
passing that value. This trick prevents processes from restarting (if you
have a process like explorer that you are trying to kill).

Since I don't recall the details, it would take a bit of experimentation.
But hopefully not too much.

David
=====================
http://cellphone.duneram.com
"Surf a wave to the future"
I'm a windows SDK CBT guru with a patent....

>--
>http://mail.python.org/mailman/listinfo/python-list

_________________________________________________________________
Express yourself with the new version of MSN Messenger! Download today -
it's FREE! http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/


Myles

unread,
May 25, 2004, 9:49:09 PM5/25/04
to
"Joe Wong" <joe...@mango.cc> wrote in message news:<mailman.164.1085188...@python.org>...

> os.popen2() to start my daemon, I can't figure out a way to stop it
> 'nicely'... Is there anything that I can do?

Mark Hammond's Python for Windows extensions includes a demonstration
of killing processes on Windows:

On my PC:
C:\Python23\Lib\site-packages\win32\scripts\killProcName.py

Python for Windows extensions:
http://starship.python.net/crew/mhammond/
https://sourceforge.net/projects/pywin32/

Regards, Myles.

Joe Wong

unread,
May 26, 2004, 10:32:54 PM5/26/04
to Myles, pytho...@python.org
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?


----- Original Message -----
From: "Myles" <my...@geocities.com>
Newsgroups: comp.lang.python
To: <pytho...@python.org>
Sent: Wednesday, May 26, 2004 9:49 AM
Subject: Re: how to terminate a process on win32?

> --
> http://mail.python.org/mailman/listinfo/python-list
>


Tim Peters

unread,
May 26, 2004, 10:58:12 PM5/26/04
to pytho...@python.org
[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).

0 new messages