I am trying to send a command to the external command line (cmd.exe) from the Windows form application that I'm writing in VB.NET (using VS2008).

I can only access the external program thru the command line (its not my program) and I must do so from a form app.

I am trying to use the following code. I am able to call a cmd.exe window, but I can't pass the command line my command.

Using mp As New Process
   With mp.StartInfo
      .FileName = "cmd.exe"
      .UseShellExecute = False
      .CreateNoWindow = False
      .RedirectStandardInput = True
	.RedirectStandardInput = True
   End With
      mp.Start()
      mp.StandardInput.WriteLine(p)
      Debug.WriteLine(p)
      mp.WaitForExit()
      mp.Close()
End Using

The command that I'm attempting to pass requires nested quotes. I believe the syntax is correct. However, at the moment, I can't pass anything to the command line to verify whether or not the command is syntactically correct. The command follows:

Dim p As String = Chr(34) & "C:\Program Files\proeWildfire 4.0\bin\proe.exe" & Chr(34) & " -g:no_graphics -i:rpc_input +batchedrw " & Chr(34) & "+input=L:\MechEng\Pro_E\Work_Order\W026637\tank_shell_master.asm.130" & Chr(34) & " " & Chr(34) & "+output=C:\Users\Rob Frei\Desktop\tank_shell_master.easm" & Chr(34) & " +measure"

What am I doing wrong?

Thx.

I am trying to send a command to the external command line (cmd.exe) from the Windows form application that I'm writing in VB.NET (using VS2008).

I can only access the external program thru the command line (its not my program) and I must do so from a form app.

I am trying to use the following code. I am able to call a cmd.exe window, but I can't pass the command line my command.

Using mp As New Process
   With mp.StartInfo
      .FileName = "cmd.exe"
      .UseShellExecute = False
      .CreateNoWindow = False
      .RedirectStandardInput = True
	.RedirectStandardInput = True
   End With
      mp.Start()
      mp.StandardInput.WriteLine(p)
      Debug.WriteLine(p)
      mp.WaitForExit()
      mp.Close()
End Using

The command that I'm attempting to pass requires nested quotes. I believe the syntax is correct. However, at the moment, I can't pass anything to the command line to verify whether or not the command is syntactically correct. The command follows:

Dim p As String = Chr(34) & "C:\Program Files\proeWildfire 4.0\bin\proe.exe" & Chr(34) & " -g:no_graphics -i:rpc_input +batchedrw " & Chr(34) & "+input=L:\MechEng\Pro_E\Work_Order\W026637\tank_shell_master.asm.130" & Chr(34) & " " & Chr(34) & "+output=C:\Users\Rob Frei\Desktop\tank_shell_master.easm" & Chr(34) & " +measure"

What am I doing wrong?

Thx.

Instead of using cmd.exe for the file name, use the program you are executing, and pass it the arguments through the .Arguments property:

.FileName = "C:\Program Files\proeWildfire 4.0\bin\proe.exe"
.Arguments = "-g:no_graphics -i:rpc_input +batchedrw " & Chr(34) & "+input=L:\MechEng\Pro_E\Work_Order\W026637\tank_shell_master.asm.130" & Chr(34) & " " & Chr(34) & "+output=C:\Users\Rob Frei\Desktop\tank_shell_master.easm" & Chr(34) & " +measure"

That should accomplish what you are looking to do I think.

Thank you. That sort of helped ...

The proe process (xtop) is now starting up as a result of changing .FileName to the proe.exe path that you suggested.

However, the process dies rather quickly. I'm not sure that the eDrawings batch publisher that I'm trying to talk to likes the broken syntax.

I'm still at a loss on this ... can't I simply pass the full command to cmd.exe somehow?

Again, thanks.

Thank you. That sort of helped ...

The proe process (xtop) is now starting up as a result of changing .FileName to the proe.exe path that you suggested.

However, the process dies rather quickly. I'm not sure that the eDrawings batch publisher that I'm trying to talk to likes the broken syntax.

I'm still at a loss on this ... can't I simply pass the full command to cmd.exe somehow?

Again, thanks.

I played around with this a bit, and was unable to do it via the method you were trying. I was able to pass arguments to cmd.exe and startup notepad.exe giving it parameters to load a particular file via the .argument property. Trying to send the command string to the cmd.exe after start failed however. Putting in breaks just after start revealed why. Starting cmd.exe in this way caused it to close immediately. In otherwords the process is terminated before
this line executes:

mp.StandardInput.WriteLine(p)

cmd.exe has nothing to do and so the process closes (ie it is not waiting for an input stream).

