Jump to content

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;")

$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)

I appreciate any help

Edited by corgano


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|")

        ;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)

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




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)
        ConsoleWrite("Waiting for helper"&@CRLF)

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


;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))

While True

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

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

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 


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.

  • Similar Content

    • By DesireDenied
      Hey guys,
      I having some hard times getting false-positive, probably because I am trying to execute my AutoUpdater.
      Here is my code:
      Global $iUpdateTimer = 0 While 1 checkUpdates(10) WEnd Func checkUpdates($iDelay = 10) $iDelay = $iDelay * 1000 * 60 If TimerDiff($iUpdateTimer) > $iDelay Then ConsoleWrite('checking for updates...' & @CRLF) $iUpdateTimer = TimerInit() If FileExists('AutoUpdater.exe') Then ShellExecuteWait('AutoUpdater.exe') ; this is the line which cause my problem EndIf EndFunc And AutoUpdater code:
      #include <MsgBoxConstants.au3> #include <FileConstants.au3> Global $sExecName = 'test.exe' Global $sUpdatePath = @UserProfileDir &'\desktop\AnyAppName\update\'& $sExecName Global $sUserPath = @UserProfileDir &'\desktop\AnyAppName\'& $sExecName Global $sCopyright = 'someUniqueStringHere' If Not FileExists($sUpdatePath) Then Exit 0 If FileGetVersion($sUpdatePath, $FV_LEGALCOPYRIGHT) <> $sCopyright Then Exit 0 ; checking if we really want to update and execute the file If FileGetVersion($sUpdatePath) > FileGetVersion($sUserPath) Then $iResponse = MsgBox(BitOR($MB_YESNO, $MB_ICONQUESTION),'AnyAppName', 'There is an update available, would you like to update?') If $iResponse == $IDYES Then If ProcessExists($sExecName) Then ProcessClose($sExecName) Sleep(500) EndIf FileCopy($sUpdatePath, $sUserPath, $FC_OVERWRITE) Sleep(3000) ShellExecute($sUserPath) Exit 1 EndIf EndIf Exit 0 I am not trying to ask, why is my code is getting recognized as false-positive, because this is quite obvious, but is there any other way to get things done without running external process?

    • 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
        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.
    • By ohaya
      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?
  • Create New...