Jump to content

Unable to reread Std Stream


Recommended Posts

This is what I am running (this is just an example):

#include <Constants.au3>
$foo = Run(@SystemDir & "\cmd.exe", @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD + $STDIN_CHILD)
While 1
    $line = StdoutRead($foo)
    If @error = -1 Then ExitLoop
    MsgBox(0, "STDOUT read:", $line)
WEnd
While 1
    $line = StderrRead($foo)
    If @error = -1 Then ExitLoop
    MsgBox(0, "STDERR read:", $line)
WEnd
MsgBox(0, "Done", "CMD Opened.  Sending Help.")
StdinWrite($foo, "help" & @LF)
StdinWrite($foo)
MsgBox(0, "Debug", "Help Sent.  Trying to Read.")
While 1
    $line = StdoutRead($foo)
    If @error = -1 Then ExitLoop
    MsgBox(0, "STDOUT read:", $line)
WEnd
While 1
    $line = StderrRead($foo)
    If @error = -1 Then ExitLoop
    MsgBox(0, "STDERR read:", $line)
WEnd
MsgBox(0, "Debug", "Exiting...")oÝ÷ ØéÁzÖµßAèÁëÞ­§íz»ayø«²Ô^iا+cºËl¶l"Ëaƺ
+ë-jw«®èl©j»@+mç!jxw±jjez;¬´Ä®º+{-y§h|Jë¢
Edited by AutoIt Smith
Link to comment
Share on other sites

Tested on .133 and Still not working. Researching it a bit more. If I send a command to the CMD window, it automatically closes. Here is something I am working on. Send help, or any command and you will find the cmd process closes.....

#include <Constants.au3>
#include <GUIConstants.au3>
$StdGUI = GUICreate("GUIExample", 492, 431, 200, 125)
$StdSend = GUICtrlCreateButton("Send StdIn", 336, 8, 145, 33)
$StdRead = GUICtrlCreateInput("", 8, 16, 313, 21, -1, $WS_EX_CLIENTEDGE)
GUICtrlSetColor($StdRead, 0xC0C0C0)
GUICtrlSetBkColor($StdRead, 0x800000)
$StdOut = GUICtrlCreateEdit("", 8, 55, 475, 180, BitOR($ES_READONLY, $WS_VSCROLL), BitOR($WS_EX_CLIENTEDGE, $WS_EX_STATICEDGE))
GUICtrlSetColor($StdOut, 0xC0C0C0)
GUICtrlSetBkColor($StdOut, 0x800000)
GUICtrlSetTip($StdOut, "Reads the Stdout of Command Process.")
$StdErr = GUICtrlCreateEdit("", 8, 240, 475, 180, BitOR($WS_BORDER, $WS_VSCROLL), BitOR($WS_EX_CLIENTEDGE, $WS_EX_STATICEDGE))
GUICtrlSetColor($StdErr, 0xC0C0C0)
GUICtrlSetBkColor($StdErr, 0x800000)
GUICtrlSetTip($StdErr, "Reads the Stderr of Command Process.")
$CmdPid = Run(@SystemDir & "\cmd.exe", @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD + $STDIN_CHILD)
GUISetBkColor(0x000080, $StdGUI)
GUISetState(@SW_SHOW, $StdGUI)
While 1
    $msg = GUIGetMsg()
    If Not ProcessExists($CmdPid) Then Exit MsgBox(16, "ERROR", "The Command process is gone!")
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
        Case $msg = $StdSend
            SplashTextOn("Updating...", "Sending command and reading...", 300, 30)
            StdinWrite($CmdPid, GUICtrlRead($StdRead) & @LF)
            StdinWrite($CmdPid)
            While 1
                $Buffer = StdoutRead($CmdPid)
                If @error Then ExitLoop
                GUICtrlSetData($StdOut, GUICtrlRead($StdOut) & @CRLF & $Buffer)
            WEnd
            While 1
                $Buffer = StderrRead($CmdPid)
                If @error Then ExitLoop
                GUICtrlSetData($StdErr, GUICtrlRead($StdErr) & @CRLF & $Buffer)
            WEnd
            GUICtrlSetData($StdRead, "")
            SplashOff()
    EndSelect
WEnd
ProcessWaitClose($CmdPid)
Exit

I really need to know is this abug, or something wanted by Std functions?

Edited by AutoIt Smith
Link to comment
Share on other sites

It's not a bug.

The output stream isn't closed until the process decides to (usually when it ends), so your while loop never ends. On the same note, you shouldn't close STDIN after each write, as you'll never get it back.

I'll dig up my old code so you can have a working example.

[font="Tahoma"]"Tougher than the toughies and smarter than the smarties"[/font]

Link to comment
Share on other sites

Nevermind, I had not idea the Console functions were related. Thank you. :whistle:

I would love to see your examples.

Figured it out... Finally....

#include <Constants.au3>
#include <GUIConstants.au3>
#Include <GUIEdit.au3>
Global $StdGUI = GUICreate("GUIExample", 492, 431, 200, 125)
Global $StdSend = GUICtrlCreateButton("Send StdIn", 336, 8, 145, 33)
Global $StdRead = GUICtrlCreateInput("", 8, 16, 313, 21, -1, $WS_EX_CLIENTEDGE)
GUICtrlSetColor($StdRead, 0xC0C0C0)
GUICtrlSetBkColor($StdRead, 0x800000)
Global $StdOut = GUICtrlCreateEdit("", 8, 55, 475, 180, BitOR($ES_READONLY, $WS_VSCROLL), BitOR($WS_EX_CLIENTEDGE, $WS_EX_STATICEDGE))
GUICtrlSetColor($StdOut, 0xC0C0C0)
GUICtrlSetBkColor($StdOut, 0x800000)
GUICtrlSetTip($StdOut, "Reads the Stdout of Command Process.")
Global $StdErr = GUICtrlCreateEdit("", 8, 240, 475, 180, BitOR($WS_BORDER, $WS_VSCROLL), BitOR($WS_EX_CLIENTEDGE, $WS_EX_STATICEDGE))
GUICtrlSetColor($StdErr, 0xC0C0C0)
GUICtrlSetBkColor($StdErr, 0x800000)
GUICtrlSetTip($StdErr, "Reads the Stderr of Command Process.")
Global $CmdPid = Run(@SystemDir & "\cmd.exe", @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD + $STDIN_CHILD)
GUISetBkColor(0x000080, $StdGUI)
GUISetState(@SW_SHOW, $StdGUI)
AdlibEnable("IOCheck")
While 1
    $msg = GUIGetMsg()
    If Not ProcessExists($CmdPid) Then Exit MsgBox(16, "ERROR", "The Command process is gone!")
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
        Case $msg = $StdSend
            StdinWrite($CmdPid, GUICtrlRead($StdRead) & Chr(10))
            GUICtrlSetData($StdRead, "")
    EndSelect
WEnd
ProcessClose($CmdPid)
Exit
Func IOCheck()
    $Bytes = StdoutRead($CmdPid, "", True)
    If $Bytes > 0 Then
        $Buffer = StdoutRead($CmdPid, $Bytes)
        GUICtrlSetData($StdOut, GUICtrlRead($StdOut) & @CRLF & $Buffer)
    EndIf
    $Bytes = StderrRead($CmdPid, "", True)
    If $Bytes > 0 Then
        $Buffer = StderrRead($CmdPid, $Bytes)
        GUICtrlSetData($StdErr, GUICtrlRead($StdErr) & @CRLF & $Buffer)
    EndIf
    _GUICtrlEditLineScroll ($StdOut, Default, _GUICtrlEditGetLineCount ($StdOut))
    _GUICtrlEditLineScroll ($StdErr, Default, _GUICtrlEditGetLineCount ($StdErr))
EndFunc   ;==>IOCheck

Now you can talk to Windows Command....technically dos shell. In a patriotic shell :).

Edited by AutoIt Smith
Link to comment
Share on other sites

A good example script AutoIt Smith. Couple of minor bugs with squares in output, would be incorrect carraige returns perhaps, but overall nice. Would be a good idea to add to Scripts'n'Scraps so it can be shared. :whistle:

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...