Sensibility FTW. Awesome.

Share
As a U.S. citizen, I’m not the biggest fan of the U.S. government.  Having said that, every once in a while, they do something cool:

Take THAT Steve Jobs.  Your anti-competitive practices and AT&T’s bullshit finally pissed off the right people.  HA!

 

You gotta be kidding me….

Share

WTF??? Naughty BIOS....

This resulted in a BSOD and a spontaneous reboot. This is why I hit “save” after every line of code. *sigh*

 

Busy Busy Busy

Share

Hard at work!

Busy at work doing cross-platform application development in Mono/.NET using Monodevelop and GTK#.  Mmmmmmm……. We loves it.

 

CyrusBuilt StopWatch Library for Mono/.NET

Share

I’ve worked on several projects where I desperately needed functionality of a StopWatch in code.  For example, I worked on a front-end for nmap.  When ever you clicked the “start scan” button to initiate the nmap scan, the idea was to start a timer and then update a label with the elapsed time as the time passed, and then give a total scan duration (yes, nmap calculates scan duration itself, but I wanted the combined duration of the front-end’s operations *plus* nmap’s).

This really isn’t that complicated.  The System.Timers namespace provides most of what we need to do exactly that.  But I need MORE from the System.Timers class.  I needed methods that performed a number of calculations AND built a formatted elapsed time string for me, so I didn’t have to keep repeating this code in an event handler.  I have seen examples from people using the TimeSpan class where they would set a variable equal to DateTime.Now before they started an operation, then get the value of DateTime.Now when the operation was over and calculate then calculate the difference.  This is all well and good, but I needed something that acted more like a real stopwatch.  At a defined interval (or pulse, if you will), I wanted an event to get raised, which simulated the tick of the second hand on the stopwatch.  I could then have an event handler that got all the time information for the event and only have to update a label (or whatever) using the data passed from the event.

Yes, System.Timers.Timer does have an “Elapsed” event.  But the event doesn’t give me everything I want.  So basically, I decided to write my own reusable class library for both Mono and .NET that accomplished everything I needed, and then some.

The CyrusBuilt StopWatch class library has the following features:

  • Calculate elapsed time between start time and current time (if running) or start time and stop time.
  • Calculate total elapsed time in days.
  • Calculate total elapsed time in hours.
  • Calculate total elapsed time in minutes.
  • Calculate total elapsed time in seconds.
  • Calculate total elapsed time in milliseconds.
  • Get a formatted elapsed time string.
  • Raise an event that passes the above information in the event arguments.
  • Start/Stop the stopwatch.
  • Allow user defined “pulse” or “tick” interval (defaults to 1 second).
  • Set a flag to indicate whether or not the stopwatch is running.

The following is an example of how to use it:

Without events:

using CyrusBuilt.Utils;
 
StopWatch sw = new StopWatch();
sw.Start();
 
// Wait 3 seconds.
while (sw.IsRunning)
{
   System.Threading.Thread.Sleep(3000);
}
 
sw.Stop();
Console.WriteLine("Time elapsed: " + sw.GetElapsedTimeString());
sw.Dispose();

With Events

This is a lil bit trickier, but still quite manageable and probably the most common way it would be implemented in a Form.  First, let’s create an event handler for the Elapsed event. Assume we have a label control called MyLabel:

using CyrusBuilt.Utils;
 
// put this somewhere inside a class declaration or FormLoad event handler (for example).
StopWatch sw = new StopWatch();
 
// ........ shortened for brevity ..... //
private void StopWatchElapsed(object sender, StopWatchElapsedEventArgs e)
{
   MyLabel.Text = e.ElapsedTimeString;
}

Now we just need to register the event, then when the event fires after we start the stopwatch the label will be automatically updated with the elapsed time.

private void Go()
{
   sw.Elapsed += new StopWatchElapsedEventHandler(StopWatchElapsed);
   sw.Start();
 
   while (sw.IsRunning)
   {
      System.Threading.Thread.Sleep(3000);
   }
 
   sw.Stop();
}

In the above example, the label text should be “03:00″ (3 seconds, 0 milliseconds). Since we know only a matter of seconds will pass for our operation, we could have also written our event handler like this:

