slloyd has asked for the wisdom of the Perl Monks concerning the following question:

In Windows the Task Manager shows Total Handles on the Performance tab. I am trying to capture that number using perl. Does anyone know how to do this? Is there an Windows API for this? Any other thoughts?
  • Comment on Getting Total Object Handles in Windows

Replies are listed 'Best First'.
Re: Getting Total Object Handles in Windows
by ppetty (Initiate) on Dec 17, 2004 at 18:26 UTC
    try the following perl script:

    use Win32::PerfLib; my $server = ""; Win32::PerfLib::GetCounterNames($server, \%counter); %r_counter = map { $counter{$_} => $_ } keys %counter; # retrieve the id for process object $process_obj = $r_counter{Process}; # retrieve the id for the process ID counter $process_id = $r_counter{'Handle Count'}; # create connection to $server $perflib = new Win32::PerfLib($server); $proc_ref = {}; # get the performance data for the process object $perflib->GetObjectList($process_obj, $proc_ref); $perflib->Close(); $instance_ref = $proc_ref->{Objects}->{$process_obj}->{Instances}; foreach $p (sort keys %{$instance_ref}) { $counter_ref = $instance_ref->{$p}->{Counters}; foreach $i (keys %{$counter_ref}) { if($counter_ref->{$i}->{CounterNameTitleIndex} == $process_id) { printf( "% 6d %s\n", $counter_ref->{$i}->{Counter}, $instance_ref->{$p}->{Name} ); } } }


    If you look at the _Total number, it's the Total Handles for all processes. This number is the same as the Totals / Handles on the Performance tab in the Windows Task Manager. Good Luck!!!
Re: Getting Total Object Handles in Windows
by ikegami (Patriarch) on Dec 17, 2004 at 17:04 UTC
    It's a counter, as you can see in Win2k and WinXP's Start | Settings | Control Panel | Administrative Tools | Performance | (right-click on System's monitor, or create a new counter log) | Add Counters. you can find it under Performance Object = Process, Counter = Handle Count, Instance = _Total. As such, I believe you can use Win32::PerfLib to find its value. (I've never used the module.)
      I have looked in Win32::PerfLib and cannot seem to get this particular item back...

        Works for me...

        use strict; use warnings; use Win32::PerfLib (); my $server = ""; { my %counters; my $process_group_id; my $handle_count_counter_id; my %perf_data; my $perflib; my $process_instances; my $total_instance; my $total_counters; my $total_handle_count_counter; my $total_handle_count; Win32::PerfLib::GetCounterNames($server, \%counters); foreach (keys(%counters)) { if ($counters{$_} eq 'Process') { $process_group_id = $_; } if ($counters{$_} eq 'Handle Count') { $handle_count_counter_id = $_; } } $perflib = new Win32::PerfLib($server); $perflib->GetObjectList($process_group_id, \%perf_data); $perflib->Close(); $process_instances = $perf_data{'Objects'}->{$process_group_id}->{' +Instances'}; foreach (keys(%$process_instances)) { if ($process_instances->{$_}->{'Name'} eq '_Total') { $total_instance = $process_instances->{$_}; last; } } $total_counters = $total_instance->{'Counters'}; foreach (keys(%$total_counters)) { if ($total_counters->{$_}->{'CounterNameTitleIndex'} == $handle_ +count_counter_id) { $total_handle_count_counter = $total_counters->{$_}; last; } } $total_handle_count = $total_handle_count_counter->{'Counter'}; print($total_handle_count, $/); }

        OT rant:

Re: Getting Total Object Handles in Windows
by McMahon (Chaplain) on Dec 17, 2004 at 17:34 UTC
    If you don't care about the rest of the Taskmanager information, the Win32::GUITest example spy-- might get the right information for you.

    Super Search for "spy--" and you'll find a couple of examples of monks who've hacked the basic script for their own purposes.