Jump to content

Reading STDout of running process


corgano
 Share

Recommended Posts

How do you read the STDout stream from an already running process? I know you can launch a program via run() with flags to get the SDTout stream, but being able to change and re-run a script without needing to restart the running process would be very useful

In this thread, it is suggested that one could use the GetStartupInfo methiod, but I'm not sure how to call it or what dll it's in

'?do=embed' frameborder='0' data-embedContent>>

Edit: Here is my attempt at changing it to autoit. I'm confused as to how you're supposed to specify what process you're trying to get the stream of, and am probably doing it wrong.

$STARTUPINFO=DllStructCreate("" & _
  "DWORD  cb;" & _
  "ptr lpReserved;" & _
  "ptr lpDesktop;" & _
  "ptr lpTitle;" & _
  "DWORD  dwX;" & _
  "DWORD  dwY;" & _
  "DWORD  dwXSize;" & _
  "DWORD  dwYSize;" & _
  "DWORD  dwXCountChars;" & _
  "DWORD  dwYCountChars;" & _
  "DWORD  dwFillAttribute;" & _
  "DWORD  dwFlags;" & _
  "USHORT   wShowWindow;" & _
  "USHORT   cbReserved2;" & _
  "ptr lpReserved2;" & _
  "HANDLE hStdInput;" & _
  "HANDLE hStdOutput;" & _
  "HANDLE hStdError;")


DllCall("Kernel32.dll","none","GetStartupInfo","ptr",DllStructGetPtr($STARTUPINFO))
$handle = DllStructGetData($STARTUPINFO,"hStdOutput")
#include <Constants.au3>

Local $line
While 1
    $line = StdoutRead($handle)
    If @error Then ExitLoop
    if StringLen($line) > 3 Then ConsoleWrite($line)
WEnd 

I appreciate any help

Edited by corgano

0x616e2069646561206973206c696b652061206d616e20776974686f7574206120626f64792c20746f206669676874206f6e6520697320746f206e657665722077696e2e2e2e2e

Link to comment
Share on other sites

Bump? Is there something obvious that I'm missing here or does no one really know? Is it impossible to do with AutoIt?

One thing I'm considering is having a helper script that works between the program and my program, that will detect when my main script starts and redirect the console information to it . This seems like a really awkward and roundabout way of doing things... 

I Have a completed example here

Helper.au3 (compile before use)

#include <Constants.au3>

AutoItWinSetTitle("Helper") ;used for the main script to detect it

$MainPID = ""


While Sleep(1000)   ;while running (add exit conditions in this loop)

    If WinExists("Mainscript|") Then    ;Detect the main script starting. This will only happen if it was started with no cmdline

        ;get the path (as part of it's title) of the mainscript and close it
        $title = WinGetTitle("Mainscript|")
        ProcessClose(WinGetProcess($title))

        ;get the path of the main script
        $mainPath = StringReplace($title, "Mainscript|", "")
        $mainDir = StringLeft($mainPath, StringInStr($mainPath, "\", 0, -1))
        If StringRight($mainPath, 4) = ".au3" Then $mainPath ='"C:\Program Files (x86)\AutoIt3\AutoIt3.exe" "'&$mainPath&'"'

        ;run the main script again, this time as child so we can send it console info
        $MainPID = Run($mainPath&" "&@AutoItPID, $mainDir, Default, $STDIN_CHILD + $STDOUT_CHILD)

        While ProcessExists($MainPID)
            Sleep(1000)

            ;code to get useful data here
            StdinWrite($MainPID, "From helper:  "&Random(0,9999999,1) & @CRLF)
            If @error Then
                StdinWrite($MainPID)
                ExitLoop
            EndIf

        WEnd

    EndIf

WEnd

and the main script

#include <GuiRichEdit.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

;when the helper runs it then it's run with the helper's PID
if $CmdLine[0] = 0 Then ;when run by itself

    AutoItWinSetTitle("Mainscript|"&@ScriptFullPath)    ;the helper looks for this, and gets the script path from this

    If not WinExists("Helper") Then
        ConsoleWrite("Starting helper..."&@CRLF)
        Run("helper.exe")
    Else
        ConsoleWrite("Waiting for helper"&@CRLF)
    EndIf

    while sleep(100)    ;wait for the helper to close it. When the helper
    WEnd

EndIf

;if the helper launches it

Local $hGui, $hRichEdit, $iMsg
$hGui = GUICreate("Example (" & StringTrimRight(@ScriptName, 4) & ")", 320, 350, -1, -1)
$hRichEdit = _GUICtrlRichEdit_Create($hGui, "Command line: "&$CmdLineRaw&@CRLF, 10, 10, 300, 220, BitOR($ES_MULTILINE, $WS_VSCROLL, $ES_AUTOVSCROLL))
GUISetState()


While True

    $line = ConsoleRead()
    if StringLen($line) > 3 Then
        _GUICtrlRichEdit_AppendText($hRichEdit, $line)
    EndIf


    $iMsg = GUIGetMsg()
    Select
        Case $iMsg = $GUI_EVENT_CLOSE
            _GUICtrlRichEdit_Destroy($hRichEdit) ; needed unless script crashes
            GUIDelete()     ; is OK too
            Exit
    EndSelect
WEnd

The main script can be either au3 OR exe and the helper can find and send data to it. To test:

Run helper.exe

Run the source from SciTE, Observe it working

Close it. Leave the helper running

Compile the main script, and run main.exe

Helper will see it and attach to it. Observe it working

Probably nto the best way to do it, but works. Anyone got a better way 

0x616e2069646561206973206c696b652061206d616e20776974686f7574206120626f64792c20746f206669676874206f6e6520697320746f206e657665722077696e2e2e2e2e

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...