473,396 Members | 1,929 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Kill process based on window name (win32)

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
4 17379

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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Carlo Filippini | last post by:
Hi on a XP machine I start a ftp process with: $pid = open (CMD, "ftp -in -w:1024 -s:$file_cmd $server 2>&1 |") or die "Can't execute: $!"; In some occasions I want to kill the process, but I...
0
by: Markus Poehler | last post by:
Hi my program should run on terminal server. I open Acrobat process and I have to kill them at some points in my application. This fails cause of insufficient rights on terminal server. the...
1
by: Manfred Braun | last post by:
Hi All, I am writing a tool, which should monitor some exe-processes, which are not very solid. Th main function is to re-start them, if they hung, but this is complicated. I can detect things...
2
by: ChrisFrohlich | last post by:
I have been trying to use the Office PIA's to write an ASP.NEt page to: 1. Open a template workbook 2. Populate some data 3. Save the file back to the server 4. Quit Excel and free up Memory I...
1
by: Joey | last post by:
I am using the below code to run a dos based program. While it is running you can hit ctrl-c to cancel the process. Does anyone know how I can send the same keystroke to the already runing...
1
by: AE_Cory | last post by:
I'm a n00b to Visual C++ and OOP, but not to programming in general. Here's the problem: Not knowing what I'm doing, I've made my VC++ application as a CLR Window Forms project. Now, I have a...
0
by: vinitfichadia | last post by:
Hello friends, In my vb.net application i would like to kill the process using the application name i.e, i m dynamically opening grid data into excel inthe same window and excel filename is date,...
1
shrek123
by: shrek123 | last post by:
I want to kill some process on remote machine which is on some other domain. I am using Win32::OLE GetObject to do that. But I am getting following error Error1: When my remote machine is on...
18
by: =?Utf-8?B?VGhlU2lsdmVySGFtbWVy?= | last post by:
Because C# has no native SSH class, I am using SharpSSH. Sometimes, for reasons I do not know, a Connect call will totally lock up the thread and never return. I am sure it has something to do...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.