Jump to content

Stdinwrite Consoleread Comunication


Recommended Posts

EDIT: Posted a working sample in scripts & scraps.

Thanks.

I found a thread where StdinWrite and consoleRead are mentioned as a methods of communication between processes and sub processes.

You don't need to use TCP connections, you can use the STD handles to communicate. See the Console* functions and Std* functions in the beta help file.

The gist of what you do is Run() the child script with the replace std handle options set (STDIN, STDOUT). The parent script can send data to the child over the child's STDIN stream. The parent script uses StdinWrite() to send the data and the child uses ConsoleRead() to read it. The child can use ConsoleWrite() to send data to the STDOUT stream which can be read from the parent with StdoutRead().

Does someone have a working sample of this?

I'm also curious about jpm's where he sort of implies that this technique is not possible if you reuse the interpreter.

Just a slight comment if you don't use the communication as mention by Valik or ChrisL.

If main script is compiled you don't need to have AutoIt install just run (@AutoItExe & " /AutoIt3ExecuteScript yourgeneratedscriptfile") it will reuse the AutoIt executable which allow your main script to run. :)

I have created two simple "chat" applications to see if I can make this woork.

; Script generated by GUIBuilder 9.4

#include <GuiConstants.au3>

Dim $gChildPid
Dim $gParent = 0
Dim $data
Dim $GUI_Handle = GuiCreate("chatBase", 426, 251,(@DesktopWidth-426)/2, (@DesktopHeight-251)/2)

Dim $Input_2 = GuiCtrlCreateInput("Input1", 10, 10, 350, 20)
Dim $Edit_3 = GuiCtrlCreateEdit("Edit2", 10, 40, 350, 180)
DIm $label1 = GUICtrlCreateLabel("INIT: ", 10,222, 350, 20)
Dim $buttonRunChild = GuiCtrlCreateButton("Run Child", 370, 50, 50, 30)
Dim $buttonSend = GuiCtrlCreateButton("Send", 370, 10, 50, 30)

GuiSetState()
While 1
        $msg = GuiGetMsg()
    Switch $msg
        case $GUI_EVENT_CLOSE
            ExitLoop
        case $buttonSend
            buttonSend_Event()
        case $buttonRunChild
            buttonRunChild_Event()
        Case Else
        ;;;
    EndSwitch
    If $gParent Then 
        $data = StdoutRead($gChildPid)
    Else
        $data = ConsoleRead()
; Expect @error = -1; EOF or not set.
    EndIf
    if $data <> "" Then 
        GUICtrlSetData($Edit_3, (GUICtrlRead($Edit_3) & @CRLF &  $data))
    Endif
    If @error <> 0 then GUICtrlSetData($label1,@MIN & ":" & @SEC & _
        " ERROR: Read: @error:=" & @error &  "@ScriptLineNumber:=" & @ScriptLineNumber)
WEnd

Func buttonRunChild_Event()
    $gParent = 1
    $gChildPid = run(@AutoItExe)
      ;$gChildPid = run(@AutoItExe & " /AutoIt3ExecuteScript chatBase.au3")
EndFunc
Func buttonSend_Event()
    If $gParent Then 
        StdinWrite($gChildPid, GUICtrlRead($Input_2))
    Else
        ConsoleWrite(GUICtrlRead($Input_2))
    EndIf
    If @error <> 0 then GUICtrlSetData($label1, @MIN & ":" & @SEC & _
        " ERROR: Write: @error:=" & @error &  "@ScriptLineNumber:=" & @ScriptLineNumber)
EndFunc

I might be to tiered to get this right at the moment. But if anyone could modify it to work or cast some light on what to do to make such communication it would be appreciated

Edited by Uten
Link to comment
Share on other sites

Here's an example of communication between parent and child processeses.

Since I'm too lazy to create a GUI, please use the Console*() functions provided in my signature.

NOTE: ONLY WORKS WHEN THE SCRIPT IS COMPILED!!! (A side effect of using Mx63.dll)

;;;;parent.au3
#include <Misc.au3>
#include "au3console.au3"
PluginOpen("Mx63.dll")
ConsoleLoad("Parent_0")
$child = Run("child.exe", "", @SW_SHOW, 3)
while 1
   $data = StdoutRead($child, -1, True)
   If StringLen($data) <> 0 Then
      ConsolePrintLine("CHILD>>>"&StdoutRead($child))
   EndIf
   If WinActive("Parent_0") And _IsPressed("0D") Then
      $data = ConsoleInputLine()
      ConsolePrintLine("PARENT>>>"&$data)
      StdinWrite($child, $data)
   EndIf
wend
ConsoleFree()

;;;;child.au3
#include <Misc.au3>
#include "au3console.au3"
PluginOpen("Mx63.dll")
ConsoleLoad("Child_0")
while 1
   $data = ConsoleRead(-1, True)
   If StringLen($data) <> 0 Then
      ConsolePrintLine("PARENT>>>"&ConsoleRead())
   EndIf
   If WinActive("Child_0") And _IsPressed("0D") Then
      $data = ConsoleInputLine()
      ConsolePrintLine("CHILD>>>"&$data)
      ConsoleWrite($data)
   EndIf
wend
ConsoleFree()

NOT TESTED!!

#)

Edited by nfwu
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...