I suspect that what you are running into is that the application is not accepting the parameters you are trying to pass in.

An easy way to test this is to paste your original string into a command prompt window and try executing it that way, to see if at the very least what you are trying to pass to it is causing it to do what you want. The reason I say this is I was digging around for info about proe.exe, and while I was unable to find command line parameter information, I did find a forum post that was talking about passing in a file name that contained a list of commands to run automated. If this is the case then you would need to write your string out to a text file and pass that filename into the .arguments property.

Here is the code I did the testing with note that the "/C" in the string is needed to tell cmd.exe that you want it to run something, so you might try this route as well. The second Code snipet should work assuming proe.exe takes those arguments. I am however questioning if enclosing all the arguments in quotes is neccecary or even prudent.

Dim p As String = "/C Notepad.exe " & Chr(34) & "test.txt" & Chr(34)

        Using mp As New Process
            With mp.StartInfo
                .FileName = "cmd.exe"
                .Arguments = p
                .UseShellExecute = False
                .CreateNoWindow = False
                .RedirectStandardInput = True
                .RedirectStandardInput = True
            End With
            mp.Start()
            'mp.StandardInput.WriteLine(p)
            Debug.WriteLine(p)
            mp.WaitForExit()
            mp.Close()
        End Using
Dim p As String = "/C" & Chr(34) & "C:\Program Files\proeWildfire 4.0\bin\proe.exe" & Chr(34) & " -g:no_graphics -i:rpc_input +batchedrw " & Chr(34) & "+input=L:\MechEng\Pro_E\Work_Order\W026637\tank_shell_master.asm.130" & Chr(34) & " " & Chr(34) & "+output=C:\Users\Rob Frei\Desktop\tank_shell_master.easm" & Chr(34) & " +measure"

        Using mp As New Process
            With mp.StartInfo
                .FileName = "cmd.exe"
                .Arguments = p
                .UseShellExecute = False
                .CreateNoWindow = False
                .RedirectStandardInput = True
                .RedirectStandardInput = True
            End With
            mp.Start()
            'mp.StandardInput.WriteLine(p)
            Debug.WriteLine(p)
            mp.WaitForExit()
            mp.Close()
        End Using

Sorry jbennet, but I do not see how this code permits me to pass a command to the command line once the process is started?

Thanks a million coat. I truly appreciate the help and you've provided lots of it ;-)

I will begin testing with what you've shared and report back.

If I have to write out to a text file, I might just as well generate a .bat programatically and then call it using process.start. I already know that the batch file approach works. I was, however, hoping to avoid the extra step and do this in one simple, fluid piece of code.

Okay, the notepad version of this works great. very cool.

Following your logic, I changed my batch program code to match. I can't explain why, but the process dies immediately.

I'm confident the command syntax is good. I've been checking this all along in VS2008 with debug.WriteLine(p). Though the tricky syntax doesn't offer confidence to the programmer ... JavaScript is easier.

In checking all of this, however, I discovered a problem on my end. I'm writing my code in a virtual environment (Vista64) on a Mac. I inadvertently failed to connect the "L:\ drive" which, of course, the command requires in order to run! Upon fixing that problem, I tried your earlier suggestion again, and it worked great.

So for the sake of clarity, should anyone else ever visit this post, the new code looks like this:

Using mp As New Process
                        With mp.StartInfo
                            .FileName = fn
                            .Arguments = p
                            .UseShellExecute = False
                            .CreateNoWindow = False
                            .RedirectStandardInput = True
                        End With
                        mp.Start()
                        'mp.StandardInput.WriteLine(p)
                        Debug.WriteLine(p)
                        mp.WaitForExit()
                        mp.Close()
                    End Using

Where fn is:

Dim fn As String = "C:\Program Files\proeWildfire 4.0\bin\proe.exe"

and p is:

Dim p As String = "-g:no_graphics -i:rpc_input +batchedrw " & Chr(34) & "+input=L:\MechEng\Pro_E\Work_Order\W026637\tank_shell_master.asm.130" & Chr(34) & " " & Chr(34) & "+output=C:\Users\Rob Frei\Desktop\tank_shell_master.easm" & Chr(34) & " +measure"

This code sends the command in "p" to Pro/Engineer. It tells the eDrawings batch publisher (loaded on top of Pro/E) to publish an assembly for viewing in eDrawings. The batch program I'm writing will eventually automate the process.

Thanks, coat, your help is greatly appreciated, and I think the code you offered in the notebook example will prove to be very useful for others (I know I'll be keeping the snippet on file).

Glad it worked for you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.