By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,574 Members | 3,021 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,574 IT Pros & Developers. It's quick & easy.

Kill process based on window name (win32)

P: n/a
Hi.

I am trying to close/kill all processes that show visible windows on
Windows XP. So far I've created a script that uses win32gui.EnumWindows
to iterate through all windows, check for which windows are visible,
then send a WM_CLOSE message to the window to request that it closes.
Of course, not all apps want to close nicely. At this point I need to
use something like TerminateProcess to kill the app, but how do I find
the process id (hopefully based on the window id).

Thanks for any help.

Aug 12 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a

drodrig wrote:
Hi.

I am trying to close/kill all processes that show visible windows on
Windows XP. So far I've created a script that uses win32gui.EnumWindows
to iterate through all windows, check for which windows are visible,
then send a WM_CLOSE message to the window to request that it closes.
Of course, not all apps want to close nicely. At this point I need to
use something like TerminateProcess to kill the app, but how do I find
the process id (hopefully based on the window id).

Thanks for any help.
win32process.GetWindowThreadProcessId should do the trick.

Roger
Aug 12 '06 #2

P: n/a
Thank you Roger. Your advice did the trick. For anyone interested, the
basic code to terminate a process (politely) would be something like
this (hwnd is retrieved using win32gui.EnumerateWindows):

# Get the window's process id's
t, p = win32process.GetWindowThreadProcessId(hwnd)
# Ask window nicely to close
win32gui.PostMessage(hwnd, win32con.WM_CLOSE, 0, 0)
# Allow some time for app to close
time.sleep(10)
# If app didn't close, force close
try:
handle = win32api.OpenProcess(win32con.PROCESS_TERMINATE, 0, p)
if handle:
win32api.TerminateProcess(handle,0)
win32api.CloseHandle(handle)
except:
pass:

Roger Upole wrote:
drodrig wrote:
Hi.

I am trying to close/kill all processes that show visible windows on
Windows XP. So far I've created a script that uses win32gui.EnumWindows
to iterate through all windows, check for which windows are visible,
then send a WM_CLOSE message to the window to request that it closes.
Of course, not all apps want to close nicely. At this point I need to
use something like TerminateProcess to kill the app, but how do I find
the process id (hopefully based on the window id).

Thanks for any help.

win32process.GetWindowThreadProcessId should do the trick.

Roger
Aug 12 '06 #3

P: n/a
I am running the program mentioned below as an NT service on a terminal
server. The service listens on a UDP port for any of a series of
commands. In this case, the command "closeApps <user>" will notify the
service to close all the open apps for user (<user>). So while the code
below works great for a standalone app, it fails as a service because
the window handles of each user are not retrievable (I should say I
don't know how to retrieve them). Is this even possible? I looked at
some of the Windows API calls but nothing stuck out.

Suggestions?

drodrig wrote:
Thank you Roger. Your advice did the trick. For anyone interested, the
basic code to terminate a process (politely) would be something like
this (hwnd is retrieved using win32gui.EnumerateWindows):

# Get the window's process id's
t, p = win32process.GetWindowThreadProcessId(hwnd)
# Ask window nicely to close
win32gui.PostMessage(hwnd, win32con.WM_CLOSE, 0, 0)
# Allow some time for app to close
time.sleep(10)
# If app didn't close, force close
try:
handle = win32api.OpenProcess(win32con.PROCESS_TERMINATE, 0, p)
if handle:
win32api.TerminateProcess(handle,0)
win32api.CloseHandle(handle)
except:
pass:

Roger Upole wrote:
drodrig wrote:
Hi.
>
I am trying to close/kill all processes that show visible windows on
Windows XP. So far I've created a script that uses win32gui.EnumWindows
to iterate through all windows, check for which windows are visible,
then send a WM_CLOSE message to the window to request that it closes.
Of course, not all apps want to close nicely. At this point I need to
use something like TerminateProcess to kill the app, but how do I find
the process id (hopefully based on the window id).
>
Thanks for any help.
>
win32process.GetWindowThreadProcessId should do the trick.

Roger
Aug 13 '06 #4

P: n/a
Trying to do this for different sessions is going to be much more
complicated. Each terminal service session has its own
window station, and there may be more than one desktop per
window station. You'll need several functions from the win32service
module for accessing window stations and desktops,
and the win32ts module (which was just added in Pywin32 build 209)
for terminal services functions.

As a start:
win32ts.WTSEnumerateSessions to list the sessions
win32ts.WTSQuerySessionInformation to get the username and window
station name for the session
win32service.OpenWindowStation
EnumDesktops to get a list of all desktops in the window station
win32service.OpenDesktop to access each desktop
EnumDesktopWindows to get handles to all windows on the desktop

You might be better off to use win32ts.WTSLogoffSession to kill the session
altogether.

Roger
"drodrig" <dr*****@magicbrain.comwrote in message news:11********************@75g2000cwc.googlegroup s.com...
>I am running the program mentioned below as an NT service on a terminal
server. The service listens on a UDP port for any of a series of
commands. In this case, the command "closeApps <user>" will notify the
service to close all the open apps for user (<user>). So while the code
below works great for a standalone app, it fails as a service because
the window handles of each user are not retrievable (I should say I
don't know how to retrieve them). Is this even possible? I looked at
some of the Windows API calls but nothing stuck out.

Suggestions?

drodrig wrote:
>Thank you Roger. Your advice did the trick. For anyone interested, the
basic code to terminate a process (politely) would be something like
this (hwnd is retrieved using win32gui.EnumerateWindows):

# Get the window's process id's
t, p = win32process.GetWindowThreadProcessId(hwnd)
# Ask window nicely to close
win32gui.PostMessage(hwnd, win32con.WM_CLOSE, 0, 0)
# Allow some time for app to close
time.sleep(10)
# If app didn't close, force close
try:
handle = win32api.OpenProcess(win32con.PROCESS_TERMINATE, 0, p)
if handle:
win32api.TerminateProcess(handle,0)
win32api.CloseHandle(handle)
except:
pass:

Roger Upole wrote:
drodrig wrote:
Hi.

I am trying to close/kill all processes that show visible windows on
Windows XP. So far I've created a script that uses win32gui.EnumWindows
to iterate through all windows, check for which windows are visible,
then send a WM_CLOSE message to the window to request that it closes.
Of course, not all apps want to close nicely. At this point I need to
use something like TerminateProcess to kill the app, but how do I find
the process id (hopefully based on the window id).

Thanks for any help.
win32process.GetWindowThreadProcessId should do the trick.

Roger

Aug 13 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.