Posts Tagged ‘visual basic’

Automating Command Line Applications VisualBasic.Net

April 29th, 2009

A lot of the programming that I do for myself involves the creation of little tools that patch two existing parts of my workspace together so that they work better. For example, I recently had need of an SFTP client that links into a database front end to retrieve a list of files to download. Since the SFTP specification is ridiculously long and convoluted, I decided to try and use WinSCP to take care of the FTP portion of the program instead of re-inventing the wheel. Because I was trying to patch the database layer and FTP client together into a single application, I decided to take a shot at controlling WinSCP directly from the command line so that the two portions of the project were contained in a single application.

Readers who have spent time fooling around in Visual Basic 6 might remember the Shell function that allows a program to launch another process. Generally speaking, if we don’t care about the output of the program, or are just launching something like a browser window, this command is more than sufficient. However in this case, I wanted to capture the output of WinSCP and its exit code so that I could tell if errors had occurred and take appropriate action on them, so I needed something with a little bit more kick.

Since I wrote my code with WinSCP in mind, the rest of the article is going to focus on creating an application that will serve as an FTP front-end that allows the user to upload or download a file to or from a remote SFTP server. That said, the ideas are general and easy to adapt to any other application that supports command-line arguments.

You’ll want to begin by dragging and dropping the executable that you wish to automate directly into your project, and setting its “Copy to Output” property to “Copy to Newer” to ensure that your application can always find it. For WinSCP, that included both the winscp.com and winscp.exe files from the install directory. That done, let’s jump in.

Using System.Diagnostics.Process:

This handy System class provides a beefier version of the Shell functionality, and is really very easy to use, once you get the hang of it. At the top of your class, add the line imports System.Diagnostics so that we can find the pieces that we’ll need with ease.

When automating a command-line application, we first create a ProcessStartInfo object that contains all of the information about the process that we would like to create. Start by declaring one of these:

Dim startInfo As New ProcessStartInfo("winscp.com")

This line creates a ProcessStartInfo object with the path to the application that we’d like to automate as it’s only argument. Next, we set a few properties of the object:

startInfo.Arguments = commandLineArguments
startInfo.UseShellExecute = False
startInfo.RedirectStandardOutput = True
startInfo.CreateNoWindow = True

The first line passes the string commandLineArguments to startInfo, telling it to give them to winscp.com as command line arguments. Command line argument options for most programs can be found on their websites. If you’re interested in WinSCP in particular, it has some great online documentation that you can take a look at. The next three lines tell the startInfo object that it should direct the standard output from the process to a System.IO.StreamReader object where we can intercept it, and that it should run that process in the background, without showing the window to the user.

Next, we create a new Process object, and pass it the information that we just stored in startInfo:

Dim p as New Process
p = Process.Start(startInfo)

This creates a new process for winscp.com, and passes it the command line arguments that we stored earlier in startInfo. Now, we need to catch the standard output from our newly created process and store it somewhere so that our users can see what the application did.

Dim s as IO.StreamReader = p.StandardOutput
While p.Responding
Try
lstOutput.Items.Add(s.ReadLine)
Catch
Exit While
End Try
End While

On the first line, we create a new IO.StreamReader object that will catch the standard output from our hosted process. The while loop tries repeatedly to read a line of output from the application and dump it into a list box while the process is still responding to the operating system. When the application finishes its work, s.ReadLine will return null, killing the loop and allowing the program to continue.

Finally, when a program exits, it returns an exit code that lets the operating system know whether or not it completed its work successfully. We can access this code with the line

p.ExitCode

In most programs, if this returns zero, the program finished successfully. If an error was encountered, this function should return something other than zero, and depending on the program, the other value may have some meaning that you can use as a status check.

Next Steps:

Because this code is purposely general, it should be easy to adapt it to control most any application that provides some kind of command line interface. This method can make for a nice alternative to batch scripts if you need to write batch scripts with some kind of logic in them and don’t feel like installing a python interpreter to get the job done. At the cost of making the code application-specific, you could easily change it to analyze the output from the program and take some action based on those return values.

Downloads:

As always, the source is available for download. Check out Tyler Burton’s Hash Verifier application if you want to ensure that you’re getting the same copy of the code that I claim you are.
Source Code: Click Here
MD5 Hash: 4B5D543E193CAC2B4B9477727E17EFB2
SHA1 Hash: ABD8692CBB20D316DBF35FE650399D034DA57697

OpenGL in VisualStudio.Net with the TAO Framework

March 4th, 2009

Some who read me on a regular basis might be wondering what ever happened to the RPG I was writing in VisualStudio using Microsoft’s XNA technology. Truth is, it never went anywhere. Like a lot of past projects, I got very psyched about it, wrote a bunch of code, and then school started and I got way too busy to finish it. I haven’t touched the code since I wrote that post, but now I’ve got some new ideas.

Last term in school, I took an excellent course on OpenGL programming with C++. The course covered how to create and render 3D graphics, with study lent to topics such as window management, points, vertices, and polygons, lighting and shading, hidden surface removal, and texturing. My earlier post about the MAX 3D Engine was a byproduct of that course.

Since then, I’ve discovered a managed .NET wrapper around the OpenGL libraries called the Tao Framework that allows you to (in theory) code any graphics application in Visual Basic or C# that you could in C++, with the added bonus of the pretty IDE, code completion, top of the line window handling procedures, and the .NET libraries. Now at this point, if you’re a graphics programmer, you’re laughing aloud at my outrageous claim – managed, run-time interpreted code could never be fast enough to run a video game! You may be correct. Frankly, I have no idea, as I haven’t yet had the time to write a full video game.

What I have come up with however, are two starter projects for anybody wishing to try their hand at OpenGL programming using Tao and Visual Basic.NET. The code in both is well documented, easy to follow (especially if you are familiar with standard OpenGL routines), and seems to run at a reasonable 60fps. Now, I can’t tell until I add a few more polygons to the scene whether this framerate is an artificial limit applied by the environment, or if interpreted code actually has no hope of ever running a game at a reasonable speed. That is an experiment for a later day. For now, I will simply share these starter projects for all to use. If you do something with them, please leave a comment and let me know how it went.

Tao2D Test Harness:

A simple application that spins a tri-coloured, smooth-shaded triangle around the y-axis.

Source Code: Tao2D Source VB.Net.zip

MD5 Hash: 4DA0FC584B1EF8738B3B9CA4C1F55388

Binaries: Tao2D Binary.zip

MD5 Hash: 611382CB00CADD860A81A85573CBA763

Tao3D Test Harness:

A simple application that spins a really crappy looking cone around the x-axis

Source Code: Tao3D Source VB.NET.zip

MD5 Hash: 8D54DB42109F12C745AD14922FF8850E

Binaries: Tao3D Binaries.zip

MD5 Hash: 5D4CD4D02B3EE1194758A543DF36C034

As always, I recommend using Tyler Burton’s Hash Verifier program to verify the integrity of these downloads.

Post-Midterms Class Roundup

November 1st, 2008

So midterms are finished, and life goes on. As much as I’d love this post to be tech-oriented, showcasing some brilliant new software that I’ve written in my spare time, the truth is, I haven’t had enough spare time lately in which to write any software of value.

The small exception to that claim is this app, a neat little VB and XML oriented program that allows you to read an iTunes playlist file (as exported to XML), and auto-copy all of the songs in that playlist to any folder you wish, preserving the artist/album/song file structure.

I wrote the program to fill my brand new blackberry with a selection of excellent songs, because the built in Roxio media manager is great for pictures, but slow as sin when it comes to larger media files. After writing it, I realized that it could also be a useful piracy tool; but then again, a hammer could be a positively fantastic homicide tool in the wrong hands too. Of all the libraries I’ve ever written, my .NET XML parser has spawned off more crappy little programs, making it possibly the most useful bit of code I’ve created.

Other than that, my coding has lately been limited to some cool graphics stuff in with OpenGL and C++. So far we’ve constructed a classic spinning cube implementation, complete with a custom view pipeline that implements transformations, dynamic shading, and back face culling in software. The next topic that we’re studying is texturing, and eventually, ray tracing and shaders.

While none of my assignments for this course have been interesting enough to warrant posting, I’ll definetly up my final project for the course, which at this point, is probably going to be a 3D terrain generator, similar to that of Sim City 4 fame.