Jump to content

External console programs ran by AutoIT are non-interactive

Recommended Posts


I am trying to convert a console script I made in VBscript to AutoIT because it has a lot of features that allows me to make it user-friendly. I notice though that it's not exactly tailored for console applications, but I'm trying to make it work and I'm running into a minor obstacle.

Once it's done doing its routine, I want the console window to remain open so the user can scroll the log before dismissing it. The only way I know of doing that in windows is to ask for user input. However, the MessageBox method wouldn't work because it disables interaction with the console window.

I saw that discussing how to make a "Press Any Key..." method was forbidden because that's keylogger territory. However, I tried figuring out how to make a "Press ENTER to continue..." method, and I can't seem to find one either.

So next thing I try is to run PAUSE.EXE using the RunWait method, but that didn't work for some reason. The "Press any key to continue..." prompt would display but the application would still close instantly.

After messing around with this, I notice that any console program ran within it will be non-interactive. Neither PAUSE or SET /P will wait for user input, and MORE will quit without outputting anything. I tried making a simple wrapper application to test this, and I get the same results:

#pragma compile(Console, true)
#include <AutoItConstants.au3>
RunWait(@ComSpec & ' /c "' & $CmdLineRaw & '"');

Is that intentional? Is there a known workaround?

ShellExecuteWait will not have this problem, but it will run console commands in a new console window, which is really not graceful if it's just to pause a script until user input...

In either case, could someone direct me to any post that could help me make a simple "Press Enter to continue" method for console scripts? The ones I've found haven't worked and I think they're intended for GUI applications.

Thank you.

Link to post
Share on other sites

If my application was GUI, yes. But in this context, I compile my application as a console application. In a Console application, Run and RunWait launch CMD and other console applications within the same console window as the application, and then let the remainder of the AutoIT script execute when it's done. This is true regardless of whether I use /c or /k with CMD.

I figured out how to make the console script pause until user input, but still, I'd like to keep getting feedback on whether or not this issue with external commands is a bug and if there's a workaround.

Global $ScriptPaused = False

Func Unpause()
    $ScriptPaused = False

Func Pause()
    $ScriptPaused = True
    HotKeySet("{ENTER}", "Unpause")
    While $ScriptPaused = True


Link to post
Share on other sites

Hi @Bluewoods, try to compile this:

#include <Misc.au3>
Global $hDLL = DllOpen("user32.dll")
Global $myWhdl = HWnd(_WinGetHandleByPID(@AutoItPID)) ; get Handle of this executable

ConsoleWrite('Press "Enter" to exit' & @CRLF)

Func Pause()
        Sleep(50) ; just wait
    Until _IsPressed("0D", $hDLL) And WinActive($myWhdl) ; ENTER key detected
EndFunc   ;==>Pause

; https://www.autoitscript.com/forum/topic/55094-get-windows-handle-if-you-have-pid/?do=findComment&comment=417603
Func _WinGetHandleByPID($vProc)
    Local $aWL = WinList()
    For $iCC = 1 To $aWL[0][0]
        If WinGetProcess($aWL[$iCC][1]) = $vProc And _ ; (window with same pid and visible)
                BitAND(WinGetState($aWL[$iCC][1]), 2) Then
            Return $aWL[$iCC][1]
    Return SetError(2, 0, 0);No windows found
EndFunc   ;==>_WinGetHandleByPID


small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to post
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
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...