VB.NET Woes with STDIN during Process.Start()

Saint Nick

Lifer
Jan 21, 2005
17,722
6
81
So, I am using an open source program that accepts "batch" lists of files. But instead of a command line argument, it accepts them from STDIN. For example, you can run the program on the command line like this

Code:
codegen.exe -s < c:\path\to\file.txt
And the codegen will give output with no issue.

However, I can't call this in VB.NET. Here is my current, non-working implementation...

Code:
            Dim CodegenProc As New Process
            Dim CodegenStreamReader As StreamReader

            CodegenStreamReader = My.Computer.FileSystem.OpenTextFileReader("c:\path\to\file.txt")

            CodegenProc.StartInfo.RedirectStandardInput = True
            CodegenProc.StartInfo.RedirectStandardOutput = True
            CodegenProc.StartInfo.UseShellExecute = False

            CodegenProc.StartInfo.FileName = "C:\codegen.exe"
            CodegenProc.StartInfo.Arguments = "-s"
            'CodegenProc.StartInfo.CreateNoWindow = True



            CodegenProc.Start()

            CodegenProc.StandardInput.WriteLine(CodegenStreamReader.ReadToEnd)

            CodegenStreamReader.Close()
What am I doing wrong? It opens up the command prompt and hangs. If you go to the command line and type "codegen.exe -s" only, you get this same behavior.

I have tried this:

Code:
CodegenProc.StartInfo.Arguments = "-s < c:\path\to\file.txt"
and this...
Code:
CodegenProc.StandardInput.Write("c:\path\to\file.txt")
and they don't work either... HELP!
 
Last edited:

brandonb

Diamond Member
Oct 17, 2006
3,731
2
0
I have something similar in my code: But its attached to the standard output, not input. But it works fine. I don't have Visual Studio installed on my home computer so I can't try to come up with a better solution for you, but I will take a stab at it when I get on my work computer.

(Basically this code is designed to run a bunch of .sql files against a db server, and report any errors coming back from sqlcmd commandline program)

Code:
                Dim newProc As Diagnostics.Process
                Dim p As New Diagnostics.ProcessStartInfo
                Dim strl_CMD As String
                Dim strl_RESULTS As String = ""
                Dim strl_LINE As String = ""

                For Each strl_SCRIPT As String In objl_SCRIPTS

                    strl_CMD = " -E " & _
                               " -S " & objv_PARAMETER("Server") & _
                               " -i " & Chr(34) & objv_PARAMETER("Path") & strl_SCRIPT & Chr(34) & _
                               " -d " & objv_PARAMETER("DB") & _
                               " -I"

                    p = New Diagnostics.ProcessStartInfo
                    p.WindowStyle = ProcessWindowStyle.Hidden
                    p.Arguments = strl_CMD
                    p.FileName = "sqlcmd"

                    p.RedirectStandardOutput = True
                    p.RedirectStandardError = True
                    p.RedirectStandardInput = True
                    p.UseShellExecute = False
                    p.CreateNoWindow = True

                    newProc = Diagnostics.Process.Start(p)
                    Dim objl_STREAMREADER As System.IO.StreamReader = newProc.StandardOutput
                    newProc.WaitForExit()

                    strl_LINE = objl_STREAMREADER.ReadToEnd.Trim()
                    If strl_LINE.Length > 25 Then
                        strl_RESULTS += strl_SCRIPT & vbCrLf & "-----------------------" & vbCrLf & strl_LINE & vbCrLf & vbCrLf
                    End If

                Next

I'm wondering if the codegen.exe is not immediately starting with the CodegenProc.Start() and is detecting no input stream and exits too soon, before your code has the opportunity to redirect the input and send the file.

I'd say this may be a problem more with codegen.exe. If its launched with the -s parameter, it should maybe wait for the standard input to be sent to it. You can test this by launching it in the command window with just the -s parameter, wait for it to hang, then type in c:\path\to\file.txt and hit enter.

If that works, I'd say change the startinfo.arguments to just -s. Attach your input stream, then do a writeline on the c:\path\to\file.txt
 

Apathetic

Platinum Member
Dec 23, 2002
2,587
6
81
One thing that strikes me as wrong is that you're trying to read from *any* data file specified by the user by having the OS redirect STDIN, yet your code is trying to open a *specific* file.

You want something like this:

CodegenStreamReader = CodegenProc.StandardInput;

Here's a link to an example that does what you want.

Dave
 

Saint Nick

Lifer
Jan 21, 2005
17,722
6
81
What I ended up doing is wrapping the codegen.exe into a batch program, then passing the c:\path\to\files.txt as an argument to the batch. Works really well.