Retrieves the standard output (StdOut) of a program.
The functions included are:
RunConsoleAppWait() Runs a program and returns its standard output when it exits.
RunConsoleApp() Runs a program and returns immediately. The program's standard output can be retrieved using GetConsoleAppStdOut(). The return value of this function must be passed to the CloseConsoleAppHandle() function when no longer needed.
GetConsoleAppStdOut() Retrieves the standard output of a program that was started using the RunConsoleApp() function.
CloseConsoleAppHandle() Must be called to free resources allocated by calls to RunConsoleApp()Here is an example using the RunConsoleAppWait() function:
SIMPLE EXAMPLE
MsgBox, % RunConsoleAppWait("cmd.exe /c echo Hello World.")Here is an example using the RunConsoleApp() function:
REAL-TIME EXAMPLE
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
#Include ConsoleApps.ahk
;Create GUI window with an edit control to print the output.
Gui, Add, Edit, x15 y15 w460 h300 -Wrap ReadOnly hscroll vtxtStdout,
Gui, Show, w490 h330, Standard Input/Output Redirection Example
; Create the process
ConsoleAppHandle := RunConsoleApp("ConsoleApps_TestFile.bat -p nothing", A_ScriptDir)
; In reality, this function will never return with a ConsoleAppHandle value of 0, but
; it is good to check for this possibility in case future versions do return a value of
; 0 for failure.
if (ConsoleAppHandle == 0)
{
MsgBox, Error running cmd.exe
ExitApp
}
/* Continuously retrieve the process's output and write it to the edit control
* in a loop as it arrives.
*/
Loop
{
; Append the ConsoleApps standard output to StdOut.
ConsoleAppStillRunning := GetConsoleAppStdOut(ConsoleAppHandle, StdOut, BytesAppended, ExitCode)
; If StdOut was appended with new text, write it to the GUI window.
if (BytesAppended)
GuiControl, , txtStdOut, %StdOut% ; append the output to the edit control.
; If the ConsoleApp is no longer running, then stop checking for output.
if (!ConsoleAppStillRunning)
break
; Give up remainder of time-slice to give the child process a chance to generate more output
; before checking again.
sleep 0
}
; This function must be called when the ConsoleAppHandle is no longer needed.
CloseConsoleAppHandle(ConsoleAppHandle)
if (!ExitCode)
MsgBox, The program has completed successfully.
else
MsgBox, The program has exitted with an error code of %ExitCode%.
return
; The following label is receives control when the user closes the GUI window.
GuiClose:
ExitAppHere is the code for the latest version of the script:
http://www.autohotke...eApps.2.1.1.zip
CHANGES
Version 2.1.1NEW: Script now initializes itself regardless of how it is loaded.
Version 2.1FIX: Script can now initialize properly on startup when loaded as a library by calling the CONSOLEAPPS() function.Version 2.0NEW: Functions renamed to be loaded from as a library.Version 1.2FIX: Now correctly redirects the StdErr stream.
DOC: Updated and revised documentation.Version 1.1DOC: Revised example code.[/list]KNOWN ISSUES/RESOLUTION
ConsoleApps fails to capture error messages produced by applications.
CAUSE: This is caused by a bug in the design of the script, because of which output is not captured from the StdErr stream of the application.
RESOLUTION: Download and include version 1.2 of this script.




