r/visualbasic • • Feb 20 '20

Pass argument from vb to a cmd app

We have a list we need to input into a cmd app. What's the easiest way to pass values in rows into command prompt?

6 Upvotes

3 comments sorted by

3

u/TheFotty Feb 20 '20

Not 100% sure from your description, but assuming this cmd app is a 3rd party one and it isn't just command line arguments you need to pass when launching, but actual input you need to enter after the cmd app has been launched, then it sounds like you want to use process.start() while also using ProcessStartInfo flags with RedirectStandardInput. This allows your code to send the input to the app. There is also a RedirectStandardOutput property if you want to collect the actual output the app would normally display on screen so you can display it in your own user interface and just hide the cmd prompt screen.

https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.processstartinfo.redirectstandardinput?view=netframework-4.8

1

u/chrwei Feb 21 '20

depends which vb you're using and if vb can start the app or if it's already running

1

u/JakDrako Feb 21 '20

Here's an example of using "cmd.exe" from a vb program. It's adapted from this StackOverflow post.

Sub Main

	Dim p = New Process()
	p.StartInfo.UseShellExecute = False
	p.StartInfo.CreateNoWindow = False

	p.startInfo.FileName = "cmd.exe"
	p.StartInfo.Arguments = "/k"

	p.StartInfo.RedirectStandardInput = True
	p.StartInfo.RedirectStandardoutput = True

	p.Start()

	' adapted from: https://stackoverflow.com/questions/15870516/redirect-input-and-output-for-cmd-exe
	Dim prompt = "Prompt_C2BCE8F8E2C24403A71CA4B7F7521F5B_F659E9F3F8574A72BE92206596C423D5 "

	p.StandardInput.WriteLine($"prompt {prompt}")
	p.StandardInput.Flush
	p.StandardOutput.ReadLine()
	p.StandardOutput.ReadLine()

	Do
		Dim txt = Console.ReadLine
		If txt = "exit" Then Exit Do
		p.StandardInput.WriteLine(txt)
		p.StandardInput.WriteLine
		p.StandardInput.Flush

		Dim line = p.StandardOutput.ReadLine
		'Console.WriteLine(line)
		Do
			line = p.StandardOutput.ReadLine()
			If line = prompt Then Exit Do
			Console.WriteLine(line)
		Loop
	Loop

End Sub

Replace "cmd.exe" with your program; you'll probably won't need the "prompt" part... then replace the "Dim txt = console.readline" with getting your data.