Jump to content

STDOUT in a GUICtrlEdit


Recommended Posts

#include <Constants.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>

Opt("GUIOnEventMode", 1)  ; Change to OnEvent mode

Dim $hWin = GUICreate("Ping",400,200)
Dim $hEdit = GUICtrlCreateEdit("",5,5,390,190);,$GUI_SS_DEFAULT_EDIT - $WS_HSCROLL + $ES_READONLY)

GUISetOnEvent($GUI_EVENT_CLOSE, "_Die")

GUISetState(@SW_SHOW)

_Ping("10.120.9.79")

While True
    Sleep(1000)
WEnd

Func _Ping($Target)
    Local $hWndCmd = Run(@ComSpec & " /c ping " & $Target, @SW_HIDE, $STDIN_CHILD + $STDERR_MERGED)
    Local $sCmdOutput
    While True
        $sCmdOutput &= StdoutRead($hWndCmd)
        If @error Then ExitLoop
        GUICtrlSet($hEdit,$sCmdOutput,1)
    WEnd

    MsgBox(0,Default,$sCmdOutput)
EndFunc

Func _Die()
    GUIDelete($hWin)
    Exit
EndFunc

I want to use this functionality elsewhere, but for now I'm testing with PING. Why won't this work? I just want to run a CLI command and display the output in an edit control.

Thoughts?

 

Link to comment
Share on other sites

First things first, there's no function GUICtrlSet, you're looking for GUICtrlSetData.

Second, you have @SW_HIDE in the "working dir" parameter and the last parameter is in the "show_flag" parameter.

Third, do you want this to update as it's running, or after the ping command has finished, or does it matter?

The code below shows both methods regarding point #3, you can choose one or the other, or both for that matter because the second one will overwrite the first one's entries with the total received text.

#include <Constants.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>

Opt("GUIOnEventMode", 1) ; Change to OnEvent mode

Dim $hWin = GUICreate("Ping", 400, 200)
Dim $hEdit = GUICtrlCreateEdit("", 5, 5, 390, 190);,$GUI_SS_DEFAULT_EDIT - $WS_HSCROLL + $ES_READONLY)

GUISetOnEvent($GUI_EVENT_CLOSE, "_Die")

GUISetState(@SW_SHOW)

_Ping("yahoo.com")

While True
    Sleep(1000)
WEnd

Func _Die()
    GUIDelete($hWin)
    Exit
EndFunc   ;==>_Die

Func _Ping($Target)
    Local $hWndCmd = Run(@ComSpec & " /c ping " & $Target, "", @SW_HIDE, $STDIN_CHILD + $STDERR_MERGED)
    Local $sCmdOutput= "", $LastCmdOutput = ""
    While True
        $sCmdOutput &= StdoutRead($hWndCmd)
        If @error Then ExitLoop
        ; this section will display it as it is being read
        If $sCmdOutput <> $LastCmdOutput Then
            GUICtrlSetData($hEdit, $sCmdOutput)
            $LastCmdOutput = $sCmdOutput
        EndIf
        ; end section
    WEnd
    GUICtrlSetData($hEdit, $sCmdOutput) ; this will display it after the ping command has finished

;~  MsgBox(0, Default, $sCmdOutput)
EndFunc   ;==>_Ping

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

(For the record, I had removed the GUICtrlSetData from the script during testing and typo'd when I typed it back into the forum)

And THANK YOU. I had copied that code from a script I wrote last year that I KNEW was working. Missed a daggum comma.

Link to comment
Share on other sites

Glad it's working for you, punctuation can always cause all sorts of problems. :)

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

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