Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Getting a Local/Remote Win32 Task List

by gaggio (Friar)
on Jul 05, 2000 at 03:10 UTC ( #21069=snippet: print w/replies, xml ) Need Help??
Description: Here a function to list all the processes running on a machine. It uses Win32::Perflib.
The function returns a pid-indexed hash with all the tasks running on a Win32 machine, as listed in the task manager.
You'll notice, that the function takes the machine you want to monitor as an argument: it is possible to get the task list of a remote machine.
It can be used as the first part of a program to fully remote control a Win32 machine (you will probably need to use Win32::Process to kill programs, and implement some sort of client server protocol also, a very tiny job for a Perl Monk :-)
sub get_remote_process_list {
my $server = $_[0];
my %rtasks;
my %counter;
Win32::PerfLib::GetCounterNames($server, \%counter);
my %r_counter = map { $counter{$_} => $_ } keys %counter;
# retrieve the id for process object
my $process_obj = $r_counter{Process};
# retrieve the id for the process ID counter
my $process_id = $r_counter{'ID Process'};
# create connection to $server
my $perflib = new Win32::PerfLib($server);
my $proc_ref = {};
# get the performance data for the process object
$perflib->GetObjectList($process_obj, $proc_ref);
my $instance_ref = $proc_ref->{Objects}->{$process_obj}->{Instances};
foreach my $p (sort keys %{$instance_ref})
    my $counter_ref = $instance_ref->{$p}->{Counters};
    foreach my $i (keys %{$counter_ref})
        if($counter_ref->{$i}->{CounterNameTitleIndex} == $process_id)
            $rtasks{$counter_ref->{$i}->{Counter}} = $instance_ref->{$
  return %rtasks;
Replies are listed 'Best First'.
RE: Getting a Local/Remote Win32 Task List
by Adam (Vicar) on Jul 06, 2000 at 00:38 UTC
    Having not used Win32::PerfLib before, I am curious about this snippet of code, but I don't quite understand what I need to pass in for $server. Are you looking for the IP? Or a machine name? or something else? Thanks.


      You can pass either (machine name or IP). Try it, it is working here (I litteraly did a "cut-and-paste" from within my NT remote controlling project - yes, I actually wrote one).
        gaggio, i tried using your little tidbit and it is awesome. you rock! Being a beginner in perl, i was wondering if you know how to format the info displayed from the remote tlist? Thanks in advance. Ray
      I'm getting: Can't call method "GetObjectList" on an undefined value at line 68. when using your sub and I have no idea why....any ideas?
        i'm getting the same error. find anything out?
Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: snippet [id://21069]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2022-06-26 18:05 GMT
Find Nodes?
    Voting Booth?
    My most frequent journeys are powered by:

    Results (86 votes). Check out past polls.