Re: killing a process by window title
by Dirk Bremer (NISC) other posts by this author
Jan 29 2003 2:56PM messages near this date
view in the new Beta List Site
Re: killing a process by window title; was: socket application
|
keyboard buffers
I use this program to run at startup, check for an instance of the Windows
Task Manager, and if it is not already running, start the Task Manager and
then minimize it. The object of this program is to always have a minimized
copy of the Task Manager running.
#! C:/perl/bin/perl -w
# TaskManager.pl 02/27/2002.
# This program will start an instance of the NT Task Manager and then
# minimize it.
# Declare pragmas.
use diagnostics;
use strict;
use warnings;
# Declare modules.
use Win32::GUI;
use Win32::Process;
# Declare all subroutines.
sub CheckForInstance();
sub ErrorReport();
# Declare global variables.
my $Desktop = GUI::GetDesktopWindow();
my $Window = GUI::GetWindow($Desktop, GW_CHILD);
my $Title;
{
# Check for an existing instance of the Task Manager.
CheckForInstance();
# Declare local variables.
my $Process;
# Start an instance of the NT Task Manager.
Win32::Process::Create($Process,
'C:/Winnt/system32/taskmgr.exe',
'',
0,
CREATE_NO_WINDOW,
'C:/Winnt') || die ErrorReport();
while ()
{
$Desktop = GUI::GetDesktopWindow();
$Window = GUI::GetWindow($Desktop, GW_CHILD);
# Locate the Task Manager window and minimize it.
while($Window)
{
$Title = GUI::Text($Window);
goto NEXTWIN if (length($Title) < 17);
if ($Title =~ /task manager/i) {GUI::Minimize($Window); exit(1)}
NEXTWIN: $Window = GUI::GetWindow($Window, GW_HWNDNEXT);
}
sleep(1);
}
exit(1);
}
sub CheckForInstance()
{
while($Window)
{
$Title = GUI::Text($Window);
goto NEXTWIN if (length($Title) < 17);
if ($Title =~ /task manager/i) {print("An instance of Task Manager
already exists\n"); exit(1)}
NEXTWIN: $Window = GUI::GetWindow($Window, GW_HWNDNEXT);
}
return(1);
}
sub ErrorReport()
{
print(Win32::FormatMessage(Win32::GetLastError()));
return(1);
}
__END__
This program illustrates a way to locate a window by its title, which is
well and good. The question would be, once the window has been located via
Win32::GUI, can Win32::GUI provide the window's PID so that it could be
killed via Win32::Process?
Dirk Bremer - Systems Programmer II - ESS/AMS - NISC St. Peters
USA Central Time Zone
636-922-9158 ext. 8652 fax 636-447-4471
dirk.bremer@[...].cc
www.nisc.cc
_______________________________________________
Perl-Win32-Users mailing list
Perl-Win32-Users@[...].com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Thread:
Thomas R Wyant_III
Dirk Bremer (NISC)
|