0

An application I need to use (USB capture utility) has a .cmd version that I can call from my Visual Basic code. I am able to launch the application and put it in "command line mode" like this:

Public Class MyClass
    Dim StreamWriteUtility As System.IO.StreamWriter
    Dim StreamReadUtility As System.IO.StringReader
    Dim ProcessInfo As ProcessStartInfo
    Dim Process As Process

    Public Sub StartUSBCapture(ByVal DataStorageLocation As String)
        Dim ProcessInfo As ProcessStartInfo
        Dim Process As New Process

        ProcessInfo = New ProcessStartInfo("C:\FW_Qualification_Suite\data-center-windows\data-center\bin\datacenter.cmd", "-c ")
        ProcessInfo.CreateNoWindow = True
        ProcessInfo.UseShellExecute = False 'Must be changed if redirect set to True
        ProcessInfo.RedirectStandardInput = True

        Process = Process.Start(ProcessInfo)

        SWUtility = Process.StandardInput

        While True
            SWUtility.WriteLine("run") 'Looping for test to ensure this isn't a timing issue
        End While
    End Sub
End Class

This launches the application and opens a separate command line window that should accept further commands (i.e., capture, run, stop, etc). However, I am having trouble getting those subsequent commands to show up in the command line window. I've tried redirecting the standard input of the process, but still nothing shows up in the console window.

enter image description here

Can anyone tell how I'm supposed to actually get these commands from my Visual Basic program into this application?

nobby
  • 373
  • 1
  • 3
  • 15
  • 1
    You would need to redirect standard input of the *process you create* and send text to it that way, not with `Console.Writeline` (your program has no connection otherwise to the created process). This has probably also been asked and answered here. – crashmstr Nov 19 '15 at 13:09
  • Similar: [Repeatably Feeding Input to a Process' Standard Input](http://stackoverflow.com/questions/6721396/repeatably-feeding-input-to-a-process-standard-input), [C# Process Call, Interact with Standard Input and Standard Output](http://stackoverflow.com/questions/19720288/c-sharp-process-call-interact-with-standard-input-and-standard-output) – crashmstr Nov 19 '15 at 13:11
  • Also: [Redirect process standard input in Task](http://stackoverflow.com/questions/25219334/redirect-process-standard-input-in-task) – crashmstr Nov 19 '15 at 13:12
  • I've read through the post on redirecting the standard input of my process, but still nothing shows up in the console window. Post edited for updated code. – nobby Nov 19 '15 at 13:45
  • Have you tried `WriteLine` over `Write`? I'd expect that the process is waiting for the actual newline character. – Bradley Uffner Nov 19 '15 at 13:54
  • @Bradley: Thanks, tried WriteLine, but no joy. – nobby Nov 19 '15 at 13:57
  • Also, I wouldn't expect your output to show up in the other window's console as if you typed it. The console window is sort of the "default input stream" for a command line process. When you redirect the input you are disconnecting the output of that console and substituting your stream for the console. In effect you are sending data directly to the application and skipping the console. – Bradley Uffner Nov 19 '15 at 13:59
  • @Bradley: nothing shows up in the application itself, either, nor does it execute the behavior it should if it had received a "run" command (i.e., start a USB capture). – nobby Nov 19 '15 at 14:00
  • Any more thoughts on what I'm doing wrong? – nobby Nov 19 '15 at 15:45
  • Try `While True`. VB.NET True/False values are 0 and -1. – Josh Nov 19 '15 at 16:31
  • @Josh: Thanks for the suggestion, but no difference. – nobby Nov 19 '15 at 17:59
  • Did you try it in conjunction with `WriteLine`? – Josh Nov 19 '15 at 18:10
  • @Josh: Yes, using `WriteLine`. Actually, tried it using just `Write` as well. – nobby Nov 19 '15 at 18:14
  • I've tried changing the application to a simple Notepad app, and it's still not working. One thing I DID notice is that if I set a breakpoint at `StreamWriteUtility = Process.StandardInput`, I never actually hit it. The `Process = Process.Start(ProcessInfo)` line gets executed, and then my debug session terminates without ever running the last two lines. Why would that be? – nobby Nov 19 '15 at 22:24

0 Answers0