Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: How can I close a Windows program called from a my Perl script?

by bcarroll (Pilgrim)
on Jan 18, 2013 at 20:28 UTC ( [id://1014118]=note: print w/replies, xml ) Need Help??


in reply to How can I close a Windows program called from a my Perl script?

Since this is Windows, you can use WMI to start,monitor, and kill the process.
use strict; use Win32::OLE qw( in ); use Win32::OLE::Variant; usage if @ARGV < 2; if( my $Pid = RemoteExec( @ARGV ) ) { print "Process created with PID $Pid\n"; } else { print "Process not created\n"; } exit; ##################################################################### sub RemoteExec { my $Machine = shift; my $CommandLine = join " ", @_; my($CLASS, $WMI, $Process, $vPid); $Machine =~ s|^[\\/]+||; $CLASS = "WinMgmts:{impersonationLevel=impersonate}!//$Machine"; print "Trying to launch @_ on $Machine\n\n"; $WMI = Win32::OLE->GetObject( $CLASS ) or die "Unable to connect to \\\\$Machine :" . Win32::OLE->Las +tError(); $Process = $WMI->Get( "Win32_Process" ) or die "Unable to get a Win32_Process :" . Win32::OLE->LastErr +or(); $vPid = Variant( VT_I4 | VT_BYREF, 0 ); # $vPid (out) PID o +f the created Process if( 0 == $Process->Create( $CommandLine, undef, undef, $vPid ) ) { return $vPid; } else { return undef; } } ##################################################################### sub usage { my($ScriptName) = $0 =~ m/.*\\(.*)$/; print<<"USAGE"; Usage : $ScriptName [\\\\]COMPUTERNAME CommandLine Parameters example :$ScriptName COMPUTER1 NOTEPAD C:\\FooBar.txt USAGE exit; }
Original node (89593)

To kill the process use kill($Pid);

Replies are listed 'Best First'.
Re^2: How can I close a Windows program called from a my Perl script?
by BrowserUk (Patriarch) on Jan 18, 2013 at 21:01 UTC

    Killing the process may not be the same as closing it via its architected mechanism.

    For example, the output file may not get properly closed and flushed leading to incomplete content.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1014118]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-03-29 01:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found