Sign in to follow this  
Followers 0

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")
#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

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


Func btnRandomSeedClick()

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
        ;i do some parsing here of the output then return it , that all works fine


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.






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

    • computergroove
      By computergroove
      I've made a form in Koda where I make a list of 3 user definable elements that I want to be able to replicate with a button press so a user can add data that can be used as part of the program. 
      #include <ButtonConstants.au3> #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> #Region ### START Koda GUI section ### Form=D:\Carbonite Important\0 - Programing\Scite and Autoit\Koda\Forms\Mileage Estimator.kxf $Form1 = GUICreate("Save Project", 614, 1126, 298, 141) $Location = GUICtrlCreateInput("Location", 40, 112, 345, 21) $Distance = GUICtrlCreateInput("Distance", 400, 112, 89, 21) $AddLocation = GUICtrlCreateButton("AddLocation", 288, 80, 97, 25) $Label1 = GUICtrlCreateLabel("Location", 40, 88, 45, 17) $Label2 = GUICtrlCreateLabel("Distance", 400, 88, 46, 17) $HomeBase = GUICtrlCreateInput("HomeBase", 312, 32, 121, 21) $LeapYear = GUICtrlCreateCheckbox("LeapYear", 464, 32, 17, 17) $Label3 = GUICtrlCreateLabel("Leap Year?", 488, 32, 59, 17) $Variance = GUICtrlCreateInput("Variance", 504, 112, 81, 21) $Label4 = GUICtrlCreateLabel("Variance", 504, 88, 46, 17) $Label5 = GUICtrlCreateLabel("Home Base", 312, 8, 59, 17) $Button1 = GUICtrlCreateButton("Button1", 144, 56, 1, 57) $Save = GUICtrlCreateButton("Save Project", 40, 16, 121, 25) $Load = GUICtrlCreateButton("Load Project", 176, 16, 121, 25) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Form1 Case $AddLocation EndSwitch WEnd I want to press the "add location" button and have the fields {Location, Distance and variance} create a new blank record located under the existing 3 input boxes. Since the fields can number over 30 locations in some examples I am thinking about having these items in a scrolling window. What is the best way to do this and can auto it add a variable while running by pressing a button?
    • XaelloNegative
      By XaelloNegative
      I made a small blunder with my code. I created the GUI under 1920x1080 resolution however, my user is configured to ONLY 1080x720 and cannot go any further than that.
      What do you guys think is the best way to correct this one? Resolution mathematics is something that came to mind however I do not have access to his PC to check  from time to time if  my estimation is correct and the resolution on my PC doesn't go down to 1080x720.
      Please help.
    • Simpel
      By Simpel
      Thanks to this post ( I can now write a helpfile to the cmd when for instance passing parameter -h or -help at cmd. But then cmd is blocked by the script (I had to free it with CTRL+BREAK):
      #include <WinAPI.au3> _WinAPI_AttachConsole() $hConsole = _WinAPI_GetStdHandle(1) _WinAPI_WriteConsole($hConsole, "Print helpfile................" & @CRLF) Inside MSDN Library then I found this sentence: A process can use the FreeConsole function to detach itself from its console.
      But I don't find something like _WinAPI_FreeConsole(). How can I do it in another way?
      Regards, Conrad
    • TheAutomator
      By TheAutomator
      A fullscreen console with custom commands!

      Hi everyone!
      This funny project started as a question in the help section:
      I'd like to share this script with everyone that is interested. 
      Why would I want it?
      You like the old style fullscreen console (like in the old day's), You can add custom commands, You can customize the font a lot more compared to cmd.exe, You can share ideas or add tweaks to the script.  
      Still to do:
      Write a simple custom programming language to implement this tool. Writing a little help file / pdf to describe my little programming language. Add little sound effects like a beep if there is a syntax error (optional). Clean up and modify Console.Au3 content. Add an option to have to type a login password (optional). Make an optional installer that also gives scripts for this tool a custom icon and open with command. ...Call Neo?  
      Thanks to:
      xxaviarxx: debugging, some ideas. jguinch: debugging, adding a bunch of tweaks and ideas. kylomas: debugging, new ideas.  
      Edits and updates:
      Added usage of tab key in edit control Edit has focus now on startup I'm currently rewriting a simple custom programming language to implement this tool.  
      UDF download: Console.au3
    • davidacrozier
      By davidacrozier
      Hello all ~
      I am running an autoit script on Windows 10 inside VMware Workstation 12 Pro version 12.5.2.  Technically I am remoting into ESXi which has a Domain Controller (DC), WebServer, FilServer, Windows 10, etc.  Using the GUI (i.e. running explorer.exe) I am able to open several different folders successfully.  The desktop, documents, USB external all open without issue.  The network share opening gives me issues.  Whenever I attempt to open \\filserver\users\user\sharedfolder I get the documents folder instead.  I understand that the documents folder is the default for explorer.  I have also attempted to use the letter drive mapped to the network share (Z:) and receive the same result.  When I run this script on Windows 10 alone without  the VM or the ESXi I am able to open the network share without problems.  I have tried to use the net use command to designate a letter M: to the network share folder prior to running the script.  This did not work for me.  
      One additional avenue I think might work is to use the systreeview321 and _GUICtrlTreeView_FindItem to step through the tree looking for the network share.  Once found,  double click on it and see if that opens the shared network folder.  I can click inside the VM with my mouse on the network share and it opens just fine.  Not sure if running up against GUI issues, or permission issues, or what?
      Thanks in advance,
      Davida Crozier
      This script is a subset of a much larger program, but it illustrates what I am dealing with.