Hobby Project, need some help with issuing command lines....
Hobby Project, need some help with issuing command lines....
Hobby Project, need some help with issuing command lines....
Hobby Project, need some help with issuing command lines....
Hobby Project, need some help with issuing command lines....
Hobby Project, need some help with issuing command lines.... Hobby Project, need some help with issuing command lines.... Hobby Project, need some help with issuing command lines.... Hobby Project, need some help with issuing command lines.... Hobby Project, need some help with issuing command lines.... Hobby Project, need some help with issuing command lines.... Hobby Project, need some help with issuing command lines.... Hobby Project, need some help with issuing command lines....
Hobby Project, need some help with issuing command lines.... Hobby Project, need some help with issuing command lines....
Hobby Project, need some help with issuing command lines....
Go Back  Xtreme Visual Basic Talk > > > Hobby Project, need some help with issuing command lines....


Reply
 
Thread Tools Display Modes
  #1  
Old 09-11-2009, 10:49 AM
RitchieTheBrit RitchieTheBrit is offline
Newcomer
 
Join Date: Sep 2009
Posts: 5
Default Hobby Project, need some help with issuing command lines....


Hey guys! Mucking around with VB Express '08. This is the second prog I have tried making, and although it is quite straightforward, I am having issues finding out the best way to work it.

The prog will basically need to send a command line to an external program, with a switch, the program will output tab delimited information to STDOUT, and I want to then perform some file manipulation based on a certain part of each line of the STDOUT. The program I am launching can also output to a text file easily enough, so I will get it working like that, and incorporate STDOUT at a later point.

I have broken it down, got all the filepaths put in with user GUI, but I am having some problems getting VB to pass a command line correctly. I have tried loads of things, but this makes the most sense...

Code:
Shell("cmd.exe")
SendKeys.Send(ExecPath & " -listextrainfo > extrainfo.txt")
SendKeys.Send("{ENTER}")
ExecPath is the variable containing the executable. This should create a file in the executable folder called extrainfo.txt. Instead I am getting a whole mess of command line windows popping up, and some messed up output like the key has been pressed and held.

I am working under Windows 7 64bit, if thats helps. Would this be the correct command to use?
Reply With Quote
  #2  
Old 09-11-2009, 11:08 AM
Qua's Avatar
QuaHobby Project, need some help with issuing command lines.... Qua is offline
Impetuous & volatile

* Expert *
 
Join Date: Apr 2005
Posts: 2,177
Default

Your approach to this problem is what is causing you issues. Sending single key strokes without advanced API at works is always doomed to failure.

Instead of relying on SendKeys you should instead redirect your focus on the System.Diagnostics.Process class. It will allow you to open an executeable, pass commandline arguments to it and redirect it's standard output and input streams.

Take a look at this article that covers an implementation of what you're trying to aproach.
__________________
Reading is the foundation for all knowledge - Unknown.
Reply With Quote
  #3  
Old 09-11-2009, 11:54 AM
RitchieTheBrit RitchieTheBrit is offline
Newcomer
 
Join Date: Sep 2009
Posts: 5
Default

That makes sense!

Unfortunately the first line throws errors, and I am still very much learning what the hell I am doing...

Code:
System.Diagnostics.ProcessStartInfo outputInfo = New System.Diagnostics.ProcessStartInfo(ExecPath, " -listextrainfo")
Error 1 'ProcessStartInfo' is a type in 'Diagnostics' and cannot be used as an expression.

Error 2 Name 'outputInfo' is not declared.

And it keeps trying to wrap a parenthesis starting at outputInfo to the end of the line. I thought that this was a declaration in itself, so I shouldn't need to declare it first.
Reply With Quote
  #4  
Old 09-11-2009, 12:08 PM
Qua's Avatar
QuaHobby Project, need some help with issuing command lines.... Qua is offline
Impetuous & volatile

* Expert *
 
Join Date: Apr 2005
Posts: 2,177
Default

Ohh I'm very sorry for confusing you. The article I linked to is written in C# which is another language making use of the .NET Framework. Here's the code translated with a C# to VB.NET translator:
Code:
Dim sCommand As String = (" -u "  _
            + (sUserId + (" --password="  _
            + (sPwd + " mysecretdb "))))
