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

Fellow Monks --

The OS is: the 'wonderful' Windows XP Professional
The quest is to forcibly stop then restart a program.
I am logged in with admin privledges, so, in theory this should not be an issue. The code is below:
#!/usr/bin/perl -w use strict; use Win32; use Win32::Process; use Win32::Process::Info; my $execID2find="prog2stopnstart32.exe"; my $pi = Win32::Process::Info->new; my @info = $pi->GetProcInfo(); my @procsOfInterest = grep{ $_->{Name} eq $execID2find } @info; if( @procsOfInterest ) { print "KILL $_->{ProcessId}\n", kill 9, $_->{ProcessId}, for @procsOfInterest; } else { print "No process with the name $execID2find was found\n"; } # Create the process object. my $ProcessObj; Win32::Process::Create($ProcessObj, "C:\\Program Files\\programs\\prog2sto +pnstart32.exe", "prog2stopnstart32", 0, NORMAL_PRIORITY_CLASS, ".")|| die ErrorReport(); $ProcessObj->Suspend(); $ProcessObj->Resume(); # $ProcessObj->Wait(INFINITE); sub ErrorReport{ print Win32::FormatMessage( Win32::GetLastError() ); }
When I run the script the PID is identified it attempts to kill the program, indicated by a delay, then returns 0, indicating that the program has not been killed. I cannot restart the program without killing it first.
I believe that what I need is to be able to override whatever is stopping me from killing the program, then kill it.
I am certianly open to more than one way to do this....A combination of Windows Scripting and Perl perhaps?
As always your wisdom and help are greatly appreciated in advance.

Replies are listed 'Best First'.
Re: forcible termination of Win32 program then restart
by BrowserUk (Patriarch) on Sep 20, 2006 at 16:39 UTC

    Win32 doesn't 'do' signals...much. Perl.exe does some emulation of them and responds to them in a limited way, but that is of no help if the process you are trying to signal is a non-Perl process.

    The win32 way of terminating another process is to us TerminateProcess. To use that call you need to translate $pid into a process handle, which can be done with OpenProcess. Use Win32::API(Win32::API::Prototype) to call those system apis.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: forcible termination of Win32 program then restart
by Anonymous Monk on Sep 20, 2006 at 17:08 UTC
    I know this is probably pushing the bounds of being OT, but, I have looked at the documentation of these including the Windows sight, and there are no concrete examples.
    Any further possibility of addtional enlightenment? perhaps example?
    Thanks.

      This example takes a process identifier (pid) from the command line argument and uses the api's above to kill it.

      #! perl -slw use strict; use Win32::API::Prototype; ApiLink( 'kernel32', q[ HANDLE OpenProcess( DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwProcessId ) ]) or die $^E; ApiLink( 'kernel32', q[ BOOL TerminateProcess( HANDLE hProcess, UINT uExitCode ) ]) or die $^E; my $pid = shift @ARGV or die 'No pid supplied'; ## The magic number 0x1f0fff == PROCESS_ALL_ACCESS ## See http://msdn.microsoft.com/library/default.asp ## ?url=/library/en-us/dllproc/base/process_security_and_access_right +s.asp ## for details/other possible values. my $hProcess = OpenProcess( 0x1F0FFF, 0, $pid ) or die $^E; TerminateProcess( $hProcess, -1 ) or die $^E; __END__ c:\test>573923 784

      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.