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

Hello. Using a perl script to search for process names and PIDs on Windows 2000. My problem is that unless a Java developer specifically creates a unique process name like "MyProcess" it simply shows up in the Task Manager Processes tab as "java.exe".

However, in the Applications tab you can see distinctions such as the name of the batch file like "MyProcess.bat" which would be enough for me to know which process to terminate.

Using the code below I've found all of the processes that show up in the Processes tab, but I cannot seem to find out if WMI can also obtain the information shown in the TM Applications tab. Any ideas? I'm slogging through the MSDN info on WMI, but haven't spotted it yet.

Thanks in advance.
$Machine = ''; $Machine =~ s{^[\\/]{2,}}{}; my $CLASS = 'WinMgmts:{impersonationLevel=impersonate}!//' . $Machine; my $WMI = Win32::OLE->GetObject($CLASS) or die Win32::OLE->LastError() +; foreach my $proc (in $WMI->InstancesOf('Win32_Process')){ print "$proc->{'Name'}" . " | " . "$proc->{'ProcessId'}\n"; $where = index($searchString, $proc->{'Name'}); if ($where > -1) { UpdateLogFile("$searchString running"); last; } }

Replies are listed 'Best First'.
Re: Getting Windows App Info with Perl
by BrowserUk (Patriarch) on Oct 02, 2003 at 22:01 UTC

    You should take a look at Win32::Process::Info. It uses the WMI interface (under 2K/XP) to retrieve just about everything retreivable. Whether the the particular piece of information you are after is amongst them I cannot confirm as A) I use NT which does have the WMI interface B) CPAN is currently down (or at least inaccessible from my machine).

    The interface is simplicity itself.

    my $pi = Win32::Process::Info->new( undef, 'WMI' ); my @procs = $pi->GetProcInfo();

    @procs now contains a hash for every process in the system contina a key for (just about) every conceivable piece of information available from the system. If the module has a flaw it is that you cannot ask for the information for just a given process, but the overhead (on my NT box at least) seems minimal.

    I think that you will find this easier than querying the information from the WMI yourself using OLE, if for no other reason than the information you need for that seems to be hidden away such that it is only acessible if you know exactly what you are looking for. With the module, the author has done all that searching for you :)


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
    If I understand your problem, I can solve it! Of course, the same can be said for you.

Re: Getting Windows App Info with Perl
by PodMaster (Abbot) on Oct 02, 2003 at 21:10 UTC
Re: Getting Windows App Info with Perl
by rdwaters (Initiate) on Oct 14, 2003 at 20:37 UTC
    I wanted to follow up on this one. I am still using Win32::Process to search for running instances of java.exe, but I now pass the found instance to a subroutine that uses the Win2000 utility tlist.exe to examine the information in detail. Let's say I find java.exe and PID is 1268, then my program runs this command:

    tlist 1268

    This returns all sorts of useful (and not!) information that lets me search for a meaningful reference in the CmdLine value. The java class file is listed there in my case. Below is the rough subroutine (not yet optimized to drop out of the loop when the answer is found) where tlist examines the PID passed. Thanks to all for your help!
    sub CheckProcess { foreach $_ (`tlist.exe $_[0]`) { my $finder = index($_, "CIBFeeder"); if ($finder > -1) { print "$_\n"; return 1; } } return 0; }