That is a good idea.  Will have to try that.

I usually use WMI.  A long as this is run by an admin, there are no
issues.  This is from one of Dave Roth's books with a couple of
modifications.  Tells you what you need to know.  Just pass the name of
the process (Exactly how it shows up in Taskman).

use Win32::OLE qw( in );
   use Win32::OLE::Variant;

   my $Machine = Win32::NodeName();  #hostname;
   
   my $CLASS =
"WinMgmts:{impersonationLevel=impersonate,AuthenticationLevel=connect}!/
/$Machine/root/cimv2";

   #/*** Get the WMI (Microsoft's implementation of WBEM) interface
   $WMI = Win32::OLE->GetObject( $CLASS ) || die "Unable to connect to
\\$Machine:" . Win32::OLE->LastError();

   #/*** Get the collection of Win32_Process objects
   $ProcList = $WMI->InstancesOf( "Win32_Process" );

   $Counter=0;
   
   foreach $Pid ( @_ )
   {
      my $ProcList = &GetProc( $Pid, $ProcList );
      
      if( scalar @$ProcList )
      {
         foreach my $Proc ( @$ProcList )
         {
        $strUser = Variant( VT_BYREF | VT_BSTR, "" );
    
        $strDomain = Variant( VT_BYREF | VT_BSTR, "" );
    
        $strSID = Variant( VT_BYREF | VT_BSTR, "" );
    
            if ( $Proc->GetOwner( $strUser, $strDomain ) == 0 )  
            {
               if ( $Proc->GetOwnerSid( $strSID ) == 0 ) 
               {  
                  if ( $EnvUser eq $strUser )
                  {                
                        #/*** If the program ends up here, then the App
is running ***/#                
                $ProcName = $Proc->{Name};
                
                $ProcPID = $Proc->{ProcessID}; 
                 
                $Counter++;
                         
                  }
               }
            }       
         }        
      }
   }
   
   if ( $Counter >= 1 )
   {
      
      $retval = "1";     #/*** This means that what ever the search was,
it IS here ***/#

      
      return($retval,$Counter,$ProcName,$ProcPID);
      
   }else{
       
      $retval = "0";    #/*** This means that what ever the search was,
it isn't here ***/#
      
      return($retval);
   }
}   

sub GetProc
{
  my( $Pid, $ProcList ) = @_;
  
  if( $Pid =~ /^\d+$/ )
  {
      $List = &GetProcByPid( $Pid, $ProcList );
    
  }else{
      
      $List = &GetProcByName( $Pid, $ProcList );
  }
  return( $List );
}

sub GetProcByPid
{
  my( $Pid, $ProcList ) = @_;
  
  # Cycle through each Win32_Process object 
  # until you find the right one
  foreach my $Proc ( in( $ProcList ) )
  {
    return( [ $Proc ] ) if( $Pid == $Proc->{ProcessID} );
  }
  return( [] );
}

sub GetProcByName
{
  my( $Name, $ProcList ) = @_;
  my( @Procs );
  my( $Regex ) = ( $Name =~ m#[$^\\/*?{}\[\]]+# );
  
  # Cycle through each Win32_Process object 
  # until you find the right one
  foreach my $Proc ( in( $ProcList ) )
  {
    if( $Regex )
    {
      push( @Procs, $Proc ) if( $Proc->{Name} =~ /$Name/i );
    }else{
       push( @Procs, $Proc ) if( lc $Name eq lc $Proc->{Name} );
    }
  }
  return( [EMAIL PROTECTED] );
}

-----Original Message-----
From: Dirk Bremer [mailto:[EMAIL PROTECTED] 
Sent: Friday, April 08, 2005 10:15 AM
To: Steven Satelle; perl-win32-admin@listserv.ActiveState.com
Subject: RE: Win32::Process::Info

Steven,

This is how I do it by window-title to insure that only one instance of
a given program can run at a time. It might be adaptable for your
purposes:

    # Check for another instance of this program.
    use Win32::GUI;
    my $Desktop = GUI::GetDesktopWindow();
    my $Window  = GUI::GetWindow($Desktop, GW_CHILD);
    my $Nbr     = 0;

    while ($Window)
    {
        my $Title  = GUI::Text($Window);
        if (length($Title) > 1 and $Title !~ /command prompt/i) {$Nbr++
if ($Title =~ /^archivepdf/i)}
        $Window = GUI::GetWindow($Window, GW_HWNDNEXT);
    }

    if ($Nbr > 1)
    {
        print(STDERR "Another instance of this program exists, exiting
program\n");
        print(STDERR "Ending $0\n");
        exit(1);
    } 

-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of
Steven Satelle
Sent: Friday, April 08, 2005 09:42
To: perl-win32-admin@listserv.ActiveState.com
Subject: Win32::Process::Info

I'm trying to find the process id of a process on a win32 system (NT or
2k) 
I want to kill it, but all I have is the name - ActiveForms.

I've been messing about with Win32::Process::Info but while I can get
the process list, I cant get the process name or any info from it. Can
anyone tell me where I'm going wrong?

Heres a sample of the code:

use WIn32::Process::Info;

$processes = Win32::Process::Info->new();

@pids = $processes->ListPids();


foreach $pid (@pids)
{
        @info = $processes->GetProcInfo($pid);
        print @info{'CreationDate'}, "\n";
}
_______________________________________________
Perl-Win32-Admin mailing list
Perl-Win32-Admin@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

_______________________________________________
Perl-Win32-Admin mailing list
Perl-Win32-Admin@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


_______________________________________________
Perl-Win32-Admin mailing list
Perl-Win32-Admin@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to