Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Run .exe and send input

by ksublondie (Friar)
on Oct 29, 2019 at 21:19 UTC ( [id://11108090]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks!

I have an exe I need to be able to automatically run and send an "enter" followed by an alt+F4 (close). Here's what I have so far:

use strict; use warnings; use lib 'f:\scripts524'; use English; my $cmd='CreateFile.exe'; my $pid = open CMD, '|-', $cmd or die $!; sleep 2; print CMD "\r"; print CMD "\n"; print CMD "\f"; print CMD hex '6B'; print CMD oct 153; close CMD; waitpid($pid,0);

My exe runs, but isn't getting the automated input. I haven't had much luck in my research so far. Is this even possible?

Replies are listed 'Best First'.
Re: Run .exe and send input
by rjt (Curate) on Oct 29, 2019 at 21:47 UTC

    Windows GUI applications do not typically take their input from standard input, so this won't work. You could probably do this with Win32::API, but even though Perl is my favorite language and this is perlmonks.org, in this case I would recommend AutoHotKey, as it makes this sort of interaction stupidly easy, and you'll find a lot more documentation and examples online.

    Untested.Tested.

    ^F5:: run, CreateFile.exe WinWaitActive, CreateFile, ,2 if ErrorLevel { MsgBox, WinWaitActive timed out. return } else { send, {Enter} WinClose } return

    Note that WinWaitActive expects the window title, which is not necessarily the same as the executable. You can match on other criteria as well.

    Run the script, and then you can trigger the execution/Enter/close at will with Ctrl-F5. There are other activation methods you can use, but the hotkey approach is easy to debug. I also changed your logic to wait 2 seconds for the window to activate, rather than just sleeping for 2 seconds and hoping for the best, but this is just an example to get you started.

    AHK Documentation

Re: Run .exe and send input
by stevieb (Canon) on Oct 29, 2019 at 21:26 UTC

    This almost appears as though you want something to occur behind the user's back, and without their knowledge, including creating a file that they may not know about.

    I don't use Windows often, but I know an Alt-F4 silently closes a Window.

    Am I correct in these assumptions? If not, could you please explain what it is you're really trying to do here so that myself and other Monks aren't assuming you're trying to do something nefarious? (We see questions to that degree periodically).

      Yes, you are correct in that I need this to occur behind the user's back and no, this is not nefarious.

      We have a 3rd party app that uses a unique keyfile on the workstation. This exe creates that file. We'd like to convert these workstations to a vmware appstack environment, so I need the ability to run this exe to create these keyfiles dynamically as needed.

Re: Run .exe and send input
by GrandFather (Saint) on Oct 30, 2019 at 00:58 UTC

    Take a look at Win32::GuiTest. It provides ways to find and manage GUI windows such as WaitWindow(...) and SendKeys(...).

    Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-04-18 23:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found