943,096 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Marked Solved
  • Views: 4409
  • VB.NET RSS
Dec 18th, 2009
0

Send Command to Command Line from Windows Form Application

Expand Post »
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.

VB.NET Syntax (Toggle Plain Text)
  1. Using mp As New Process
  2. With mp.StartInfo
  3. .FileName = "cmd.exe"
  4. .UseShellExecute = False
  5. .CreateNoWindow = False
  6. .RedirectStandardInput = True
  7. .RedirectStandardInput = True
  8. End With
  9. mp.Start()
  10. mp.StandardInput.WriteLine(p)
  11. Debug.WriteLine(p)
  12. mp.WaitForExit()
  13. mp.Close()
  14. 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:

VB.NET Syntax (Toggle Plain Text)
  1. 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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
rfrei is offline Offline
11 posts
since Dec 2009
Dec 18th, 2009
0
Re: Send Command to Command Line from Windows Form Application
Click to Expand / Collapse  Quote originally posted by rfrei ...
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.

VB.NET Syntax (Toggle Plain Text)
  1. Using mp As New Process
  2. With mp.StartInfo
  3. .FileName = "cmd.exe"
  4. .UseShellExecute = False
  5. .CreateNoWindow = False
  6. .RedirectStandardInput = True
  7. .RedirectStandardInput = True
  8. End With
  9. mp.Start()
  10. mp.StandardInput.WriteLine(p)
  11. Debug.WriteLine(p)
  12. mp.WaitForExit()
  13. mp.Close()
  14. 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:

VB.NET Syntax (Toggle Plain Text)
  1. 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:

VB.NET Syntax (Toggle Plain Text)
  1. .FileName = "C:\Program Files\proeWildfire 4.0\bin\proe.exe"
  2. .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.
Last edited by thecoat; Dec 18th, 2009 at 2:04 pm.
Reputation Points: 10
Solved Threads: 3
Newbie Poster
thecoat is offline Offline
16 posts
since Dec 2009
Dec 18th, 2009
0
Re: Send Command to Command Line from Windows Form Application
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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
rfrei is offline Offline
11 posts
since Dec 2009
Dec 18th, 2009
0
Re: Send Command to Command Line from Windows Form Application
Last edited by jbennet; Dec 18th, 2009 at 4:39 pm.
Moderator
Featured Poster
Reputation Points: 1764
Solved Threads: 573
Moderator
jbennet is offline Offline
16,481 posts
since Apr 2005
Dec 18th, 2009
0
Re: Send Command to Command Line from Windows Form Application
Click to Expand / Collapse  Quote originally posted by rfrei ...
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.


VB.NET Syntax (Toggle Plain Text)
  1. Dim p As String = "/C Notepad.exe " & Chr(34) & "test.txt" & Chr(34)
  2.  
  3. Using mp As New Process
  4. With mp.StartInfo
  5. .FileName = "cmd.exe"
  6. .Arguments = p
  7. .UseShellExecute = False
  8. .CreateNoWindow = False
  9. .RedirectStandardInput = True
  10. .RedirectStandardInput = True
  11. End With
  12. mp.Start()
  13. 'mp.StandardInput.WriteLine(p)
  14. Debug.WriteLine(p)
  15. mp.WaitForExit()
  16. mp.Close()
  17. End Using

VB.NET Syntax (Toggle Plain Text)
  1. 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"
  2.  
  3. Using mp As New Process
  4. With mp.StartInfo
  5. .FileName = "cmd.exe"
  6. .Arguments = p
  7. .UseShellExecute = False
  8. .CreateNoWindow = False
  9. .RedirectStandardInput = True
  10. .RedirectStandardInput = True
  11. End With
  12. mp.Start()
  13. 'mp.StandardInput.WriteLine(p)
  14. Debug.WriteLine(p)
  15. mp.WaitForExit()
  16. mp.Close()
  17. End Using
Reputation Points: 10
Solved Threads: 3
Newbie Poster
thecoat is offline Offline
16 posts
since Dec 2009
Dec 18th, 2009
0
Re: Send Command to Command Line from Windows Form Application
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?
Reputation Points: 10
Solved Threads: 1
Newbie Poster
rfrei is offline Offline
11 posts
since Dec 2009
Dec 18th, 2009
0
Re: Send Command to Command Line from Windows Form Application
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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
rfrei is offline Offline
11 posts
since Dec 2009
Dec 18th, 2009
0
Re: Send Command to Command Line from Windows Form Application
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:

VB.NET Syntax (Toggle Plain Text)
  1. Using mp As New Process
  2. With mp.StartInfo
  3. .FileName = fn
  4. .Arguments = p
  5. .UseShellExecute = False
  6. .CreateNoWindow = False
  7. .RedirectStandardInput = True
  8. End With
  9. mp.Start()
  10. 'mp.StandardInput.WriteLine(p)
  11. Debug.WriteLine(p)
  12. mp.WaitForExit()
  13. mp.Close()
  14. End Using

Where fn is:

VB.NET Syntax (Toggle Plain Text)
  1. Dim fn As String = "C:\Program Files\proeWildfire 4.0\bin\proe.exe"

and p is:

VB.NET Syntax (Toggle Plain Text)
  1. 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).
Reputation Points: 10
Solved Threads: 1
Newbie Poster
rfrei is offline Offline
11 posts
since Dec 2009
Dec 21st, 2009
0
Re: Send Command to Command Line from Windows Form Application
Glad it worked for you.
Reputation Points: 10
Solved Threads: 3
Newbie Poster
thecoat is offline Offline
16 posts
since Dec 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: dll problem with vb.net
Next Thread in VB.NET Forum Timeline: run time error





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC