Jump to content

How to simulate user input in console apps?


Recommended Posts

Hello :), I am trying simulate user input in console apps, so far this is what I have tried:

#include <AutoItConstants.au3>
#include <MsgBoxConstants.au3>

Example()

Func Example()
    Local $iPID = Run("test.bat", "", @SW_HIDE, $STDIN_CHILD + $STDOUT_CHILD)

    StdinWrite($iPID, "Banana")

    Local $sOutput = "" ; Store the output of StdoutRead to a variable.

    While 1
        $sOutput &= StdoutRead($iPID) ; Read the Stdout stream of the PID returned by Run.
        If @error Then ; Exit the loop if the process closes or StdoutRead returns an error.
            ExitLoop
        EndIf
    WEnd

    MsgBox($MB_SYSTEMMODAL, "", "The output string is: " & @CRLF & $sOutput)
EndFunc   ;==>Example

Code for test.bat:

@echo off
set /p var = "Input Var: "
echo This is the var: %var%

 

I can't write "Banana" when the the bat script prompts for input :(, I can't find any solution on the WWW for this...

 

Thanks in Advance, TD :)

Edited by TheDcoder
Batch code correction

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

@computergroove I think its clear, I am trying to simulate user input in a console application, Like typing something in a command :)

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

@computergroove That's used to simulate... keyboard input in any application, also it will not work when the window is hidden and the output stream is redirected, TD :)

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

at all vars within bat/cmd etc are written %SomeVarName%. Standardly you can't use the Name of the bat. You would have to use cmd.exe instead. but i am not sure about all of that but that's what i only know for this.

  • C++/AutoIt/OpenGL Easy Coder
  • I will be Kind to you and try to help you
  • till what you want isn't against the Forum
  • Rules~

 

Link to comment
Share on other sites

@RaiNote & @gruntydatsun :oops:, For some reason the last % was not copied. Fixed now

If you quote the prompt it always works.  If you dont quote it, anarchy mannnn

Thanks! :)

 

It still does not work... TD :(

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

"Send" keystrokes worked into XP CMD windows, but injecting keystrokes into console apps is now blocked by Microsoft, and has been since 2008R2:

http://blogs.technet.com/b/askperf/archive/2009/10/05/windows-7-windows-server-2008-r2-console-host.aspx

This was a nasty shock for me when I developed a large testing app in XP and it did not work in the 2008R2 deployment environment.

However, I found that you can redirect input. My particular use cases required specific, predictable keystrokes to start command line apps and provide command line inputs, so I created text files and started my CMD windows with the input redirected from the text file. This works fine but you have to know the entire input session from beginning to end for this to work, as you can't modify the input files once the session has begun.

If you want to start a CMD and inject commands on-the-fly, then you will have to do something tricky, such as start a CMD window with a BAT file that checks another BAT file at intervals, and executes the commands in there.

Link to comment
Share on other sites

I can still StdinWrite to a running batch file and capture it's output in Windows 10.  I don't have a copy of windows 7 available to try it on but the attached files work on my computer win 10 x64.

This example has a batch file that you feed a path then it does a dir on the path.  The output is captured by StdOutRead.

The batch file looks like:
 

@echo off
set /p var1="Enter path to run dir on"
dir %var1%

And the autoit file looks like:
 

#include <AutoItConstants.au3>

Example()

Func Example()
    Local $iPID = Run("dir_command.bat", "", @SW_SHOW, $STDIN_CHILD + $STDOUT_CHILD) ;run batch file
    StdinWrite($iPID, "C:\SharedData\TastefulPicsOfPete")                            ; send folder path to stdin
    Local $sOutput = ""                                                              ; Store the output of StdoutRead to a variable.
    While 1
        $sOutput &= StdoutRead($iPID)       ; Read the Stdout stream of the PID returned by Run.
        If @error Then                      ; Exit the loop if the process closes or StdoutRead returns an error.
            ExitLoop
        EndIf
    WEnd
    msgbox(1,"Value of $sOutput",$sOutput)
EndFunc   ;==>Example

And the output looks like this:

 

Output.jpg

Edited by gruntydatsun
ok so pasting screenshots doesnt work attached to post
Link to comment
Share on other sites

@gruntydatsun It will work fine on older versions of windows, i.e < Windows 7, see the link which clicked posted :)

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

set /p var = "Input Var: "

This line actually needs a variable named %var % to get the value (note the whitespace). Environmental variable can be almost any character including spaces as you have a space.

Using StdinWrite is a PITA as you may need to keep looping until StdinWrite is needed and then get out at the right time. Usually Stdoutread will not error until the StdIn stream is closed.

This seems to work though have doubt I have done well with this example. The constants gave me issues with my version of AutoIt so they went bye bye.

Example()

Func Example()
    Local $sOutput = "" ; Store the output of StdoutRead to a variable.
    Local $iPID = Run("cmd /q /c test.cmd", "", @SW_SHOW, 3)
    ; Get the initial data output here.
    Do
         Sleep(10)
        $sOutput &= StdoutRead($iPID) ; Read the Stdout stream of the PID returned by Run.
    Until @extended
    ; Clear the output here.
    $sOutput = ''
    ; Send the input.
    StdinWrite($iPID, "Banana" & @CRLF)
    ; Close the input stream so StdoutRead sets error.
    StdInWrite($iPID)
    ; Get the rest of the data.
    Do
        Sleep(10)
        $sOutput &= StdoutRead($iPID)
    Until @error
    ; Show the result.
    MsgBox(0, "", "The output string is: " & @CRLF & '"' & $sOutput & '"')
EndFunc   ;==>Example

 

Link to comment
Share on other sites

@MHz This is the output:

 Volume in drive C is Windows 8.1
 Volume Serial Number is XXXX-XXXX

 Directory of C:\Users\TheDcoder\Desktop

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

Your result is different to mine.

This is what I copy from the msgbox.

---------------------------

---------------------------
The output string is: 

"This is the var: Banana

"
---------------------------
OK   
---------------------------

I am testing with the AutoIt code I posted above and using this file named test.cmd.

@echo off
set /p "var=Input Var: "
echo This is the var: %var%

I cannot explain why you have output of the volume information. I am currently using Windows 7 x64 with AutoIt 3.3.10.2. AutoIt script is being run uncompiled from Scite. Repeatedly testing I get the same output. I am confused.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...