Jump to content

[Solved] Unable to read stream when Run opt flag is BitOR($STDIN_CHILD, $STDOUT_CHILD)


Recommended Posts

So I am trying to make a GUI for a console application. I need to be able to both read and write data to and from the console application's stream. Problem is, if I try to set the Run opt flag as BitOR($STDIN_CHILD, $STDOUT_CHILD), then I can't read the stream but I can write. I've also tried running as admin, it doesn't seem to help. There is no error reported, it just continues silently. I think this is a bug?

#include <Array.au3>
#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
Global $iPID_NetCat = Null
DirCreate(@TempDir & "\BetaLeaf Software\NetCat Tool\")
If @OSArch = "X64" Then
    Global $sEXE_FileName = "nc64.exe"
    FileInstall("nc64.exe", @TempDir & "\BetaLeaf Software\NetCat Tool\nc64.exe")
Else
    Global $sEXE_FileName = "nc.exe"
    FileInstall("nc.exe", @TempDir & "\BetaLeaf Software\NetCat Tool\nc.exe")
EndIf
#Region ### START Koda GUI section ### Form=
$Form = GUICreate("NetCat Tool", 364, 254, 192, 124)
$Label_SwitchConnectionSettings = GUICtrlCreateGroup("NetCat Connection Settings", 4, 0, 185, 100, BitOR($GUI_SS_DEFAULT_GROUP, $BS_CENTER))
$label_IPAddress = GUICtrlCreateLabel("IP Address", 8, 21, 55, 17, $SS_CENTER)
$input_IPAddress = GUICtrlCreateInput("192.168.86.25", 80, 19, 100, 21, BitOR($GUI_SS_DEFAULT_INPUT, $ES_CENTER))
GUICtrlSetLimit($input_IPAddress, 15)
GUICtrlSetTip($input_IPAddress, "Example: 192.168.0.21")
$label_Port = GUICtrlCreateLabel("Port", 8, 48, 23, 17, $SS_CENTER)
$input_Port = GUICtrlCreateInput("", 80, 45, 100, 21, BitOR($GUI_SS_DEFAULT_INPUT, $ES_CENTER))
GUICtrlSetLimit($input_Port, 5)
GUICtrlSetTip($input_Port, "A port between 1-65535")
$Label_ConnectionStatus = GUICtrlCreateLabel("Connection Status: Not Connected", 12, 73, 169, 17)
GUICtrlSetColor($Label_ConnectionStatus, 0xFF0000)
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
AdlibRegister("_AutoConnect", 250)
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

Func _AutoConnect()
    If $iPID_NetCat = Null Then
        ConsoleWrite("Starting..." & @CRLF)
        $iPID_NetCat = Run($sEXE_FileName & " " & GUICtrlRead($input_IPAddress) & " " & GUICtrlRead($input_Port), @TempDir & "\BetaLeaf Software\NetCat Tool\", @SW_HIDE, BitOR($STDIN_CHILD, $STDOUT_CHILD))
        GUICtrlSetData($Label_ConnectionStatus, "Connection Status: Connecting...")
        GUICtrlSetColor($Label_ConnectionStatus, 0x0000ff)
        $timer = TimerInit()
        ConsoleWrite("Connecting..." & @CRLF)
        Do
            Sleep(1000)
            Local $vData = StdoutRead($iPID_NetCat)
            If @error Then ConsoleWrite("[ERROR]" & @error & "," & @extended & @CRLF)
            If @extended Then ConsoleWrite("[INFO]" & @extended & @CRLF)
            ConsoleWrite("[UPDATE]" & @CRLF & $vData & @CRLF)
            If StringInStr($vData, "Welcome to netcat!") Then ExitLoop
        Until TimerDiff($timer) > 16000
        If ProcessExists($sEXE_FileName) Then
            GUICtrlSetData($Label_ConnectionStatus, "Connection Status: Connected!")
            GUICtrlSetColor($Label_ConnectionStatus, 0x00ff00)
        EndIf
    Else
        If Not ProcessExists($sEXE_FileName) Then
            ConsoleWrite("Lost connection!" & @CRLF)
            $iPID_NetCat = Null
            GUICtrlSetData($Label_ConnectionStatus, "Connection Status: Not Connected")
            GUICtrlSetColor($Label_ConnectionStatus, 0xff0000)
        EndIf
    EndIf
EndFunc   ;==>_AutoConnect
Func _Parse()
    Sleep(250)
    Local $vData = StdoutRead($iPID_NetCat)
    ConsoleWrite("[UPDATE]" & @CRLF & $vData & @CRLF)
EndFunc   ;==>_Parse

 

Edited by BetaLeaf

 

 

Link to comment
Share on other sites

Might be because NetCat Tool doesn't write to the stream.  It is not that impossible.  Some programs that are console type, doesn't write on stream.

To test your program use cmd.exe, instead of the tool.  If that works and it doesn't with NetCat, you got your answer.

Edited by Nine
Link to comment
Share on other sites

Since OP says that when using streams individually they work regularly, I suppose the problem is in the general logic of the script rather than in nc.exe, and even less is a bug in the redirection of the RUN command.
Where can I download sc.exe that you use? is this perhaps ?? (https://eternallybored.org/misc/netcat/) ??

Edited by Chimp

 

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

12 hours ago, Nine said:

Might be because NetCat Tool doesn't write to the stream.  It is not that impossible.  Some programs that are console type, doesn't write on stream.

To test your program use cmd.exe, instead of the tool.  If that works and it doesn't with NetCat, you got your answer.

I tried the following and it doesn't seem to help: 

$iPID_NetCat = Run(@ComSpec & " /c " & $sEXE_FileName & " " & GUICtrlRead($input_IPAddress) & " " & GUICtrlRead($input_Port), @TempDir & "\BetaLeaf Software\NetCat Tool\", @SW_HIDE, BitOR($STDIN_CHILD, $STDOUT_CHILD))

And to be crystal clear, I'm not able to read the stream if both $STDIN_CHILD and either $STDOUT_CHILD, $STDERR_CHILD, or $STDERR_MERGED are used. If only one is used, then everything works as intended. Basically, if $STDIN_CHILD is used in addition to another opt flag, then $STDIN_CHILD doesn't work. It's like it can only do either a read or write stream at one time and it prioritizes the $STDOUT_CHILD stream

 

 

Link to comment
Share on other sites

With the Run command, don't use BitOr, just use + between the variable names, you need to add them together instead of Oring them.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

20 minutes ago, BrewManNH said:

With the Run command, don't use BitOr, just use + between the variable names, you need to add them together instead of Oring them.

The opt_flag values of the RUN command can be OR'ed without any issues because they all fall on bit boundaries.  i.e. 1, 2, 4, 8, 16, and 65536

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