473,544 Members | 1,870 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.EnumWi ndows
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 TerminateProces s 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 17424

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.EnumWi ndows
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 TerminateProces s to kill the app, but how do I find
the process id (hopefully based on the window id).

Thanks for any help.
win32process.Ge tWindowThreadPr ocessId 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.Enumer ateWindows):

# Get the window's process id's
t, p = win32process.Ge tWindowThreadPr ocessId(hwnd)
# Ask window nicely to close
win32gui.PostMe ssage(hwnd, win32con.WM_CLO SE, 0, 0)
# Allow some time for app to close
time.sleep(10)
# If app didn't close, force close
try:
handle = win32api.OpenPr ocess(win32con. PROCESS_TERMINA TE, 0, p)
if handle:
win32api.Termin ateProcess(hand le,0)
win32api.CloseH andle(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.EnumWi ndows
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 TerminateProces s to kill the app, but how do I find
the process id (hopefully based on the window id).

Thanks for any help.

win32process.Ge tWindowThreadPr ocessId 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.Enumer ateWindows):

# Get the window's process id's
t, p = win32process.Ge tWindowThreadPr ocessId(hwnd)
# Ask window nicely to close
win32gui.PostMe ssage(hwnd, win32con.WM_CLO SE, 0, 0)
# Allow some time for app to close
time.sleep(10)
# If app didn't close, force close
try:
handle = win32api.OpenPr ocess(win32con. PROCESS_TERMINA TE, 0, p)
if handle:
win32api.Termin ateProcess(hand le,0)
win32api.CloseH andle(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.EnumWi ndows
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 TerminateProces s to kill the app, but how do I find
the process id (hopefully based on the window id).
>
Thanks for any help.
>
win32process.Ge tWindowThreadPr ocessId 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.WTSEnum erateSessions to list the sessions
win32ts.WTSQuer ySessionInforma tion to get the username and window
station name for the session
win32service.Op enWindowStation
EnumDesktops to get a list of all desktops in the window station
win32service.Op enDesktop to access each desktop
EnumDesktopWind ows to get handles to all windows on the desktop

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

Roger
"drodrig" <dr*****@magicb rain.comwrote in message news:11******** ************@75 g2000cwc.google groups.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.Enumer ateWindows):

# Get the window's process id's
t, p = win32process.Ge tWindowThreadPr ocessId(hwnd)
# Ask window nicely to close
win32gui.PostM essage(hwnd, win32con.WM_CLO SE, 0, 0)
# Allow some time for app to close
time.sleep(1 0)
# If app didn't close, force close
try:
handle = win32api.OpenPr ocess(win32con. PROCESS_TERMINA TE, 0, p)
if handle:
win32api.Termin ateProcess(hand le,0)
win32api.Close Handle(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.EnumWi ndows
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 TerminateProces s to kill the app, but how do I find
the process id (hopefully based on the window id).

Thanks for any help.
win32process.Ge tWindowThreadPr ocessId 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
4885
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 don't manage. I have tried both: kill 'QUIT', $pid;
0
3310
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 lines: Dim p As New Process For Each p In Process.GetProcesses If LCase(p.ProcessName) = "acrord32" Then 'p.Kill()
1
3137
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 like no longer generating IO or such properties and I use WMI to do that. I am also looking for attached subprocesses, which are one of the system...
2
2514
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 have been able to do all of the above steps except #4. Each time Excel.exe persists in memory. I have already referred to Q317109 "Office...
1
3797
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 process after the below code is executed? Dim process As New Process() Dim FileName As String = "externalprog.exe" Dim Arguments As String ...
1
1711
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 process that is lauched by pushing a button. The code for this process is inside the Button_Click event handler for that particular button. The...
0
2012
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, now i would like to kill the process using filename(here date). how can i get the processid by the name of the file that i want to kill. Thanks...
1
4403
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 same domain as of local machine Win32::OLE(0.1701) error 0x80070005: "Access is denied" after character 0 in...
18
10203
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 with weirdness going on with the server I am talking to. Anyhow, this locked up state happens once in a while (maybe once per day) and I can't...
0
7370
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7617
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7779
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7387
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7714
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5305
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4924
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3415
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
992
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.