private void StopWatchElapsed(object sender, StopWatchElapsedEventArgs e)
{
   MyLabel.Text = string.Format("Elapsed: {0} seconds", e.ElapsedSeconds.ToString());
}

The StopWatch library is released under GPLv2 and the source code and binaries are available from the downloads page in the “Mono/.NET” section.

 

Checking for Java in VBScript (Existence and Path)

Share

I was working on another login script for one of our clients a while back and ran into a situation where I needed to programmatically check if Java ™ was installed.  I then had another scenario where I needed to get the path to the JRE.  Unfortunately, I didn’t know any good way to do that at the time so I put together a couple of helper functions.  I’m not entirely sure if this is the best way to do this, but its the best method I’ve come up with so far, and has served me well for a few weeks now.

Some background:

The client is a school with a large number of student and staff workstations (whole labs, classrooms, etc).  They have a (rather poorly written IMHO) educational software package written in Java deployed all over the place.  As such, the primary requirement for this software is the Java Runtime Environment (JRE).  Unfortunately, not all the workstations have the JRE (intentionally in some cases).  The client requested that the script logic conditionally create shortcuts on the desktop for various URLs and applications, with the Java application being one of them.  Naturally, a condition for creating the shortcut to the Java application needed to be that Java actually be installed in the first place (the second being that the application itself is installed, and the other being that the user belonged to a particular security group).

But, the shortcut can’t just point to the .JAR file.  It has to launch the runtime with the .JAR as an argument (also could’ve created a batch file or other script to do the same thing, but why bother) which means I needed to know the path to the JRE so I could build the path to the javaw.exe binary.

The code:

The method I chose involves looking in the local machine’s registry.  So before I go too deep into the Java stuff, we’re going to need the following function.  The purpose of this function is to check for the existence of a named value within a specified key in a specified hive in the local registry:

Function RegValueExists(ByVal Hive, ByVal Key, ByVal Value)
   Const sComputer = "."
   Dim arrValNames
   Dim arrValTypes
   Dim idx
   Dim iRet
   Dim objReg
   iRet = False
 
   Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & sComputer & "\root\default:StdRegProv")
   If (IsObject(objReg)) Then
      If (objReg.EnumValues(Hive, Key, arrValNames, arrValTypes) = 0) Then
         If (IsArray(arrValNames)) Then
            For idx = 1 To UBound(arrValNames)
               If (LCase(arrValNames(idx)) = LCase(Value)) Then
                  iRet = True
                  Exit For
               End If
            Next
         End If
      End If
   End If
 
   Set objReg = Nothing
   Erase arrValNames
   Erase arrValTypes
   RegValueExists = iRet
End Function

Now on to the good stuff. The following function will get the path to the newest version of installed JRE on the local machine:

Function GetJavaPath()
   On Error Resume Next
   Const HKLM = &H80000002
   Const sComputer = "."
   Dim sRet
   Dim sKeyPath
   Dim sValueName
   Dim sValue
   Dim objReg
   sRet = ""
 
   Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & sComputer & "\root\default:StdRegProv")
   If (IsObject(objReg)) Then
      sKeyPath = "SOFTWARE\JavaSoft\Java Runtime Environment"
      sValueName = "CurrentVersion"
      If (RegValueExists(HKLM, sKeyPath, sValueName)) Then
         If (objReg.GetStringValue(HKLM, sKeyPath, sValueName, sValue) = 0) Then
            If (Len(sValue) > 0) Then
               sKeyPath = sKeyPath & "\" & sValue
               sValueName = "JavaHome"
 
               If (RegValueExists(HKLM, sKeyPath, sValueName)) Then
                  If (objReg.GetStringValue(HKLM, sKeyPath, sValueName, sValue) = 0) Then
                     If (Len(sValue) > 0) Then
                        sRet = sValue
                     End If
                  End If
               End If
            End If
         End If
      End If
   End If
 
   Set objReg = Nothing
   GetJavaPath = sRet
End Function

