Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

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

by danren924 (Initiate)
on Jan 18, 2013 at 19:56 UTC ( [id://1014114]=perlquestion: print w/replies, xml ) Need Help??

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

I am new to Perl, so my problem may have a simple solution. I am running a Perl script that calls a Windows program using a command line. The command line parameters include source and results-destination file names. As the program processes the input file, I need to monitor a "percent complete" indicator, then close the Windows program, and return to the Perl script for post processing steps. I am presently using the "system()" command. However, since the Windows program does not routinely close at the end of its processing, the execution never returns to the script. If I manually close the Windows program, the post-processing steps of the script proceed as desired. How can I accomplish first calling a Windows program, then monitoring the program, then closing the program when processing is complete? Thanks for your help.
  • Comment on How can I close a Windows program called from a my Perl script?

Replies are listed 'Best First'.
Re: How can I close a Windows program called from a my Perl script?
by BrowserUk (Patriarch) on Jan 18, 2013 at 20:26 UTC
    However, since the Windows program does not routinely close at the end of its processing, the execution never returns to the script.

    What is the program, and what do you have to do manually to cause it to close?

    If the program you are trying to control is a command line program that requires some additional (command line) keyboard input -- say, "quit\n" -- in order for it to terminate, the solution required is usually relatively simple.

    If however, the program is a GUI program that requires mouse (or keyboard input to the application window), that is somewhat harder. Still possible, but it would require more information.

    The easiest way to supply us with the information we need to help you, is to tell use what the program is so that we can test our solutions before replying. This assumes that it is a program that comes with the OS, or is freely available for download somewhere on the web.

    If the program is proprietary application; or costs money to obtain, be prepared for an extended Q&A session while we work out what you can do to control it.


    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.

      The program is, unfortunately, a proprietary GUI program, and not widely available. It does, however, use standard Windows GUI controls or keyboard shortcuts to close, such as "Alt-F, E" or a mouse click on the "X".

      The only command line parameters are the source and destination file names.

      Thanks again.

        Then you might be able to use something like this which starts a copy of notepad aysnchronously (system 1, ...), then waits 10 seconds before sending it the Alt-F X key sequnce to close it:

        #! perl -slw use strict; use Win32::GuiTest qw[FindWindowLike SetForegroundWindow SendKeys]; system 1, q[notepad.exe]; Win32::Sleep( 10000 ); for ( FindWindowLike( undef, 'Notepad' ) ) { SetForegroundWindow( $_); SendKeys( '%fx', 5 ); }

        Note: that as coded, this will close all running copies of notepad. To be more selective, see the pod for Win32::GuiTest::FindWindowLike().


        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.
        ), then waits 10 seconds before sending it the Alt-F X key sequnce to close it: #! perl -slw use strict; use Win32::GuiTest qw
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
    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);

      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: perlquestion [id://1014114]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2024-04-19 07:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found