Dim oInfo As System.Diagnostics.ProcessStartInfo = New System.Diagnostics.ProcessStartInfo("c:\mysql\mysqldump.exe", sCommand)
oInfo.UseShellExecute = false
oInfo.ErrorDialog = false
oInfo.CreateNoWindow = true
oInfo.RedirectStandardOutput = true
Try 
    Dim p As Process = System.Diagnostics.Process.Start(oInfo)
    Dim oReader2 As System.IO.StreamReader = p.StandardOutput
    Dim sRes As String = oReader2.ReadToEnd
    oReader2.Close
    'All the external programs output is now in sRes 
    'do what we want with it - calling fake function AnalyzeResult 
    AnalyzeResult(sRes)
Catch ex As Exception
    Console.WriteLine(ex.Message)
End Try
The concept is the same though and the code should look rather similiar.
__________________
Reading is the foundation for all knowledge - Unknown.
Reply With Quote
  #5  
Old 09-11-2009, 12:09 PM
AtmaWeapon's Avatar
AtmaWeaponHobby Project, need some help with issuing command lines.... AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

*edit* Bah, beat me to it.

You're declaring your variables as if you were writing C# code. You are getting errors that indicate you are using the VB .NET compiler. Make sure your language and compiler match.

In other words, to use the example, spend a couple of seconds thinking about what the article's C# code does and translate it to VB .NET. It's not doing anything that requires major changes, and actually has a subtle bug that would make it not work as-is in C# (this bug is not present in the VB .NET transliteration.)
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #6  
Old 09-11-2009, 01:05 PM
RitchieTheBrit RitchieTheBrit is offline
Newcomer
 
Join Date: Sep 2009
Posts: 5
Default

Thanks for the help guys! C# actually looks more streamlined than VB. Is it as easy to learn?
Reply With Quote
  #7  
Old 09-11-2009, 01:21 PM
Qua's Avatar
QuaHobby Project, need some help with issuing command lines.... Qua is offline
Impetuous & volatile

* Expert *
 
Join Date: Apr 2005
Posts: 2,177
Default

They're both equally easy to learn IMO. My first language (VB6 / VB.NET) took me several years to learn. Not long ago I decided to switch to C#, and in matter of a month I've gotten fully accustomed to C# writing it every day.

My point is, once you know how to program and even more specific you know how .NET functions, it's all about little syntactical differences. People often ask if they should learn C# or VB, and all I can say is, learn whatever is served to you, once you know a switch is nothing but a minor operation.
__________________
Reading is the foundation for all knowledge - Unknown.
Reply With Quote
  #8  
Old 09-11-2009, 02:49 PM
AtmaWeapon's Avatar
AtmaWeaponHobby Project, need some help with issuing command lines.... AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

I agree with Qua; 99% of the differences between VB .NET and C# are learning "It looks like this in VB .NET and looks like this in C#". Since they both use the .NET Framework, there's no difference in what classes, properties, and methods you use to accomplish a task. I learned VB .NET over 2 or 3 months, spent 3 years using it, and learned C# in a week.

I believe that Microsoft started out with two different ideas for each language. VB .NET was intended for Rapid Application Development, much like VB6. C# was focused more on framework developers, people who wrote code to be used by other programmers. VB .NET had better designer, Intellisense, and code snippet support and a feature set focused on trying to provide shortcuts to common application tasks. C# exposed more low-level .NET concepts like namespaces and had stronger support for XML documentation. Over time, developers are realizing that writing applications and writing frameworks aren't *that* much different: applications that follow good framework design guidelines are easier to maintain. So, the feature sets of each language grow closer together in each version.

Syntactically, I feel like the main difference is VB reads more like a sentence and therefore requires more typing to get a task done. Compare variable declaration:
Code:
' VB:
Dim value As Integer = 3

// C#
int value = 3;
The right way to read both lines is "Create a variable named value of the type Integer and assign it a value of 3." VB .NET is closer to how the sentence works. Dim means "create a variable", then the name and type come, and then assignment. In C#, you have to skip around; the type comes before the name and there's no clear "this is a variable declaration line" indicator. However, it's noticeably less verbose. Another good example is adding a dynamic event handler:
Code:
'VB:
AddHandler btnOK.Click, AddressOf btnOKOnClick

