Friday, December 31, 2010

ISO/FIPS, FIPS/ISO Country Code Conversion Classes

I've just completed a simple pair of static C# classes to convert between ISO (3166-1-alpha-2) and FIPS (and vice versa). You can access the source code using Subversion or just browse it here There are of course many ways to do it but the emphasis here is on speed.

Saturday, November 13, 2010

Delphi: Get system icon handle

Delphi function to retrieve the icon handle of a system resource. 

uses ShlObj;
function GetSystemIcon: HICON; 
  FileInfo: TSHFileInfo;
  PIDL: PItemIDList;
  SHGetSpecialFolderLocation(Application.Handle, CSIDL_DRIVES, PIDL);
  if SHGetFileInfo(PChar(PIDL), 0, FileInfo, SizeOf(TSHFileInfo),
        Result := FileInfo.iIcon;
Constants defined in ShlObj.pas

  CSIDL_DESKTOP                       = $0000; // 
CSIDL_INTERNET                      = $0001; //Internet Explorer (icon on desktop)
  CSIDL_PROGRAMS                      = $0002; //Start Menu\Programs
  CSIDL_CONTROLS                      = $0003; //My Computer\Control Panel
  CSIDL_PRINTERS                      = $0004; //My Computer\Printers
  CSIDL_PERSONAL                      = $0005; //My Documents
  CSIDL_FAVORITES                     = $0006; //user\Favorites
  CSIDL_STARTUP                       = $0007; //Start Menu\Programs\Startup
  CSIDL_RECENT                        = $0008; //user\Recent
  CSIDL_SENDTO                        = $0009; //user\SendTo
  CSIDL_BITBUCKET                     = $000a; //user\Recycle Bin
  CSIDL_STARTMENU                     = $000b; //user\Start Menu
  CSIDL_DESKTOPDIRECTORY              = $0010; //user\Desktop
  CSIDL_DRIVES                        = $0011; //"My Computer" folder
  CSIDL_NETWORK                       = $0012; //Network Neighborhood (My Network Places)
  CSIDL_NETHOOD                       = $0013; //user\nethood
  CSIDL_FONTS                         = $0014; //windows\fonts
  CSIDL_TEMPLATES                     = $0015; //Templates folder
  CSIDL_COMMON_STARTMENU              = $0016; //All Users\Start Menu
  CSIDL_COMMON_PROGRAMS               = $0017; //All Users\Start Menu\Programs
  CSIDL_COMMON_STARTUP                = $0018; //All Users\Startup
  CSIDL_COMMON_DESKTOPDIRECTORY       = $0019; //All Users\Desktop
  CSIDL_APPDATA                       = $001A; //user\Application Data
  CSIDL_PRINTHOOD                     = $001B; //user\PrintHood

Wednesday, November 3, 2010

System.Data.Sqlite locks database, even on single threaded access

Sometimes while using the System.Data.Sqlite .Net library I noticed that when db access was finished with and the method doing the access had returned, there were still open file handles to the database file. Every subsequent access created a new perpetual file handle. I have found a way that fixes this, that works for me anyway ;-) ...

Wednesday, August 18, 2010

Strange error message when opening Outlook

Today i got a strange error message when opening ms outlook.

"Spool driver not loaded"

and in a multi-line text field below it said:


Spool driver wha?

That's the last straw for me, I'm moving to Thunderbird for work email. I'm just lucky that we're allowed to choose our own software (within limits of course).

Friday, August 13, 2010

Oracle sues Google over Android

Android robot logo.Image via WikipediaSo Oracle has finally let the mask slip and have decided to sue Google over some perceived Java-related patent violation in it's Android operating system. If I ruled Google I'd put the wheels in motion and start to remove Java from my toolchain and list of requirements for development with Google App Engine. Google is the only thing keeping Java relevant on the internet today. Way to kill Java Oracle.

Monday, April 19, 2010

.Net Color To HTML Color And Back

Color translation between the .Net color type and the #rrggbb color type is made really easy using the System.Drawing.ColorTranslator class. The calling code itself is simple:

    public string ColorToHtmlStr(System.Drawing.Color c)
        return System.Drawing.ColorTranslator.ToHtml(c);

    public System.Drawing.Color HtmlStrToColor(string HtmlColor)
            return System.Drawing.ColorTranslator.FromHtml(HtmlColor);
            //Return a default if the HTML color string parameter is invalid
            //The value of the default could be passed as a second parameter.
            return System.Drawing.Color.Black;

Wednesday, April 14, 2010

Cryptographically Strong Random Number Generation With C#

In C# it's sometimes handy to be able to generate usable random numbers whether its for collision avoidance, security, generating realistic dummy data or just testing inputs. The problem is that using the built in .Net Random class several times in quick succession, although fast, will give you identical results for a given unit of execution time (assuming you are seeding with a time value, which most people seem to do).

Tuesday, April 6, 2010

14 TV Shows That Jumped the Shark | Real TV Addict

Nice post at that lists 14 TV Shows That Jumped the Shark.

Pity about SVU, always liked that show but it needs to either end or get new writers. C'est la vie.

Monday, March 8, 2010

Polaris (and my tree)

A photo of Polaris (700s exposure, ISO 400) at the center of the star trail. Maybe I overdid the color but it still looks good.

Tuesday, February 23, 2010

Orion in Late Winter

Orion is in descent now so I took a few shots before he bails completely. I took this with my EOS 400D using the standard kit 18-55 mm lens fully zoomed out with a roughly 2 min exposure time. Even with some Gimp editing the light pollution is evident, damn sodium street lights. The Orion Nebula comes through as slightly purple in the full size image which is nice.

I should probably take a shot without my house and tree in the frame but I like the composition.

Wednesday, February 3, 2010

Is Steve Jobs Slowing Down?

The iPad seems like such a crappy lazy device it struck me that, as Steve involves himself with every detail of every device, it follows that each new device is a reflection of his energy and involvement. By that measure poor Steve is slowing down or losing his touch.

Its an unpopular viewpoint to express on sites like Digg and Reddit, but given that he sat out the 2nd half of the iPad unveiling event its no secret that he's not 100%.

Get well soon Steve!

C#: Padding an Integer with Leading Zeroes

I find this method useful sometimes when displaying time of day, receipt codes or other numerical data. It takes an integer and a width and returns a zero-padded string representation of the integer. It's a simple method yeah but I've seem implementations on the web where people assemble a string using a while loop with a StringBuilder, and sometimes just using the + operator, disgusting.

Example: To get "00053" from the integer 53, call PadIntToStr(53, 5)

public static string PadIntToStr(int i, int maxWidth)
            bool ltz = i < 0;
            int diffLen = maxWidth - i.ToString().Length;
            string pole = string.Empty;

            if (diffLen <= 0)
                return i.ToString();

            if (ltz)
                i = Math.Abs(i);
                pole = "-";

            return string.Format("{0}{1}{2}", pole, new string('0', diffLen), i);


Tuesday, February 2, 2010

Dubai's World Island Sinking?

The Dubai World Island archipelago is sinking, according to NASA. Needless to say there are many financial institutions and economy commentators that deny this, possibly fearing a further massive devaluing of their island property assets.

I've cropped the NASA photo and overlaid it against a photo from the end of 2007. It's obvious that a lot of erosion is taking place there. Notice that the two island clusters off the west of North and South America have disappeared and island chain that makes up Alaska and the northern coast of Canada has merged into one long island. All this in less than 3 years!
Enjoy it while is lasts guys.