#!c:\perl\bin\perl -w use strict; use Win32::Process; # one example of use - works on Win2000, Active State 5.6.1 system("echo dir c:\\ /o/s/w > c:\\temp\\dir_osw.bat"); if (-e "c:\\temp\\dir_osw.bat") { run_monitored(10,"c:\\temp\\dir_osw.bat"); } else { print "\nbuild the batch file by hand, or hack the code.\n"; } exit; # -------- # call with int # of seconds to wait, explicit path to command to run # lifted from Dave Roth's "Win32 Perl Programming: the Standard Extensions" # pages 290-300 ISBN 1-57870-067-1 sub run_monitored { use vars qw( $Process $Timeout $File $App $Cmd $bInherit $Dir $Flag $Pid $Result $tmp ); $Timeout = int( shift @_ ); $App = shift @_ ; $File = " " ; $Cmd = "$App $File"; $bInherit = 0; $Dir = "."; # IDLE_PRIORITY_CLASS - run when idle # NORMAL_PRIORITY_CLASS - as if normal process # HIGH_PRIORITY_CLASS - more CPU, other procs suffer # REALTIME_PRIORITY_CLASS - take over the PC $Flag = CREATE_SUSPENDED | CREATE_NEW_CONSOLE | NORMAL_PRIORITY_CLASS ; if (Win32::Process::Create( $Process, $App, $Cmd, $bInherit, $Flag, $Dir ) ) { $Pid = $Process->GetProcessID(); # print "\nnew process created in suspended state"; # print "\n$Cmd"; # print "\nwith an ID of $Pid , now resuming the process...\n"; print "\nnew process created with PID $Pid"; while (1 < $Process->Resume() ) { } print "\nwaiting $Timeout seconds ... "; $Result = $Process->Wait($Timeout * 1000); if (! $Result) { print "did not end in $Timeout sec, kill it \cG\cG\cG"; $Process->Kill(0); } else { print "finished under $Timeout sec."; } } else { print "\nunable to create the new process.\n"; print "Error: " . Win32::FormatMessage(Win32::GetLastError()) . "\n\cG"; } } # run_monitored