Jump to content
Sign in to follow this  
corgano

Reading STDout of running process

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

Share this post


Link to post
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

Share this post


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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By nacerbaaziz
      hello autoit team
      is there any wey to check if any process run as admin or no?
      i mean e.g if i want to restart any process, now i have the ability to get the process path and commands line
      what i need is a wey to check if the process was runing as admin or no to restart it with the same state.
      here is the part that am using it to restart the process
      func _processRestart($i_pid, $s_ProcessPath) if not (ProcessExists($i_ProcessPid)) then return SetError(1, 0, -1) local $s_ProcessWorkDir = _WinAPI_GetProcessWorkingDirectory($i_ProcessPid) ProcessClose($i_ProcessPid) ProcessWaitClose($i_ProcessPid) ProcessWait(ShellExecute($i_pid,"", $s_ProcessWorkDir)) ProcessesGetList() return true endFunc thanks in advance
    • By Burgs
      Greetings,
        I would like to be able to write a script to send commands to the console for creation of Gstreamer pipelines.  I was thinking of something similar to this:
      Local $iPID = Run("C:\Windows\System32\cmd", "", @SW_MAXIMIZE) ;THIS OPENS THE CONSOLE...!!! if $iPID == 0 Then ConsoleWrite(@CRLF & "I DID NOT OPEN CMD...error: " & @error & @CRLF) if $iPID <> 0 Then ConsoleWrite(@CRLF & "I OPENED CMD...!!!" & @CRLF) $hCmd = WinGetHandle("C:\WINDOWS\system32\cmd.exe") if $hCmd <> 0 Then WinActivate($hCmd) ;ensure command console is active... $sOutput = Send("cd C:\gstreamer\1.0\x86_64\bin" & @CRLF, $SEND_RAW) $sOutput = Send("gst-launch-1.0 videotestsrc ! autovideosink" & @CRLF, $SEND_RAW) Sleep(3000) ControlSend($hCmd, "", "", "exit" & @CR) EndIf ;$hCmd NOT "0"... I don't really know if this is the best way to open the console and send commands into it.  I'm also not sure about how to best catch any errors that may occur...likely this needs to be accomplished with the STDOUTREAD command however I've not had experience using it before and therefore would appreciate some advice that anybody may offer. 
      Basically I'm seeking guidance on how to best automate the opening of the console, sending lines of commands to be executed, and handling any potential errors in the execution of those commands...I thank you in advance.  Regards.
    • By Jeep
      Here is a small library (UDF) that I use all the time. Not transcendent, it’s just to simplify my life.
      When I use the ConsoleWrite function, more than 99.99 percent, I go to the line (Newline), more than 99.99 %, parameters à the same except information that i exam. _CW is born.
      The second one _CW_Fmt allows to display marks (Fmt for Formatting) and allows to evaluate the format the length of a variable and the position of some characters without counting too much :-).
      For the functions derived from MsgBox () again the same observation, always the same parameters following the context: Info, Error or Warning. Thus were born _MB_Info, _MB_Error and _MB_Warning.
      As for _MB_IsOk, the same observation about the logical answer we are interested in a question with two choices: agree or disagree and not much else (True or False).
      All your remarks, suggestions and constructive criticism are welcome.
      JPD_Simply.au3
      JPD_Simply_Demo.au3
    • By ohaya
      Hi,
      I am trying to use ConsoleWrite() to output some debug to stdout/console on a Windows command (cmd) window, but it seems like nothing is being output.
      I noted that the help says:
      but what does that mean (the "compiled as a console application" part?
      I have also tried adding
      to the beginning of my .au3 and re-compiling, but when I run the .exe in a command window, nothing is being output?
      How do I get the output from ConsoleWrite() to appear?
      Thanks,
      Jim
    • By Miliardsto
      I want to detect if exact process or window uses directx or opengl or maybe something else library used in applications.
      Thats becouse there could be many windows with same names and different names and the same with process. I got so much process names I want to my script works with all, so i want standardize.
      All of this processes uses DirectX or OpenGL so then If I check this window/process uses these libraries I will be sure thats the right process
×
×
  • Create New...