And there you have it! If no JRE is installed, the returned path will be a blank string. Otherwise, the path to the latest JRE will be returned (Example: C:\program files\Java\jre6). Please, note that the installation path is what is returned, not the full path to any executable.  Now with this logic, we can also do an existence check by simply checking the output of GetJavaPath() for a blank string, then check if non-empty string contains a valid path:

Function IsJavaInstalled()
   Dim objFSO
   Dim sPath
 
   sPath = GetJavaPath()
   If (Len(sPath) > 0) Then
      Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
      If (IsObject(objFSO)) Then
         If (objFSO.FolderExists(sPath)) Then
            IsJavaInstalled = True
         End If
      End If
      Set objFSO = Nothing
   Else
      IsJavaInstalled = False
   End If
End Function

Hope this helps someone out there! If anyone knows a better or more accurate/efficient way to do this, feel free to share!

Happy coding.

 

Analysis Paralysis

Share

Currently, I am my employer’s sole software engineer.  We used to have another, but he resigned.  Even then, we never worked together on anything.  At my last job, I was one of two developers.  To be fair, neither employer is actually in the software business…. but the both had a very real need for an in-house developer.

My current employer relies on my abilities to streamline it’s operations and the operations of our clients via automation and tools.  I spend a LOT of my time developing one-off tools and scripts, as well as entire frameworks of reusable code to make things more efficient and save our staff and the our clients’ staff time.  I also build full-blown enterprise applications with SQL databases on the back-end.  Management tools, deployment tools, migration tools…. you name it.

But there is only one problem….. I do this alone. Now there is some advantages to this:  For starters, I maintain absolute control over the codebase.  I have final say in how it gets written, what language it gets written in, etc, etc.  I also have the flexibility of working from home from time-to-time and I get the resources I need to get things done (without much argument and without much questioning) because there isn’t anyone else there to refute my needs or compete for said resources.  Plus, I can work at my own pace (except if some crazy short migration or deployment schedule gets dropped into my lap at the last minute) for the most part.

But there is also a dark side to coding alone: basically…. you’re working in a vacuum.  You don’t have anyone to collaborate with.  If you get stuck, Google is your only friend.  There is no one to check your work, make sure you stick to best practices or proper development guidelines, test your code, help look for bugs, etc.  You’re one your own.  I do have people I can collaborate with from a design perspective though.  These guys are Systems Engineers, Network Engineers, Support Managers, and technicians.  Their input is quite valuable, because they are technical people and they know how software should work.  They also understand usability and can usually point out things that will probably drive a user nuts or leave them totally confused.

But I don’t have anyone else who actually understands code….. especially OOP.  Sure, we got a couple guys who can write some quick-and-VERY-dirty VBScripts or batch files, but thats about it.  Don’t misunderstand though… these guys are very talented individuals, but they simply aren’t Programmers.  There is a big difference.  A REAL programmer lives and breathes code… and understands multiple languages, knows when to use the right tool for the job and has a thorough grasp of OOP.  So while I still love what I do, and want to continue doing it, I often find myself with a sort-of “writers block” because I just can’t see a way forward.

This can be for a multitude of reasons:  didn’t get enough sleep, caffeine wore off, medical issues, totally stumped on a piece of code I can’t fix, too many distractions….. whatever.  I’ve had days where I’ve stared at my computer screen drawing a complete blank and barely capable of writing even a few lines of code.  Usually when this happens, I’ll try to divert my energy to documenting existing methods in a library, compiling API documentation, or what-have-you just so I can keep moving and making forward progress.  But even that can be a chore sometimes.  When you are totally responsible for the design, testing, development, documentation, and implementation, and all other aspects of a project,you WILL make mistakes. It is unavoidable.  Bugs are going to be present no matter how hard you try.

So I sit and stare at the code.  Read and re-read books, articles, class diagrams, etc in an attempt to figure out the best approach to solve a problem or analyze my code to determine if what I have is the best approach and if the direction I want to go in is correct and efficient.  This leads to a condition commonly referred to as “analysis paralysis”.  You keep analyzing over and over, trying to figure out what to do next or how to fix a bug and how its going to affect the “big picture” and not actually getting anything significant accomplished.

