#!/usr/local/bin/perl
use strict;
use warnings;
use Win32::Process;
use Win32::Process::Info;
my $Process_Obj = Win32::Process::Info->new (undef , "NT");
my @info = $Process_Obj->GetProcInfo ();
my $Pid;
my $Name;
my %Process;
for my $i ( 0 .. $#info ){
$Pid = $info[$i]{"ProcessId"};
$Name = $info[$i]{"Name"};
print "$Pid \t $Name \n";
$Process{$Pid} = $Name;
}
print "What program do you want to kill?\n";
my $exit_code = 1;
my $kill = <STDIN>;
chomp $kill;
Win32::Process::KillProcess($kill, $exit_code)
Update: Simplified for loop to only focus on name and Pids
This works for me. Now I broke down the data structure to show the important bits of Win32::Process::Info . And I am getting my PID from STDIN but you can use the Name and Process ID keys from the Win32::Process::Info to identify the process you want to kill and then call Win32::Process::KillProcess to end it. But you need to make sure in that case that the PID is a running process. I don't know how you are determining the PID programatically on how this will work.
If you give more specifics I can show more specific examples this is done in a way to show where the PID can come from.
Update:
I am trying to simplify the id, in your intial post you did not show where your PID was coming from. For this to work we need to know how you are determining the pid to kill. In this example I am printing a list and having the user choose but the same information can be used programatically to kill processes. I tend to search for a name and kill all instances of that name. But again this is just an example. If you need any more info just describe what you are doing with a bit more detail and I will try to help.
"No matter where you go, there you are." BB
|