473,594 Members | 2,768 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 17443

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
4888
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
3322
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
3139
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 debuggers. I wrote a test-program, which creates a divide by zero error after some seconds. I can...
2
2516
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 application does not quit after automation from Visual Studio .NET client" without much luck.
1
3825
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 Arguments = "/c"
1
1713
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 problem is that I need to be able to kill that process with another button. I have a global flag...
0
2015
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 a lot. -Vinit Fichadia
1
4408
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 "WinMgmts:{impersonationLevel=impersonate,(security)}!\\10.10.230.40" Error2: when my remote machine is on other domain.
18
10219
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 figure out how to deal with the locked up thread. If I issue a Thread.Abort() the exception never...
0
7937
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7874
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8244
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8368
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
6647
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5403
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3854
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3893
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2383
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.