littlebigman Posted August 7, 2018 Posted August 7, 2018 Hello, To make it easier for newbies, I'd like to find a way to write a GUI application that would run as a proxy for a command-line Windows application (a.k.a. DOS application as they were called in the 20th century.) Before I investigate further, can AutoIT easily interact with a CLI application, especially to parse the output returned by the CLI application through eg. regex, or should I look at another solution? Thank you.
orbs Posted August 7, 2018 Posted August 7, 2018 the answer is yes. your next steps are: 1) learn how to work with GUI in AutoIt - for example, read the help for the function GUICreate() and run the example scripts there. 2) learn how to launch an external file - the function Run() can get you started. 3) learn how to read the output stream using the function StdoutRead(). start with the above, let us know once you have some code (even if not working) and you need further assistance. Signature - my forum contributions: Spoiler UDF: LFN - support for long file names (over 260 characters) InputImpose - impose valid characters in an input control TimeConvert - convert UTC to/from local time and/or reformat the string representation AMF - accept multiple files from Windows Explorer context menu DateDuration - literal description of the difference between given dates WinPose - simultaneous fluent move and resize Apps: Touch - set the "modified" timestamp of a file to current time Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes SPDiff - Single-Pane Text Diff Magic Math - a math puzzle Demos: Title Bar Menu - click the window title to pop-up a menu
littlebigman Posted August 8, 2018 Author Posted August 8, 2018 Thanks for the infos. I'm stuck at why this code 1) doesn't display the DOS box and 2) displays an empty msgbox: Quote #include <AutoItConstants.au3> #include <MsgBoxConstants.au3> #include <Array.au3> ; Required for _ArrayDisplay only. ;Local $iPID = Run(@ComSpec & ' /K C:\MYAPP.EXE', @SW_HIDE, $STDOUT_CHILD) ;Local $iPID = Run(@ComSpec & ' /K C:\MYAPP.EXE', @SW_HIDE) Local $iPID = Run(@ComSpec & " /K C:\MYAPP.EXE", @SW_MAXIMIZE,$STDOUT_CHILD) ; Wait until the process has closed using the PID returned by Run. ProcessWaitClose($iPID) ; Read the Stdout stream of the PID returned by Run. This can also be done in a while loop. Look at the example for StderrRead. ;Local $sOutput = StdoutRead($iPID) Local $sOutput = StderrRead($iPID) MsgBox($MB_SYSTEMMODAL, $sOutput, "") None of those sources of infos* seems to present how to work with CLI applications. Is there a quick article somewhere that explains how to interact with a DOS application, and how all those commands work together? Run() RunAs RunAsWait RunWait ConsoleRead ProcessClose ShellExecute ShellExecuteWait StderrRead StdinWrite StdioClose StdoutRead Thank you. * www.autoitscript.com/forum/files/file/351-learn-to-program-using-free-tools-with-autoit/ www.autoitscript.com/autoit3/docs/ Learning to Script with AutoIt V3 (Last Updated 17 Feb 2010).zip
LisHawj Posted August 8, 2018 Posted August 8, 2018 (edited) You are missing the "workingdir" parameter. You are also reading from the STDerr_Child stream but didn't specify it in your code either. For my example, I added #requireadmin because the BitLocker example I used required admin privilege to run. You should now be able to catch both STDout and STDerr stream. See my example below. #RequireAdmin #include <AutoItConstants.au3> #include <MsgBoxConstants.au3> #include <Array.au3> ; Required for _ArrayDisplay only. _WinAPI_Wow64EnableWow64FsRedirection(False) #include <WinAPIFiles.au3> ;Local $iPID = Run(@ComSpec & ' /K C:\MYAPP.EXE', @SW_HIDE, $STDOUT_CHILD) ;Local $iPID = Run(@ComSpec & ' /K C:\MYAPP.EXE', @SW_HIDE) $iPID = Run(@ComSpec & " /K " & "C:\Windows\System32\manage-bde.exe -status C:", "", @SW_MAXIMIZE, $STDERR_CHILD + $STDOUT_CHILD) ; Wait until the process has closed using the PID returned by Run. ProcessWaitClose($iPID) ; Read the Stdout stream of the PID returned by Run. This can also be done in a while loop. Look at the example for StderrRead. Local $sOutput = StdoutRead($iPID) ;Local $sOutput = StderrRead($iPID) MsgBox($MB_SYSTEMMODAL, $sOutput, "") Edited August 8, 2018 by LisHawj word correction.
littlebigman Posted August 9, 2018 Author Posted August 9, 2018 Thanks much. Problem solved. Is there a tutorial/article somewhere specifically about how to interact with a CLI? The reference manual is nice when you need to know specifically about a given command, but it doesn't cover how to use the different commands together, eg. why use Run() instead of ShellExecute(), how to check if the command ended OK, etc.
TheSaint Posted August 9, 2018 Posted August 9, 2018 Generally you read the return of StdoutRead in a loop until all data has been returned. The Help file has examples of this. With Run, you are specifying a specific program, which you provide the path to. With ShellExecute, you are allowing Windows to decide what program to run based on the extension of the file being passed to it. You might also be interested in $CMDline. An AutoIt script can also be compiled as a Console (DOS) one ... if that helps. When testing for errors, it is ok to use - @ComSpec & " /K " But ideally, to allow the DOS window to close automatically, you should change the K to a C when using the finished script - @ComSpec & " /C " Make sure brain is in gear before opening mouth! Remember, what is not said, can be just as important as what is said. Spoiler What is the Secret Key? Life is like a Donut If I put effort into communication, I expect you to read properly & fully, or just not comment. Ignoring those who try to divert conversation with irrelevancies. If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it. I'm only big and bad, to those who have an over-active imagination. I may have the Artistic Liesense to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)
TurionAltec Posted August 10, 2018 Posted August 10, 2018 (edited) Lots of good stuff here. Run + StdoutRead / SterrRead is useful to read in the output from a CLI program. If you're expecting the program to keep outputting data, you can start it and periodically run StdoutRead without having to use Processwaitclose. Shellexecute can't do anything with StdoutRead / SterrRead, which is why it shouldn't be used. If you do want to compile your script as a console application, ConsoleWrite will let you send output (and it will show in the console in the bottom of Scite when not compiled. I use it a lot for debugging vs. Msgbox.). However if you compile your script as a console Application, ConsoleRead won't work as expected to capture typed input. $CmdLine is great if you want to pass your program commandline parameters. It is easy to read the output of CLI programs, but significantly more involved if you want to send interactive input to one (other than commandline parameters). Also, and I have no idea why @comspec keeps being included everywhere, but @comspec does nothing but take unnecessary resources if you are not trying to call commands internal to the cmd.exe interpreter (dir, copy, type, etc). Any other CLI application you can, and should, call without "@comspec /c". This includes a lot of built in Windows commands like sc.exe, net.exe, ipconfig.exe. Edited August 10, 2018 by TurionAltec
orbs Posted August 11, 2018 Posted August 11, 2018 (edited) @littlebigman, AutoIt offers resources galore for learning, practicing and developing your expertise. apart from the excellent help file (which includes concepts and tutorials, as well as extensive help for each and every function, with working examples), there is the wiki, and of course this forum. naturally, when an abundance is available, it is not necessarily structures in the way *you* would find best. until you get familiar with the available resources, i advise you follow the following route when taking on a scripting challenge: if you have absolutely no idea where to begin - google. for example, google "autoit interact with external cli program" would give you several useful links, the first being a reference to the help for the Run() command - which gives you exactly what you need, including reference for using the stdout stream. read the remarks section carefully! from there, follow the hyperlinks to the StdoutRead(), StdinWrite(), etc. and read their help file entries carefully. pretty soon you'll get the hang of it. b.t.w the help for StdoutRead() does show you how to Run() your child process, that's for your "but it doesn't cover how to use the different commands together" remark. and for your "how to check if the command ended OK" question, the same function example script also shows you several methods for error handling, by checking the return value of the function or the @error macro value. and, most important - try for yourself. practice, write code. if your code doesn't work, post it to this forum - you'll find that the people around here are very helpful, particularly when you show some effort on your part. good luck! Edited August 11, 2018 by orbs Signature - my forum contributions: Spoiler UDF: LFN - support for long file names (over 260 characters) InputImpose - impose valid characters in an input control TimeConvert - convert UTC to/from local time and/or reformat the string representation AMF - accept multiple files from Windows Explorer context menu DateDuration - literal description of the difference between given dates WinPose - simultaneous fluent move and resize Apps: Touch - set the "modified" timestamp of a file to current time Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes SPDiff - Single-Pane Text Diff Magic Math - a math puzzle Demos: Title Bar Menu - click the window title to pop-up a menu
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now