// C#:
btnOK.Click += btnOKOnClick
The proper way to read both lines is "Create an EventHandler delegate for btnOKOnClick and assign it to be one of the targets of the btnOK.Click event." In VB .NET, it reads like "Add a handler to this event, here's the address of the handler." In C#, it looks more like "Add this handler to the event."

It will help you to at least be familiar with C#. Most of the .NET world has settled on C#, and most examples are in C#. Right now, I prefer C# because the unit testing and mocking frameworks I use work best with lambda methods, and lambda support in VB .NET is pretty awful. For example, here's a comparison of code that uses a lambda method to find all even numbers in a generic list of integers (example ripped from this blog:
Code:
' VB:
evenNumbers = arrInt.FindAll(Function(i2 As Integer) i2 Mod 2 = 0)

// C#:
evenNumbers = arrInt.FindAll((i) => i % 2 == 0);
VB .NET is so much more verbose in this respect I am dreading the moment when I add automated tests to my current hobby project. I feel like VB .NET needs some work to its LINQ support as well, though in VS 2010 most of the problems are solved.
Reply With Quote
  #9  
Old 09-13-2009, 07:31 AM
RitchieTheBrit RitchieTheBrit is offline
Newcomer
 
Join Date: Sep 2009
Posts: 5
Default

Well, it's almost, kind-of half running!

The command line formation needs to look like this...

Code:
"C:\Program Files\Program\program.exe" -switch
If I do it that way, I get the expected result from the command line. This is the line that seems to be causing issues I think...

Code:
Dim ExecOut As System.Diagnostics.ProcessStartInfo = New System.Diagnostics.ProcessStartInfo("""" & ExecPath & """" & " -listextrainfo")
When it gets to the TRY section directly below that parses the stream and saved the result into a variable, it stumbles directly into the Exception catch...

Code:
A first chance exception of type 'System.ComponentModel.Win32Exception' occurred in System.dll
...with the output the paramter is incorrect. The STDOUT I am expecting is a simple tab delimited file.

Like I said, I am a complete noob who is stumbling through this, so thanks for the patience you guys have shown!
Reply With Quote
  #10  
Old 09-13-2009, 07:46 AM
Qua's Avatar
QuaHobby Project, need some help with issuing command lines.... Qua is offline
Impetuous & volatile

* Expert *
 
Join Date: Apr 2005
Posts: 2,177
Default

You're right. The issues is how you define the ProcessStartInfo parameters. If you take a look at the constructor's method signature you'll see that it takes two parameters: The file to be opened, and the arguments to be passed to the file.

The thing is, you don't have to worry about having quotes around the file name, the underlying implementation of ProcessStartInfo and Process will take care of this for you. The solution is a simple as:
Code:
Dim startInfo As New ProcessStartInfo("C:\Program Files\PRogram\program.exe", "-switch")
__________________
Reading is the foundation for all knowledge - Unknown.
Reply With Quote
  #11  
Old 09-13-2009, 08:55 AM
RitchieTheBrit RitchieTheBrit is offline
Newcomer
 
Join Date: Sep 2009
Posts: 5
Default

I should have spotted that! Grrrrrr! Lol

Thanks Qua, the TRY section works now. Onwards!
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Hobby Project, need some help with issuing command lines....
Hobby Project, need some help with issuing command lines....
Hobby Project, need some help with issuing command lines.... Hobby Project, need some help with issuing command lines....
Hobby Project, need some help with issuing command lines....
Hobby Project, need some help with issuing command lines....
Hobby Project, need some help with issuing command lines.... Hobby Project, need some help with issuing command lines.... Hobby Project, need some help with issuing command lines.... Hobby Project, need some help with issuing command lines.... Hobby Project, need some help with issuing command lines.... Hobby Project, need some help with issuing command lines.... Hobby Project, need some help with issuing command lines....
Hobby Project, need some help with issuing command lines....
Hobby Project, need some help with issuing command lines....
 
Hobby Project, need some help with issuing command lines....
Hobby Project, need some help with issuing command lines....
 
-->