Compare version strings with InnoSetup

I recently had to implement version string comparison for InnoSetup, because the solutions that I found on the internet did not suit my needs. Hopefully this will be useful for somebody else.

// Procedure to split a string into an array of integers 
procedure Explode(var Dest: TArrayOfInteger; Text: String; Separator: String);
  i, p: Integer;
  i := 0;
    SetArrayLength(Dest, i+1);
    p := Pos(Separator,Text);
    if p > 0 then begin
      Dest[i] := StrToInt(Copy(Text, 1, p-1));
      Text := Copy(Text, p + Length(Separator), Length(Text));
      i := i + 1;
    end else begin
      Dest[i] := StrToInt(Text);
      Text := '';
  until Length(Text)=0;

// Function compares version strings numerically:
//     * when v1 = v2, result = 0  
//     * when v1 < v2, result = -1  
//     * when v1 > v2, result = 1
// Supports version numbers with trailing zeroes, for example 1.02.05.
// Supports comparison of two version number of different lengths, for example
//     CompareVersions('1.2', '2.0.3')
// When any of the parameters is '' (empty string) it considers version number as 0
function CompareVersions(v1: String; v2: String): Integer;
  v1parts: TArrayOfInteger;
  v2parts: TArrayOfInteger;
  i: Integer;
  if v1 = '' then
    v1 := '0';

  if v2 = '' then
    v2 := '0';

  Explode(v1parts, v1, '.');
  Explode(v2parts, v2, '.');
  if (GetArrayLength(v1parts) > GetArrayLength(v2parts)) then
    SetArrayLength(v2parts, GetArrayLength(v1parts)) 
  end else if (GetArrayLength(v2parts) > GetArrayLength(v1parts)) then
    SetArrayLength(v1parts, GetArrayLength(v2parts)) 
  for i := 0 to GetArrayLength(v1parts) - 1 do 
    if v1parts[i] > v2parts[i] then
      { v1 is greater }
      Result := 1;
    end else if v1parts[i] < v2parts[i] then
      { v2 is greater }
      Result := -1;
  { Are Equal }
  Result := 0;

To test the code in InnoSetup, you can use the following function:

procedure TestVersions(v1: String; v2: String);
  Log(v1 + ' : ' + v2 + ' = ' + IntToStr(CompareVersions(v1, v2)));

And call it with some parameters:

  TestVersions('1', '2');
  TestVersions('2', '1');
  TestVersions('3', '3');
  TestVersions('1.1', '1');
  TestVersions('2.1', '1');
  TestVersions('1.1', '2');

  TestVersions('2.12.11', '2.12.25');
  TestVersions('', '2.12.25');
  TestVersions('2.12.25', '');
  TestVersions('', '');
  TestVersions('2.12.11', '2.012.11');

A practical example can be used as follows:

if CompareVersion(currentVersion, newVersion) = -1 then
  //do the upgrade

Visual Studio 2013 hangs after each debug session

Stumbled upon this issue when Visual Studio 2013 Update 4 would hang after each debug session. Very annoying especially when you want to quickly run the application test something, fix code and back to the beginning of this cycle. The hanging would occur for about 15-30s every time I would close the application and debug session ends. To resolve this I tried a lot of solutions suggested by Google which didn’t help:

  • Resetting Visual Studio 2013 settings via “devenv.exe /ResetSettings”
  • Deleted *.suo file
  • Rebuilt new solution file with all projects
  • Even disabled internet as I was suspecting some sort of network communication issue was causing this
  • Attempted to disable git integration with Visual Studio by renaming .git folder to _git as a temporary solution
  • Tried to disable IntelliSense with no luck

The only solution that worked for me was to disable Visual Studio host process for the project. To do this on the Project menu, click Properties, then click the Debug tab and uncheck the Enable the Visual Studio hosting process check box.

Hope this post saves a few hours messing around with settings for somebody trying to resolve this issue.

Problems with Samsung Series 5 Ultrabook and USBTinyISP

Well, this is somewhat disappointing… I recently acquired a Samsung Series 5 530U3B Ultrabook which is really nice and was going to make my main laptop for on the work on the go. Unfortunately it supplies insufficient power to my USBTinyISP programmer. The effect is that when I try to program ATTiny45, I get this error:

Binary sketch size: 2,752 bytes (of a 4,096 byte maximum)
avrdude: verification error, first mismatch at byte 0x0040
 0x02 != 0x36
avrdude: verification error; content mismatch

The verification fails on random memory addresses. This means that I will need to add an external power supply to my custom programmer or find a USB hub with external power supply. I think that the latter will be the easiest.

Notification icon in Ubuntu with Unity and Mono C# example

Ubuntu Unity changed the way notification icons are handled and StatusIcon object no longer works for Mono and C#. In order to make it work you have to use ApplicationIndicator object with newer version of Ubuntu. It seems easy enough when you know how to do it, but I couldn’t find a full example anywhere on the internet. Here is one now.

Continue reading “Notification icon in Ubuntu with Unity and Mono C# example”