Sign in to follow this  
Followers 0
SpinningCone

Odd problem with stdinwrite

3 posts in this topic

OK so i'm trying to build a tool part of which adds a GUI to a command line tool.  my tests seemed to work fine but when i ported my function into a GUI they break, the first time i invoke the commandline it returns nothing, then each subsequent time i press the button that calls to command line it ignores the current command and re-sends the last command. if i press the button a second time the command updates.    IE let's say  i have a command line app with a counter as one of the inputs.


Press button to generate output -> "0" returned
Press Button again with same paramaters -> "Hello world 1" returned

Change counter to "2" press button  -> "Hello world 1" returned

press the button again ->  "Hello world 2" returned
 

THis was boggling my mind so i stripped out the GUI and took the function that was being called in the GUI and make it a hotkey call.   now the exact same code that calls the commandline has different behavior



Press hotkey to generate output -> "0" returned

Press hotkey again with same paramaters -> "Hello world 1" returned

Change counter to "2" press hotkey-> "Hello world 2" returned

 

 

So a hotkey act's "normally" bot only after the initial "0" is returned.

 

if I perform the function call for the command line before the hotkey while loop it functions properly the first time. :   This seems like some sort of autoit bug but i have no idea how to work around it. : example code

 

 

 

#include <Constants.au3>
#include <array.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
Opt("GUIOnEventMode", 1)
#Region ### START Koda GUI section ### Form=c:\users\datwater\documents\autoit\projects\otp tool\otp tool.kxf
$Form1 = GUICreate("Form1", 392, 448, 191, 148)
GUISetOnEvent($GUI_EVENT_CLOSE, "Form1Close")
$Label1 = GUICtrlCreateLabel("seed", 23, 57, 35, 17)
$txtSeed = GUICtrlCreateInput("", 66, 55, 213, 21)
$btnRandomSeed = GUICtrlCreateButton("Create Seed", 287, 53, 74, 25)
GUICtrlSetOnEvent(-1, "btnRandomSeedClick")
$txtCounter = GUICtrlCreateInput("5", 72, 96, 71, 21)
$txtLength = GUICtrlCreateInput("6", 223, 96, 48, 21)
$Label2 = GUICtrlCreateLabel("Counter", 24, 99, 41, 17)
$label3 = GUICtrlCreateLabel("Length", 173, 98, 37, 17)
$txtOutput = GUICtrlCreateEdit("", 38, 213, 326, 201)
$btnGen = GUICtrlCreateButton("Run Command", 149, 154, 80, 25)
GUICtrlSetOnEvent(-1, "btnGenClick")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###


local $key      = ""
local $length   = 8
local $count    = 5

Local $cmd = Run("cmd.exe", @ScriptDir, @SW_SHOWNORMAL, $STDIN_CHILD + $STDOUT_CHILD)

While 1
    Sleep(100)
WEnd

Func btnGenOTPClick()
    $length = GUICtrlRead($txtLength)
    $count = GUICtrlRead($txtCounter)
    $Seed  = GUICtrlRead($txtSeed)
    $otp = runCommand($Seed,$length,$count)
    GUICtrlSetData($txtOutput,$otp & @CRLF, "|")

EndFunc

Func btnRandomSeedClick()
    GUICtrlSetData($txtKey,genKey(40))
EndFunc


func runCommand($s,$l,$c)
    ;build the command
    $command = "oathgen.exe -c " & $c & " -l " & $l &" -s " & $s & "& @CRLF
    ConsoleWrite("COMMAND: " & $command & @CRLF) ; debug check
    ;send command to the console
    StdinWrite($cmd, $command)
    $line = ""
    ;loops and gathers all the command output
    While True
        $line &=  StdoutRead($cmd)
        ConsoleWrite($line)  ;debug
        If @error Then ExitLoop
        ;to exit the while loop we look for the blank command prompt, the trimming is needed to get a clean match.
        If StringRight($line,StringLen(@ScriptDir) + 1) = @ScriptDir & ">" Then ExitLoop
        Sleep(30)
    WEnd
        
        ;i do some parsing here of the output then return it , that all works fine
    

EndFunc



Basically where i'm calling StdinWrite($cmd, $command) i can see that $command is what i want it to be,  but the first time it runs nothing is sent and the second time you press the button the command is correct but it actually sends the *old* command to standard in . This is only when running in a GUI though. if you lop out the runCommand function and run it just in a script it works as expected

Share this post


Link to post
Share on other sites