Coding without collaborating is not ideal.  I understand why some people would be drawn to it, since programming was traditionally solo task in the days of yore, but in today’s world, large projects simply cannot be accomplished alone.  Its a dead-end.  In my case, it just isn’t feasible to have multiple programmers at our organization.  For the most part, this hasn’t been too much of a problem so far because I’ve really only had a couple large-scale projects, and I’m pretty good about beating my head on something until I figure it out, and all the days where I’m “in the zone” more than make up for the days where I’m stuck.  But for those of you who are aspiring to be a professional software developer…. choose a path that allows for collaboration.  Google can only take you so far.  Plus….. Google isn’t going to check your code, perform extensive unit tests, or assist in debugging for you.

I read a very good post about this that I felt hit the nail on the head.  It’s ok to ask questions.  It’s ok to challenge others as well as yourself.  This doesn’t have to mean “face time”.  I personally, am a fairly social person; although I didn’t used to be.  So I fully understand that many programmers are introverts.  That being said, there are many tools for communication that allow you to collaborate without being face-to-face.  Web-meetings, instant messaging, e-mail, etc.  They all can be used for collaboration and many open-source projects accomplish exactly that on a global scale.

Collective knowledge is far better than trying to roll solo.

 

FAIL.

Share

So heres the thing…. I have a couple friends who do this kind of work, so I don’t like saying things like “construction workers are retards”.  But damnit…. they just keep giving me reasons!

Pssssssst….. hey buddy….. If you drive your truck with the boom up…. yer prolly gonna hit a power line or 2.  Just sayin…..

 

Palm, Suck it.

Share

So I noticed HP bought Palm…. and this makes me happy.  Every since they bought, then subsequently buried, BeOS I have been hoping for the day they would have the same fate.  With any luck…. HP will strip them down, extract the technology for their patent portfolio and then discard the rest.

*spit*

And BTW Palm…. your phones are atrocious.  I don’t know how you’ve managed to stay in business this long, but luckily it is no longer relevant.

 

Visual Studio 2008 “Themes”

Share

For the record, I’m not like some big Microsoft fanboy…. However, I work in the I.T. field, and thus dealing with Microsoft products is simply inescapable.  They simply have the largest market share, and as such, most of our clients use Microsoft Windows and Office and their networks are usually an implementation of Active Directory.

That being said, this also means most of my software development is done on Windows using Microsoft Visual Studio (C#).  One thing I got VERY used to in Linux was all my IDEs and code editors were configured using a much nicer color scheme.  I personally prefer darker color schemes.  This bright white background that is the default in Visual Studio, for starters, makes my eyes burn after 8 – 10 hours a day of coding.  So I finally got sick of it and took a look at the VS2008 settings and found that I could make changes to the 4 bajillion possible color settings available and decided that it would take too damn long to tweak each one and play with it until I find the right combination that I like.

Soooo…. and did a quick Google search and stumbled across this site that linked several VS2005 and VS2008 settings files that essentially act as themes.  They are just an export of all the color settings from the author’s Visual Studio configuration.  So just unpack the settings file, then click Tools –> Import and Export Settings… then select Import selected environment settings then save your current settings (just to be safe), then click the Browse… button at the bottom of the import screen and browse to wherever you unpacked the .vssettings file and select to import.  That settings file will contain only IDE color settings, and should not modify anything else, but the import wizard will give you a sort of preview of the changes before you make them, so check them out before pulling the trigger.

I’m currently using BradsDarkMonaco2008 scheme

My eyes couldn’t be happier!!  Not too mention, it gave VS2008 a much-needed facelift.

 

Finally…. Linux is free!!!

Share

Sloooooooooow down tex….. I know Linux is ‘free’.  But when I say ‘free’, I mean in the sense that it is finally free of SCO.  In case you’ve been living under a rock for several years, there has been a long and drawn-out legal battle over who owns the rights to UNIX (of which Linux has some base).  Well its finally over…. a unanimous jury verdict stated that Novell truly owns the rights to UNIX, and Novell seems to be interested in promoting it, not suing everyone for it.

It is with great pleasure that I say:  F@#K you SCO.  Go home.