Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

WIN32 calls

by kapper (Chaplain)
on Jul 04, 2000 at 17:00 UTC ( #21001=perlquestion: print w/replies, xml ) Need Help??

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

Inspired by mrmick & the small Simple WIN32 Service Test script he posted, I set out to write a similar script which would fetch the currently running proccesses on my w2kbox...

...After browsing WIN32 API calls for 30 minuttes, I'm getting grumpy... If anybody has a link to such information, I would greatly apreciate it, and will offcourse post the script when I reach my goal... =)

Replies are listed 'Best First'.
Re: WIN32 calls
by gaggio (Friar) on Jul 05, 2000 at 03:01 UTC
    Here a function to list all the processes running on a machine. It uses Win32::Perflib.
    Maybe that I did not quite get the purpose of your program, though. Do you want to list only the services running or the tasks? This function returns a pid-indexed hash with all the tasks running on a Win32 machine, as listed in the task manager.
    You'll notice, BTW, 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. I just decided to publish this function in the Snippets Section also, since it can be useful to somebody else.
    If you want to have your perl script run as a service, you might want to use Win32::Daemon from Roth.net. I have used the module and found it very useful.
    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); $perflib->Close(); 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->{$ +p}->{Name}; } } } return %rtasks; }

      he he.. well what you posted here, _was_ excactly what i set out to do... =)

      Unfortunatly, I cannot get yours to work (.. my incompetence surely).. it dies at :

      $perflib->GetObjectList($process_obj, $proc_ref);

      If there is an obvious reason for this, I would be glad to know, otherwise I will just continue hacking away at it myself...

        Win32::PerfLib works only on NT. Maybe only 4.0?

        /brother t0mas
        Hmm.
        This is weird. Yes, what kind of Windows do you have? I have Win NT 4.0 SP6 with Perl version 5.005_03 built for MSWin32-x86-object (do a Perl -v to check).
        Also, what is the error message when it dies?
Re: WIN32 calls
by barndoor (Pilgrim) on Jul 04, 2000 at 17:29 UTC
    This is not that easy. You are lucky with W2K as you should be able to use 'Process32First' and 'Process32Next' on a snapshot of the process list generated using 'CreateToolhelp32Snapshot'. These are in Kernel32.lib and are part of the tools debugging library. I've not used them as they don't work on NT4.0. They will work on 2K/95/98 however. Take a look on MSDN for details. They will hopefully provide what you need.
RE: WIN32 calls
by t0mas (Priest) on Jul 10, 2000 at 17:20 UTC
    I guess I really dislike questions without an answer. Don't know if you've solved this on your own kapper. I've tested Win32::Perflib without any luck on Win2k so I dicided to play around with Win32::API. I fired up VIM and came up with this code just for you. It uses Win32::API and works well on my Win2k box.
    Have fun...

    /brother t0mas

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://21001]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2021-10-22 15:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My first memorable Perl project was:







    Results (85 votes). Check out past polls.

    Notices?