To better understand the issue i started from scratch and made 3 different scripts to illustrate the problem
 (see attached)

cmdBug-Script.au3 works as intended.  cmdBug-Hotkey.au3 and cmdBug-form.au3 exhibit the oddity with sending old commands.

 

 

cmdBug-hotKey.au3

cmdBug-form.au3

cmdBug-Script.au3

Share this post


Link to post
Share on other sites

nm, figured out my problem.

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  
Followers 0

  • Similar Content

    • Skeletor
      By Skeletor
      MS Splash Screens
      The office 2016/17 office suite has made many people turn heads with its functions and especially their splash screen.
      I now present to you, the MS Style Splash Screen. For Rookies, Novices and anyone who wants a ready made splash screen for your gui application.

      You can now download it and also try it out yourself.
      You have three colors to choose from:
      Blue
      Red
      Green
      All three colours are from the famous programs.
      I also included the KODA form to you can have freedom with this splash screen.
      Note: the KODA form does not have the three colours as well as the correct blue. 

      Have fun and enjoy.
      Note:
      I will update the splash to add the automated 3 dots animation at a later stage. 
       
       
    • Skeletor
      By Skeletor
      A gui splash screen. Themed after the famous MS Office Suite.
      Three colours to choose from, Red, Blue and Green.
      You can change the labels to your own application.

      Also bundled with this is the KODA form. So you can open up and see the basic structure of this splash screen.
       
      Splash Screen GUI.zip
    • BlazerV60
      By BlazerV60
      Hello all,
      I've been trying to figure out how to launch Google Chrome in the background (hidden) but it doesn't seem possible. I've tried the following methods:
      ShellExecute("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe", "", "", "", @SW_HIDE) Also I've tried:
      ShellExecute(@ComSpec, "/c start chrome.exe","","",@SW_HIDE) Lastly I tried:
      RunWait('"'&@ProgramFilesDir&'\Google\Chrome\Application\chrome.exe" --silent-launch',@ScriptDir)  
      But all of them launch my chrome browser without hiding it. Does anyone know a workaround for this or if AutoIT just can't Chrome? 
      Bonus points if you know how to make it launch chrome hidden and make it go to https://www.autoitscript.com
       
      Thank you,
      Brian
    • hcI
      By hcI
      Hello I would like to know if there is a way to return a sentence in cmd when I launch from it (because I add arguments).
      For example, diskpart.exe which help to manage the key and hdd connected, when you launch it with the parameter "/f" the app return a sentence saying that it don't recognize the parameter "/f" and it return the sentence in the cmd where i started the application, not a new one.
      That's what I want to do but I couldn't find anything that would solve my problem on internet and on AutoIt like ConsoleWrite / ConsoleWriteError (don't work).
       
      Thanks
    • SteveJM
      By SteveJM
      I have a working program currently driven largely through menu selections. I would like to add a toolbar where most of the tollbar button actions are basically the same as menu items, but quicker to access. I have been wading around in toolbar examples, MSDN pages etc, it's clearly going to be a bit of a slog to get everything right, including tooltips etc. I thought I would start with something simple to prove the principle.
      Using bits from the help file examples I have a small program that successfully displays a toolbar. However, what seemed like the most elegant way to deal with the button commands does not seem to work. My understanding was that a toolbar button fires a WM_COMMAND message, with the command Id set by the second parameter in the call to
      _GUICtrlToolbar_AddButton ( $hWnd, $iID, $iImage)
      so I though it would be a good idea to set this Id to the same value as my menu item Id; then it would run the same task which is what I wanted. This did not work. I am using message loop mode and would like to stick with this because some of my scripts run hardware at the same time as the gui; it is easier if I don't have to worry about code being interrupted with the hardware in an unknown state . So I added a handler for WM_COMMAND, with some cribbed display code to try and see why. The toolbar button defintely fired a WM_COMMAND message and the Id looked the same, so no explanation there.
      I guess the issue is with GUIGetMsg() which may be constructed to ignore all but a limited number of control handles, i.e. those made with the GuiCtrlCreate... commands; this is speculation.
      I would dearly love to find a tidy way to get around this. Having some controls handled in the message loop and some in a WM_COMMAND handler, performing the same task, feels ugly. I would be very grateful for further insight from someone experienced with handling a toolbar. Perhaps I should be trying to fire the menu item. I have attached a code snippet to try and illustrate the issue.
       
       
       
      ToolbarTrial.au3