| Store | Cart

Re: killing a process by window title

From: Dirk Bremer \(NISC\) <dirk...@nisc.cc>
Wed, 29 Jan 2003 08:56:20 -0600
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...@nisc.cc
www.nisc.cc

Recent Messages in this Thread
Thomas R Wyant_III Jan 29, 2003 02:52 am
Dirk Bremer \(NISC\) Jan 29, 2003 02:56 pm
shu...@wencor.com Jan 29, 2003 08:17 pm
Messages in this thread