Jump to content
Sign in to follow this  
bmw74

Interactive console output to GUI

Recommended Posts

bmw74

I am writing a small GUI script to call a CLI (console) command, and as it executes the command, interactively display the console results on the GUI. So far the code that I have works, I am able to issue the right command, and there is some text that is being displayed on the GUI, but when the processing ends, the script gets paused and I cannot continue using the GUI anymore, so I am forced to kill it. I believe the problem is happening when it enters the Create function, I have marked that section with *****HERE*****

What I would like to do, is being able to select the file I am going to use, and when the Create button is clicked, it will issue the command cli.exe -create -full <filename>. On the bottom section of the GUI, all the text that returns from the console should be displayed. The CLI command always returns the console prompt (C:\_) after it runs. The text that returns the CLI command contains multiple lines, including empty lines. If you run the command on the command console, you will see something like this:

C:\cli.exe -create -full "c:\folder1\file1.bak"

Copyright information goes here

More copyright information

Processing file1.bak

File1,bak processed successfully (or Error processing file1.bak)

C:\

I need to be able to run a series of commands one after another, without closing the GUI. When no more commands are needed, then the Close button should close the GUI. Eventually the GUI will be enhanced with more options, but I want to get the Create one working first before moving on.

Here is the code that I have so far... Any guidance or advice will be much appreciated. I also welcome any changes or improvements to the existing code.

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

GLOBAL $var = @WorkingDir
DIM $FileName, $Target, $TargetName, $Check1, $Output

_main()

Func _main()
  $Form1 = GUICreate("CLI Generator", 614, 360)
  $Output = GuiCtrlCreateEdit("", 8, 230, 596, 127, $ES_READONLY, $WS_EX_STATICEDGE )
  $CloseButton = GUICtrlCreateButton("Close", 8, 182, 145, 25, 0)
  $Group1 = GUICtrlCreateGroup(" Create ", 163, 6, 441, 217)
  $Label1 = GUICtrlCreateLabel("Target Location", 179, 102, 160, 17)
  $Target = GUICtrlCreateInput("", 347, 98, 233, 21)
  $Label2 = GUICtrlCreateLabel("Enter desired name", 179, 139, 145, 17)
  $TargetName = GUICtrlCreateInput("", 347, 135, 233, 21)
  $Label3 = GUICtrlCreateLabel("Select file(s)", 179, 71, 100, 17)
  $BrowseButton = GUICtrlCreateButton("&Browse...", 347, 63, 161, 25, 0)
  $CreateButton = GUICtrlCreateButton("&Create", 307, 182, 100, 25, 0)
    
  GUISetState(@SW_SHOW)

  ; This group is to paint the GUI when more sections are added, which will modify the $Group1 area 
  $GroupA = StringSplit($Group1 & "+" & $Check1 & "+" & $Target & "+" & $Label1 & "+" & $Label2 & "+" & $TargetName & "+" & $BrowseButton & "+" & $Label3 & "+" & $CreateButton, "+" )
    
  While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
      Case $GUI_EVENT_CLOSE, $CloseButton
        Exit
      Case $BrowseButton
        $message = "Hold down Ctrl or Shift to choose multiple files."
        $FileName = FileOpenDialog($message, $var & "", "Backups (*.bak) | All files (*.*)", 1 + 4)
        $FileName = StringReplace($FileName, "|", ",")
      Case $CreateButton
        _create()
    EndSwitch
  WEnd
EndFunc

Func _create()
  Local $line, $ConsoleCLI, $PID, $data
  $ConsoleCLI = 'CLI.exe -create -full "' & $FileName 
  GUICtrlSetData ($Output, $ConsoleCLI & @CRLF)
  $PID = Run (@ComSpec & " /k " & $ConsoleCLI, "", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)

  While 1    ;****HERE****
    $data = StdoutRead($PID, 0, 1)
    If $data Then
      While 1
        $line = StdoutRead($PID)
        If @error = -1 Then ExitLoop
        GUICtrlSetData($Output, $line, 1)
      Wend
    EndIf
  WEnd
EndFunc

Func _GuiCtrlGroupSetState(ByRef $a_GroupArray, $i_State)
    For $i = 1 To $a_GroupArray[0]
        GUICtrlSetState($a_GroupArray[$i], $i_State)
    Next
EndFunc

I have looked at the help, samples and forum topics for several days now, so it is time to ask around.

Edited by bmw74

Share this post


Link to post
Share on other sites
Valuater

try this...

Func _create()
  Local $line, $ConsoleCLI, $PID, $data
  $ConsoleCLI = 'CLI.exe -create -full "' & $FileName
  GUICtrlSetData ($Output, $ConsoleCLI & @CRLF)
  $PID = Run (@ComSpec & " /k " & $ConsoleCLI, "", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)

  While 1    ;****HERE****
    $data = StdoutRead($PID, 0, 1)
    If $data Then
      While 1
        $line = StdoutRead($PID)
        If @error = -1 Then Return
        GUICtrlSetData($Output, $line, 1)
      Wend
    EndIf
  WEnd
EndFunc

8)


NEWHeader1.png

Share this post


Link to post
Share on other sites
bmw74

Thanks for your suggestion... I tried it but it still had the problem. I actually figured it out... the problem was the first While 1, it was throwing the whole function into an endless loop. I modeled the function after some samples I gathered from the forums and the help, but after doing some debugging, I noticed that it was not needed, so I got rid of it and now it works as expected.

For anyone who wants to see the change, here is the new function (everything else in the code stayed the same):

Func _create()
  Local $line, $ConsoleCLI, $PID, $data
  $ConsoleCLI = 'CLI.exe -create -full "' & $FileName
  GUICtrlSetData ($Output, $ConsoleCLI & @CRLF)
  $PID = Run (@ComSpec & " /k " & $ConsoleCLI, "", @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)

      While 1
        $line = StdoutRead($PID)
        If @error Then ExitLoop
        GUICtrlSetData($Output, $line, 1)
  WEnd
EndFunc

Share this post


Link to post
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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.