vb.net, trying to use cmd.exe in the background !! - Page 1
Forum section: electronics / projects

author: GlennSprigg
author_rank: Super Contributor
timestamp: November 18, 2022, 11:48:54 am
content: I'm trying to utilize a 'GUI' FrontEnd, to invisibly pass control to a Command Prompt, and get theresultsback to Windows.I've researched it tirelessly, and foundsomesolutions! And at times theyseem to work!I've used the likes of......Which basically seems to be what I need.Now if I substitute the "ApplicationName.exe" above, with "cmd.exe", and use asimple example"arguments" as "/c path"(The '/c' before path, is an expected switch for 'cmd.exe), as in the following image......actually works, as expected!, by displaying all the path statements, now outputed to a multi-line Text-Box within my App.And here's the actual Text-Box result...All well and good!, however....The 'arguments' I wish to pass, is to a custom command-line prog, which can, (though may not) have it's own parameters??I'm unsure of the syntax (for paths etc) to the actual custom prog to be run, as well as the following arguments (like!) forexample'/loadfile or /verbose etc' that itmayoptionally require/utilize ??Mind you, I've sort of learnt the basics about how the ".useShellExecute = False" isnecessaryin my situation,and that this affects certain 'path' instructions!Basically, I want to use the command-line from with VB to run an 'App', pass parameters to it, and get the result back in Windows!!(I'm really trying, and almost there!!)

author: matb
author_rank: Regular Contributor
timestamp: November 18, 2022, 12:26:25 pm
content: From my understanding you are trying to do a "fork" (from C) in C# ?https://stackoverflow.com/a/17736452/14137672could this help you ?Regarding which parameters to pass you will have to try your app from cmd and determine the minimum amount of parameters to give it.

author: GlennSprigg
author_rank: Super Contributor
timestamp: November 18, 2022, 01:04:57 pm
content: I'm actually using VB.NET, not C, or C#, but the syntax is basically the same these days!Yes, I'm aware of the parameters to actually use, (or not), for this app, and at a 'cmd' prompt, I can use the progexactly as it's intended, and it all works. My problem is in implementing such paths, commands & parameters withinthe example I gave above, to utilize the correct syntax for the parsing instructions??(I will check through your 'link' tomorrow though. Thanks!)

author: JohanH
author_rank: Frequent Contributor
timestamp: November 18, 2022, 02:49:21 pm
content: I used this a lot in the past in installation programs. Never found an easy way to run cmd.exe silently. Until I started using wix toolset instead. They have a silent custom action, possibly this could be reused (it's open source anyway).But it's not always a good idea to call cmd.exe (e.g. suspicious processes might be stopped by antivirus in rare cases). I think you should be able to get most variables and such through the windows API without calling cmd.exe. In this case from the Environment or ExpandEnvironmentStrings objects. But be aware that you might get different results in system and user scope.

author: PA0PBZ
author_rank: Super Contributor
timestamp: November 18, 2022, 03:01:37 pm
content: I probably don't understand your question but replacing the relevant line withCode:Dim oStartInfo As New ProcessStartInfo("net.exe", " help")works fine and shows the output of "net help" in the textbox.

author: GlennSprigg
author_rank: Super Contributor
timestamp: November 21, 2022, 09:55:24 am
content: Quote from: PA0PBZ on November 18, 2022, 03:01:37 pmThanksPAOPBZ... Yes, all those 'normal' Commands & Arguments, including your example above, seem towork fine, but if you read on a few responses here, I'll try to explain it better...

author: PA0PBZ
author_rank: Super Contributor
timestamp: November 21, 2022, 10:19:58 am
content: I did read the responses but that didn't really help me to understand the problem (better). As far as I understand you are trying to run a custom command line program with optional parameters. Since net.exe is also a command line program that can take parameters I used that to test your solution and as I hoped that worked fine.So what is the difference between your custom program and other command line programs? I guess sharing this custom program is out of the question? Maybe you can explain what you are trying to do, share the oStartInfo line and the result you get? Also note that programs use other ways to display information apart from the standard output, if you call net.exe without parameters it also gives you a brief message but this is not sent to the standard output but to the error output (I think - it is visible in the debug window).

author: GlennSprigg
author_rank: Super Contributor
timestamp: November 21, 2022, 10:22:23 am
content: Quote from: JohanH on November 18, 2022, 02:49:21 pmThank youJohanH... Firstly, (not related to MY prob), you said "Never found an easy way to run cmd.exe silently."... That one iseasy, so I found out.You simply declare within your Process options, (justlikewithe the ".UseShellExecute = False", something like...Code:<YourProcess>.CreateNoWindow = True, and you won't see the black command prompt window flash before your eyes!!!And when you said... "it's not always a good idea to call cmd.exe", i understand what you say, but in this case it was/is necessary.The 'App'/'Program' Im trying to run, is 'Java' software!, and requires going to the Command-Prompt in order to execute it/instructions.And tomatlab... Thank you, but the link was not helping. Please (ALL) see my next Comment, to have it explained better!...

author: GlennSprigg
author_rank: Super Contributor
timestamp: November 21, 2022, 10:25:17 am
content: (Sorry,PAOPBZ, I've not finished my said response!! hahaha...)

author: PA0PBZ
author_rank: Super Contributor
timestamp: November 21, 2022, 10:26:57 am
content: Quote from: GlennSprigg on November 21, 2022, 10:25:17 amNo problemHere's a picture of what I was trying to say about net.exe without paramaters:

author: GlennSprigg
author_rank: Super Contributor
timestamp: November 21, 2022, 11:09:53 am
content: Thanks againPAOPBZ!!! I will go over that myself, in a few mins!!! (sorry!)Here's my promised response!...I have a 'Java' application, that runs (quite well I might add!) from specific command files, from within "cmd.exe".If I (within the command-prompt!) go to the relevant directory, and run the 'command' file that they want,thenall works well!!. Toside-trackjust a bit, re: my original above mentioned 'coding', it i s evident to me thatsuch 'statements' typed within the Command-Shell, like 'ipconfig' etc etc are NOT 'parameters/arguments' as such,but are actually '.exe' programs, which when typed & hit Return, know where to find them because of the extensiveWindows Path Environment Variable, so it doesn't care what actual Directory it is in!! Now THAT info, like 'ipconfig'is put into the '.arguments' section of the '.cmd.exe' executable call...So why can't I reference what ever other 'executable' '.cmd' or '.bat' that I choose to run, AFTER the command prompthas been invoked??? (Using .arguments for such redirection & running). The problem I have is with the actualSyntaxof the 'parsed' parameters/info from VB.NET, that is being 'parsed' along to the command prompt??B.T.W. One simple easing of the arguments/parameters situation, is by the alternate use of process declarations, is like...Code:<YourProcess>.FileName = "<MyExecutable.exe>"andCode:<YourProcesss>.WorkingDirectory = "<your progs drive and directory>", so you don't have to 'parse' that!!There is so much on the Internet, that my 'brain' is ceasing to function! All I hope to do, is from within VB.NET, go to thecommand prompt, set the working directory, (done!), and execute a 'someprog.cmd' . Getting the RETURN text to a textbox is not the problem. That's sorted. Sorry to be a pain!. (Still not well, but trying...)P.S... My intention is to create a Windows Front-End for the software. I am familiar for many years with writing generalVB apps, but interfacing with the 'Command Prompt' has never been my Forte.... (Derrr...)

author: PA0PBZ
author_rank: Super Contributor
timestamp: November 21, 2022, 11:24:20 am
content: Probably a redundant comment, but you can specify the full path so you don't need a working directory or an entry in the path variable.So what you are have is a .cmd or .bat file that executes the Java application, and you are trying to get the output from that Java application?Can you post the contents of the .cmd or .bat file or is it confidential? I wonder if it is possible to call the Java application directly from vb.net.Did you create the Java application? If so, can you create a minimal application that demonstrates the problem?

author: JohanH
author_rank: Frequent Contributor
timestamp: November 21, 2022, 11:47:30 am
content: Quote from: GlennSprigg on November 21, 2022, 10:22:23 amOK, I never used vb.net, only wscript/cscript scripting host, so there might be some differences (very similar, though).

author: GlennSprigg
author_rank: Super Contributor
timestamp: November 23, 2022, 12:10:05 pm
content: Quote from: PA0PBZ on November 21, 2022, 11:24:20 amThank you for your patience, and helpPAOPBZ... And sorry about my delayed response! (I'm fighting med probs!).No, there is nothing secret/personal about this App!, & I'll gladly share it. (I just hate bothering people too much!!).The 'Java' App is called 'MOSpeed' which is a Cross-Compiler for the Commodore-64 or Emulators, to compile 'BasicV2' toM/C.(There are others, but it's amazing how well this one works, with either.bas or .prg files, & builds automatic loaders).it can be found on GitHub at....https://github.com/EgonOlsen71/basicv2The download is a .zip file, which expands to quite a few files, and a few Directories. Change to the directory called 'dist'.(Note: this app requires at 'least'Java vesrion 11. The latest is version 19.x.x)Assuming the location ofMOSpeedis in<drive>/<directory>/mospeed-basicv2-master/after extracting the original .zip file,then within that directory, go to the sub-directory called 'dist'. Now ONE of the files there, (that I use!) is called 'mospeed.cmd'This is the main initiator for the whole process, and below, is a copy of it's contents, (although you don't have to know that)...Code:@echo offjava -Xmx768m -cp %~dp0/basicv2.jar;basicv2.jar;dist/basicv2.jar com.sixtyfour.cbmnative.shell.MoSpeedCL %*When run, at the Command Prompt, and in the correct directory, it all works fine!!, except it will initially complain that there is nofile to initially open, or work with. so runningmospeed example.baswill work, with the example.bas file in the same directory.There are many 'parameters' that can be parsed to 'mospeed', and 95% of them are never needed, but MAY need parsing!!!You can view those by typing 'mospeed /?'So back to my problem?? via VB.net, I want to call 'cmd.exe', ok. Change to the 'working directory' (OK), and run the 'prog' called'mospeed.cmd' , as well as pass certain parameters to THAT.... Argh. (Thanks to all here). I think I'm getting confused with theterm 'Arguments', which mean something different tome??, as well as the SYNTAX within VB.NET when PARSING such parameters??

author: PA0PBZ
author_rank: Super Contributor
timestamp: November 23, 2022, 02:07:08 pm
content: Try this:Code:Public Class Form1Private Sub Run_Click(sender As Object, e As EventArgs) Handles Run.ClickDim oProcess As New ProcessDim oCmd As New ProcessStartInfo("mospeed.cmd", "example.bas")oCmd.UseShellExecute = FalseoCmd.RedirectStandardOutput = TrueoProcess.StartInfo = oCmdChDir("C:\BasicV2\dist")oProcess.Start()Dim sOutput As StringsOutput = oProcess.StandardOutput.ReadToEndOutput.Text = sOutputEnd SubEnd ClassThrowing out the streamreader part fixed the output.My files are in C:\BasicV2, if yours are anywhere else change the ChDir of course. If you want to compile a .bas that is somewhere else change the parameter "example.bas" to include the full path. If you are still struggling to use parameters just ask a more specific question.

author: GlennSprigg
author_rank: Super Contributor
timestamp: November 25, 2022, 12:15:31 pm
content: Thank you so much for all that,PAOPBZ!!!(again I apologize for any delayed response, due to heath issues).I understand, & will try your mods/code tomorrow!!I think I was confused why one did not have to refer to 'cmd.exe' before linking elsewhere!!??, and wasn't aware thatdirectlyreferring to the 'mospeed.cmd' in the 1st place would work!!Thank you again. I understand your statement about ...If you are still struggling to use parameters just ask a more specific question.but was barking up the wrong tree !!!You are indeed a Gentleman, and will report back with my improved results!!! Glenn.

author: GlennSprigg
author_rank: Super Contributor
timestamp: November 27, 2022, 12:33:06 pm
content: ToPAOPBZ, (and everyone else!!)...Everything seems to be working now, even (finally!!) with all my specific Mods!!!Yes...I needed to change the initial Coding, as peryourrecommendations, but I still ran into problems at first!!At first, I wasfinallyable to execute theCommand & Parametersthat I wanted, (which auto-run 'cmd.exe' in the background anyway),but could not still get any output to the Multi-Line 'TextBox', no matter what I changed!! (All is fine now though). To step back a bit...I was originally using an older 'Pro' version of 'Visual Studio 2015', which seemed to be failing, even though in my 'App' I was NOT usingany add-on repositories/references. So I downloaded/installed the latest 'free' VS-2022 "Community" version, and started from Scratch!!MAJOR problem after problem trying to Debug & Run!!... It was constantly throwing errors, & denied access! I found out that my MAINproblem, (other than your needed corrections!), was my AntiVirus Software!! I initially 'allowed' MS-VS to install & Run within it, but therewere still problems. FINALLY, after fully turning off 'Auto-Containment' etc etc, NOW everything WORKS as it should!!I was/(am) using the full package of 'Comodo Internet Security', which is incredible in what it does/can do, but is really complicated inregards to all the settings, and 'self-learning' ability... I'm going to change back to 'Avast', although their 'ads' were getting annoying!P.S....With the new (& old?) 'IntelliSense' enabling, within Visual Studio, it was driving me nuts with so many suggestions after suggestionsregarding code improvements, alternative commands/orders of them, & when/where/how to declare/use Variables!!!... I understoodwhat it was saying!, but mostly chose to ignore it for now!! Oh!... By the way PAOPBZ, It was (system) suggested, that instead of using'CHDIR', that I use a more powerful/modern replacement, of for example...FileIO.FileSystem.CurrentDirectory = "C:\BasicV2\dist"etc...Both work!, but I'm using the one they suggest!! Thanks again, for everything MATE!!!

author: JohanH
author_rank: Frequent Contributor
timestamp: November 27, 2022, 12:50:44 pm
content: Quote from: GlennSprigg on November 27, 2022, 12:33:06 pmNowadays Windows Defender is good enough on an otherwise regularly updated Windows. No need for third party software. Or use Linux...