Jump to content

IPC via stdinwrite() and consoleread()


Go to solution Solved by Gianni,

Recommended Posts

Hello I am attempting some IPC between two scripts, I have been able to get it to work this way in the past, but it's been a long while since I've coded that script.

Below is what I have so far, what it should do is write the current number to the stdin stream of the child process, and the child process should write to a file any data it receives.

However it seems the child process is not getting the messages.

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Change2CUI=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
if not @Compiled then Exit
If $cmdlineraw = "/IPC" Then
    $buffer = ""
    While 1
        Sleep(10)
        _checkstdin()
    WEnd
EndIf
$IPC = run(@ScriptFullPath&" /IPC", @ScriptDir, @SW_HIDE)
$i = 0
while 1
    StdinWrite($IPC, $i)
    ConsoleWrite($I&@CRLF)
    $i += 1
    sleep(10)
WEnd
func _checkstdin()
;subprocess gets commands and sends to main
$buffer &= ConsoleRead()
if $buffer <> "" Then
FileWrite(@ScriptDir&'\log.txt', $buffer&@CRLF)
EndIf
$buffer = ""
EndFunc

(this must be compiled to work, obviously)

Link to comment
Share on other sites

Does it have to be StdinWrite and ConsoleRead? There are many IPC solutions available. One of the best seems to be Mailslot written by trancexx.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

  • Solution

Hi nullschritt

in your script:

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Change2CUI=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
if not @Compiled then Exit
If $cmdlineraw = "/IPC" Then
    $buffer = ""
    While 1
        Sleep(10)
        _checkstdin()
    WEnd
EndIf
$IPC = run(@ScriptFullPath&" /IPC", @ScriptDir, @SW_HIDE)
$i = 0
while 1
    StdinWrite($IPC, $i)
    ConsoleWrite($I&@CRLF)
    $i += 1
    sleep(10)
WEnd
func _checkstdin()
;subprocess gets commands and sends to main
$buffer &= ConsoleRead()
if $buffer <> "" Then
; FileWrite(@ScriptDir&'\log.txt', $buffer&@CRLF)
    ConsoleWrite($buffer&@CRLF)
EndIf
$buffer = ""
ConsoleWrite(".")
EndFunc

it seems that

if you run the compiled script without the /IPC parameter then it works because it receives the numbers comming from the StdinWrite() Statement in line 24;

but if You run the compiled script with the /IPC parameter then the script just loops around (lines 16 -19) waiting incoming data that nobody is sending;

if you add line 37 you can see that the script is looping in vain

Bye

EDIT:

:huh2:
 Trying the script I saw that running it without the /IPC parameter I see through the numbers 0 to 9 without the dot, while 10 and above the number is preceded by a period. this "dot" should not be there :think:  where that "dot" come from?

EDIT 2:

also .... $STDIN_CHILD + $STDOUT_CHILD must be added to RUN() statement in line 21 ......

I give up ... :)

Edited by Pincopanco

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

Hi nullschritt

in your script:

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Change2CUI=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
if not @Compiled then Exit
If $cmdlineraw = "/IPC" Then
    $buffer = ""
    While 1
        Sleep(10)
        _checkstdin()
    WEnd
EndIf
$IPC = run(@ScriptFullPath&" /IPC", @ScriptDir, @SW_HIDE)
$i = 0
while 1
    StdinWrite($IPC, $i)
    ConsoleWrite($I&@CRLF)
    $i += 1
    sleep(10)
WEnd
func _checkstdin()
;subprocess gets commands and sends to main
$buffer &= ConsoleRead()
if $buffer <> "" Then
; FileWrite(@ScriptDir&'\log.txt', $buffer&@CRLF)
    ConsoleWrite($buffer&@CRLF)
EndIf
$buffer = ""
ConsoleWrite(".")
EndFunc

it seems that

if you run the compiled script without the /IPC parameter then it works because it receives the numbers comming from the StdinWrite() Statement in line 24;

but if You run the compiled script with the /IPC parameter then the script just loops around (lines 16 -19) waiting incoming data that nobody is sending;

if you add line 37 you can see that the script is looping in vain

Bye

EDIT:

:huh2:

 Trying the script I saw that running it without the /IPC parameter I see through the numbers 0 to 9 without the dot, while 10 and above the number is preceded by a period. this "dot" should not be there :think:  where that "dot" come from?

EDIT 2:

also .... $STDIN_CHILD + $STDOUT_CHILD must be added to RUN() statement in line 21 ......

I give up ... :)

It seems adding $STDIN_CHILD to the run command works, so the IPC is now working in one direction, now I've just got see if I can get it to work both ways ($STDIN_CHILD + $STDOUT_CHILD), I haven't tried this in my code yet, but I will report back shortly when I have, although my thoughts abotu it working are doubtful, as it would seem that the above flags are requesting two different handles.

Link to comment
Share on other sites

Fantastic, it works both ways with these flags.

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Change2CUI=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
if not @Compiled then Exit
If $cmdlineraw = "/IPC" Then
    $buffer = ""
    While 1
        Sleep(7)
        _checkstdin()
    WEnd
EndIf
$IPC = run(@ScriptFullPath&" /IPC", @ScriptDir, @SW_HIDE, 0x1 + 0x2)
$i = 0
while 1
    StdinWrite($IPC, $i)
    ConsoleWrite(StdoutRead($IPC))
    $i += 1
    sleep(10)
WEnd
func _checkstdin()
;subprocess gets commands and sends to main
$buffer &= ConsoleRead()
if $buffer <> "" Then
FileWrite(@ScriptDir&'\log.txt', $buffer&@CRLF)
ConsoleWrite($buffer&@CRLF)
EndIf
$buffer = ""
EndFunc

As you can see it sends text to the child, then the child sends it back to the main process, thanks for your help!

Link to comment
Share on other sites

hi nullschritt

now I see better the whole logic,

the compiled script must be run without the /IPC parameter so this script launches another instance of himself, but this time with the /IPC parameter. Now 2 istances are running, so the first instance sends the numbers to the second (in the while-whend lines 23 -28), and the second instance receives the numbers and sends them back to the first by the  _checkstdin() subroutine.

well, I'm glad you've solved by adding
the $STDIN_CHILD + $STDOUT_CHILD flags

Bye

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

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