A Better Way to Read Stdout

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
JJohnston2
Posts: 204
Joined: 24 Jun 2015, 23:38

A Better Way to Read Stdout

05 Jul 2017, 00:32

Does anyone have a good code example that shows stdout captured from a console command?
  1. Without a console window popping up
  2. Without redirecting command output to a file using > or >> and then reading the file
Code below is from the RunWait help topic. It captures stdout from an executed command (or set of commands). Unfortunately, it has a command window that pops up (unwanted). If it didn't have a window pop up it would be exactly what I'm looking for.

Code: Select all

runWaitMany(commands) {
     ; WshShell object: http://msdn.microsoft.com/en-us/library/aew9yb99
    shell := ComObjCreate("WScript.Shell")
    ; Open cmd.exe with echoing of commands disabled
    exec := shell.Exec(ComSpec " /Q /K echo off")
    ; Send the commands to execute, separated by newline
    exec.StdIn.WriteLine(commands "`n" "exit")  ; Always exit at the end!
    ; Read and return the output of all commands
    Return exec.StdOut.ReadAll()
}
The code above can be tested with this (if you want to try it):

Code: Select all

    MsgBox,,,% RunWaitMany("
    (
    echo Put your commands here,
    echo each one will be run,
    echo and you'll get the output.
    )")
This code below is in the FileOpen help topic... but doesn't show a way to execute a command before doing stdout read/writes.

Code: Select all

    ; Open a console window for this demonstration:
    DllCall("AllocConsole")
    
    ; Open the application's stdin/stdout streams in newline-translated mode.
    stdin  := FileOpen("*", "r `n")  ; Requires v1.1.17+
    stdout := FileOpen("*", "w `n")

    stdout.Write("Enter your query.`n\> ")
    stdout.Read(0) ; Flush the write buffer.
    query := RTrim(stdin.ReadLine(), "`n")
    stdout.WriteLine("Your query was '" query "'. Have a nice day.")
    stdout.Read(0) ; Flush the write buffer.
    Sleep 5000
This topic on Stack Overflow has other (more complicated ways) to run a hidden/separate script:

https://stackoverflow.com/questions/322 ... using-exec


Where I've needed this to date, I have the console command output redirected to a temporary file, then read the temp file contents and delete.
  1. That is not always sufficient if a console command hangs (I don't get anything back because it never finishes executing)
  2. Writing a temporary file and reading it back... surely there must be a better way. And there is. But re-coding some of that stuff on Stack Overflow looks like it would take some time to get it working.
Wondering if anyone else has had or solved this problem, or maybe has a library or link.
JJohnston2
Posts: 204
Joined: 24 Jun 2015, 23:38

Re: A Better Way to Read Stdout

05 Jul 2017, 10:48

Didn't find these posts during previous searches... these look promising. Thanks.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], downstairs, OrangeCat and 299 guests