Get Windows Task-Names?!

Discussion in 'Perl Misc' started by Kasper Dziurdz, Oct 31, 2003.

  1. Kasper Dziurdz, Oct 31, 2003
    #1
    1. Advertising

  2. On Fri, 31 Oct 2003 13:22:07 +0100
    Kasper Dziurdz <> wrote:
    > 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
     
    James Willmore, Oct 31, 2003
    #2
    1. Advertising

  3. 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/
     
    Kasper Dziurdz, Nov 1, 2003
    #3
  4. Kasper Dziurdz, Nov 1, 2003
    #4
  5. Purl Gurl wrote:
    > 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/
     
    Kasper Dziurdz, Nov 2, 2003
    #5
  6. Purl Gurl wrote:
    > 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/
     
    Kasper Dziurdz, Nov 2, 2003
    #6
  7. 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/
     
    Kasper Dziurdz, Nov 2, 2003
    #7
  8. Purl Gurl wrote:
    > 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/
     
    Kasper Dziurdz, Nov 3, 2003
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Victor
    Replies:
    0
    Views:
    8,873
    Victor
    Sep 1, 2004
  2. krabhi
    Replies:
    1
    Views:
    9,376
    Marco Meschieri
    Aug 9, 2006
  3. teggy
    Replies:
    0
    Views:
    829
    teggy
    May 29, 2007
  4. Mike
    Replies:
    1
    Views:
    1,960
    GArlington
    May 12, 2008
  5. Stéphane Wirtel
    Replies:
    3
    Views:
    386
    Stephane Wirtel
    Jun 15, 2007
Loading...

Share This Page