Get Windows Task-Names?!

J

James Willmore

is there a way or module to get the names of Windows tasks currently

running (for example "Google - Mozilla" but not "mozilla.exe")?!

I'm not 100% sure about this, but you could look at one of the many
Win32 modules. I'm thinking one of those modules will accomplish the
task.

A quick glance at:
http://search.cpan.org/search?query=win32+process&mode=module

shows Win32::process::Info and Win32API::processStatus _may_ fit the
bill.

One suggestion is to find out how to do what you want to do at the
command line and then fashion a script around that command.

I'm not a very big Windows user, so these are just suggestions.
Others may be more helpful.

--
Jim

Copyright notice: all code written by the author in this post is
released under the GPL. http://www.gnu.org/licenses/gpl.txt
for more information.

a fortune quote ...
When we are planning for posterity, we ought to remember that
virtue is not hereditary. -- Thomas Paine
 
K

Kasper Dziurdz

Thanks for the information! The only problem is, I never programmed in
C. I thought about using Win32::API and searched the MSDN but I did not
find what I was looking for (perhaps I have to search again).
Win32::ToolHelp seems to be good enough but returns only the processes
name like "mozilla.exe" and not "Google - Mozilla". Same with other
modules I found on CPAN.

Kasper

--
HTML::KTemplate | Perl module to process HTML templates.
http://search.cpan.org/dist/HTML-KTemplate/

Kopierschutz-Nein-Danke.de | Gegen kopiergeschützte Un-CDs!
http://www.kopierschutz-nein-danke.de/
 
K

Kasper Dziurdz

Purl said:
Kasper, your use of tasklist has rattled my memory
of tlist which will be added to my Perl site as an
effective method to list all running processes
using Win32 DOS system access.

Here is the complete code I wrote:

sub IMAGENAME { 0 }
sub PID { 1 }
sub SESSIONNAME { 2 }
sub SESSION { 3 }
sub MEMUSAGE { 4 }
sub STATUS { 5 }
sub USERNAME { 6 }
sub CPUTIME { 7 }
sub WINDOWTITLE { 8 }

sub get_tasks {

my @tasks = split "\n", `tasklist.exe /v`;
my @format = split " ", $tasks[2];
@tasks = @tasks[3 .. $#tasks];

for (my $i = 0; $i <= $#tasks; ++$i) {

my $task = [];
my $line = $tasks[$i];

foreach my $f (@format) {
my $str = substr $line, 0, (length $f) + 1, '';
$str =~ s/\s*$//;
push @$task, $str;
}

$tasks[$i] = $task;

}

return wantarray ? @tasks : \@tasks;

}

foreach ( get_tasks() ) {
print $_->[WINDOWTITLE], "\n";
}

However, tasklist.exe seems to limit the window title to 72 characters.
Everything else works fine :)

Kasper

--
HTML::KTemplate | Perl module to process HTML templates.
http://search.cpan.org/dist/HTML-KTemplate/

Kopierschutz-Nein-Danke.de | Gegen kopiergeschützte Un-CDs!
http://www.kopierschutz-nein-danke.de/
 
K

Kasper Dziurdz

Purl said:
I would like to read a sample of your raw output from tasklist.
Your code suggests WinXP tasklist returns are quite different
than traditional tlist returns:

Tasklist.exe returns a list in the following format:

Abbildname PID Sitzungsname Sitz.-Nr.
========================= ===== ================ ==========
System Idle Process 0 Console 0
System 4 Console 0
smss.exe 348 Console 0
mozilla.exe 1992 Console 0

As you mentioned, tlist.exe does not always have the correct window
title and does not return the cpu time etc.
Also of interest, a question which appears here infrequently
is how to kill a process under Win32. This kill.exe listed
at that ftp site is specifically designed for killing off
Windows tasks via DOS, which is highly adaptable to Perl.
Usual caution would apply of a process no longer running
when a kill pid command is executed, or a new process
is running under a previously used pid number.

Clearly many of these programs not available on OEM disks
will make for superior replacements for Perl modules which
often do not perform as claimed.

Perhaps something for a new module like Win32::Tools as an interface to
these programs?!

Kasper

--
HTML::KTemplate | Perl module to process HTML templates.
http://search.cpan.org/dist/HTML-KTemplate/

Kopierschutz-Nein-Danke.de | Gegen kopiergeschützte Un-CDs!
http://www.kopierschutz-nein-danke.de/
 
K

Kasper Dziurdz

TASKLIST /V /FO CSV /NH

"System Idle Process","0","Console","0","20 K","Wird
ausgeführt","NT-AUTORITÄT\SYSTEM","1:36:28","Nicht verfügbar"
"System","4","Console","0","216 K","Wird
ausgeführt","NT-AUTORITÄT\SYSTEM","0:09:56","Nicht verfügbar"
"smss.exe","348","Console","0","344 K","Wird
ausgeführt","NT-AUTORITÄT\SYSTEM","0:00:00","Nicht verfügbar"
"mozilla.exe","236","Console","0","38.496 K","Wird
ausgeführt","KASPER-XXXXXXXX\Kasper Dziurdz","0:00:07","Google - Mozilla"

Easier to process and no limit to 72 characters!

Kasper

--
HTML::KTemplate | Perl module to process HTML templates.
http://search.cpan.org/dist/HTML-KTemplate/

Kopierschutz-Nein-Danke.de | Gegen kopiergeschützte Un-CDs!
http://www.kopierschutz-nein-danke.de/
 
K

Kasper Dziurdz

Purl said:
Does XP have an equal for "kill.exe" as found in the
resource kit for older Win systems such as 9.x and
Win2K / NT5?

Yes, taskkill.exe:

http://www.microsoft.com/technet/prodtechnol/winxppro/proddocs/taskkill.asp

http://www.microsoft.com/technet/prodtechnol/winxppro/proddocs/tasklist.asp

Small rewrite again:


sub tasklist {

my @tasks = split "\n", `tasklist.exe /v /fo CSV /nh`;
shift @tasks if $tasks[0] eq ''; # remove empty line

foreach (@tasks) {
my $task = [];

foreach my $str (split ',') {
substr $str, 0, 1, '';
substr $str, -1, 1, '';
push @$task, $str;
}

$_ = $task;
}

return wantarray ? @tasks : \@tasks;

}


Thanks for the help!

Kasper

--
HTML::KTemplate | Perl module to process HTML templates.
http://search.cpan.org/dist/HTML-KTemplate/

Kopierschutz-Nein-Danke.de | Gegen kopiergeschützte Un-CDs!
http://www.kopierschutz-nein-danke.de/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top