in reply to Win32::Process::KillProcess

This works for me taking the example of creating a process in the documentation for Win32::Process

#!/usr/bin/perl use strict; use Win32::Process; my $ProcessObj; Win32::Process::Create($ProcessObj, "C:\\winnt\\system32\\notepad.exe", "notepad temp.txt", 0, NORMAL_PRIORITY_CLASS, ".")|| die "Oops"; my $exit_code = 1; my $wait=<STDIN>; chomp $wait; $ProcessObj->Kill($exit_code);

Some tests in your code would be to print out some of the variables you are using and seeing what there values are so you know what is happening with your values. You are calling values for exit code that will not exist until the program exits.

Some references:
Using OLE
Win32::Process::Info
I would suggest using Win32::Process::Info to get the process ID and using Win32::Process to kill the process. Specifically look at GetProcInfo (); and getting the name to search for the pid of a particualar application.

"No matter where you go, there you are." BB

Replies are listed 'Best First'.
Re: Re: Win32::Process::KillProcess
by jimbobfurley (Novice) on Dec 19, 2003 at 20:13 UTC
    either this :
    Win32::Process::KillProcess($pid, 1);
    or this :
    $p_obj->Kill($exitcode);
    ... do not work. The process that I am trying to kill is not touched, even though log traces state that the kill part of my code has been reached. Is there an error code that either of these functions return ?
    I'm coding this on a Windows 2003 Server machine, and the only thing that seems to work is :
    system 'fkill ' . $pid;
    I am at a total loss here.

      #!/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
        Hi again, I am obtaining the PID via Dave Roth's code from proclist.pl :
        # WMI Win32_Process class $CLASS= "winmgmts:{impersonationLevel=impersonate}$machine\\Root\\cimv +2"; $WMI= Win32::OLE->GetObject($CLASS) || die; foreach my $process (sort {lc $a->{Name} cmp lc $b->{Name}} in($WMI->I +nstancesOf("Win32_Process"))) { if($process->{Name} eq 'GravitixService.exe' || $process->{ProcessID +} eq 'Gravitix.exe') { # Store the Gravitix process ID $pid= $process->{ProcessID}; } }
        The PID ($pid) I get from this method is valid, I have verified that it is via a simple fkill on that PID.
        But, the following scenarios still do not work i.e. the process remains up and no kill operation appears to go through. I am coding on a Windows 2003 machine :
        kill method #1
        my $x= Win32::Process::KillProcess($pid, $exitcode);
        result -- nothing, process is not killed
        value of $x = 26423748


        kill method #2
        my $y= Win32::Process::Open($p_obj, $pid, 0); $p_obj->Kill(0);
        result -- nothing, process is not killed
        value of $y= 0

        I am at a loss as to WHY KillProcess is doing nothing in method #1, and in the case of method #2, why are Win32::Process:KillProcess and Win32::Process::Open both returning 0 ??