Bernie Cosell wrote:

> I'm a pretty experienced Perl programmer, but only on Unix and I'm a rank 
> newbie on Windows.  I'd like to write a very simple program to stop and 
> restart a program [so I can update its DB] and this'd be a snap on Unix 
> but I can't quite get my head around it on XP.
> 
> First, starting the new version: would I do a good old fork/exec?  Or 
> should I try to figure out how to use Win32::Process::Create? [or 
> something else?]

You first have to figure out how to find the process to kill.  You can
use WMI or Win32::Process::Info or find it manually in the Task Manager
or use something like Win32::GuiTest to search for title/class.  Once
you have the pid, you can use plain kill builtin or Win32::Process to
kill it.

I'd use Win32::Process::Info::GetProcInfo and mathch the 'ExecutablePath'
returned to the process in question and then use the corresponding
'ProcessId' reurned for the kill.  The WMI option to GetProcInfo gives
a lot more things to compare on than the NT option - including commandline
args - so it may be a bit slower, but more useful.

use strict;
use warnings;
use Win32::Process::Info;

my $cmd = 'F:\perl\bin\perl xx.pl';     # commandline for this script

my $mypid = $$;                         # pid of this running script
print "mypid=$mypid\n";

my $pi = Win32::Process::Info->new (undef, 'WMI'); # use WMI

foreach my $pref ($pi->GetProcInfo) {

        if (defined $pref->{CommandLine} and $pref->{CommandLine} eq $cmd) {

                my $pid = $pref->{ProcessId} || '<undef>';
                print "pid for $exe=$pid\n";
                kill 1, $pid;           # HUP the script
                print "shouldn't get here\n";
                last;
        }
}
print "shouldn't get here\n";

__END__

> Second, how to find and kill the existing process?  No "/dev/proc" to 
> find the existing process ID -- so how would I find it? [the task I want 
> to kill runs in the 'background', generally started with the "Startup" 
> tasks, and has a systray icon]. And once I find it, I assume it'd be best 
> to use Win32::Process:KillProcess rather than kill(1, $pid)?  
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to