PC Woes

Just as I had been smugly defending Windows as a stable OS the envitable happended. I started the machine and got the dreaded "cannot find system" registry corrupt error.  After about 30 mins of trying to recover it I finally gave up and started the re-install of XP. "Ok I’ll lose some programs and drivers but I guess that won’t take too long to reinstall". No, for some reason XP setup was convinced the disk wasn’t formatted so I had to lose everything off that disk…grrr. I could have copied files of using recovery mode but my mouse finger was quicker than my brain. Anyway the system is up and running again and thanks to this blog I managed to get all the various AMD dual core drivers back and install the Adobe upgrade suite.
 
At the same time I had just purchased 4x1GB sticks of performance RAM, luckily I hadn’t put them in otherwise I’d be blaming them for the system crash. So I thought this would be the ideal time to install them, afterall I can only lose nothing now! "Easy job", I foolishly thought. Opening up my case I suddenly realised I didn’t know if I was supposed to install matched RAM in the same bank (next to each other) or the same colour (every other slot). After a bit of hunting around I’m pretty certain it’s the same colour. So I pressed on, only to discover that the nice quick-release memory clips couldn’t release because they couldn’t get past my graphic card! Honestly these motherboard manufacturers, do they ever try their own kit out? Luckly by removing the quick-release retention clip on the graphics card there was enough give in the card for me to open the memory clips to get the sticks out (and put the new ones in). "Ok let’s see if that worked…booting…BIOS screen…4GB (good, was 2GB)…Windows XP…System…3GB". What, where is my extra 1GB? Then it hit me like a bus, XP is 32-bit it not going to use 4GB is it. Grr, I’m so used to Windows Server I’d forgotten I run XP at home :(. Not even /pae switch could persuade it. Oh well, maybe I’ll get around to putting Vista 64 on it soon.

Setup.EXE bootstrap for WiX and/or msi

Creating msi files using WiX is normally great, however one thing that is terrible is when you want your users to upgrade a product. Rather than simply selecting the msi they are required to issue

MSIEXEC /i My.msi REINSTALL=ALL REINSTALLMODE=vomus

Not exactly user friendly. So it was time to create a nice setup.exe bootstrap to detect when an upgrade was needed and issue the horror statement. A quick tour around the Windows SDK brought up the their sample. Written in Win32/C++ it seemed to be over complicated especially since I know the product is only ever going onto machines with .net pre-installed. So I wrote the following little csharp console code that does the trick for my requirements.

 

static

void Main(string[] args)

{

Type componentType = Type.GetTypeFromProgID("WindowsInstaller.Installer");

if (componentType == null)

{

Console.WriteLine("Windows Installer is either not installed or could not be located");

return;

}

object componentInstance;

try

{

componentInstance =

Activator.CreateInstance(componentType);

}

catch (Exception e)

{

Console.WriteLine("Windows Installer is either not installed or could not be located: " + e.Message );

return;

}

 

object returnType = componentType.InvokeMember("Products", BindingFlags.GetProperty, null, componentInstance, null);

IEnumerable stringList = (IEnumerable)returnType;

bool foundProduct = false;

foreach (string product in stringList)

{

if (product == "{YOURPRODUCT_GUID}")

{

foundProduct =

true;

break;

}

}

if (foundProduct)

{

System.Diagnostics.

Process.Start(@"msiexec",

@"/i My.msi REINSTALL=ALL REINSTALLMODE=vomus");

}

else

{

System.Diagnostics.

Process.Start(@"msiexec", "My.msi");

}

}

 

Optimizing Virtual PC

I rebooted my machine at work and had a quick browse through the BIOS settings looking for ways to optimize the disk. Whilst changing those settings I spotted another called Hardware Virtualization. Turns out that most modern processors from AMD and Intel support a special set of instructions that allow virtual machine software to bypass parts of the emulated layer and go straight to the processor. However, for Intel processors these are typically off by default. So if you use application like Virtual PC then turn this on in both your BIOS and the VPC settings and enjoy a small, but real, performance boost.

Fun with Enums

One of the seemingly lesser known features of Enums is their ability to convert to and from the name rather than the value, so I thought I’d do a quick refresher.

Consider the following Enum…
Medal.Bronze = 0
Medal.Silver = 1
Medal.Gold = 2

The majority of cases people leave it as an int (can be any non-char integral type). However, what happens when you want to store/persist the type? If I was to save this to a disk file I might choose to use XML and have a result like…
<Medal>2</Medal>
Then when I load the XML back I can easily convert it back…
Medal medal = (int)Convert.ToInt32("2");
However, I don’t actually like this, the value of 2 has no real meaning to the reader, I’d much prefer to see
<Medal>Gold</Medal>
Well it is possible. To save the name rather than the value you can use ToString and a format string…
medal.ToString("G");
Reading the name back into the enum uses the often overlooked Parse method…
Medal medal = (Medal)Enum.Parse(typeof(Medal), "Gold");
Admittedly this doesn’t solve localization problems of having XML, "human readable" but I prefer it.

Constellation of Features

Back in June I attended @Media2007 (atMedia2007) I was impressed by a talk given by Jesse James Garratt. He used the interesting phrase, "a constellation of features". What I found doubly interesting with the phrase is the number of interpretations it has depending on who I talk to…
1. Like a star field, here are so many stars that you have trouble singling out anything interesting
2. Like the ‘bigger dipper’, ‘the archer’, etc it is supposed to show or signify something to one person but to others it is impossible (or very hard) to recognise
3. A group of related features

In the talk the phrase was in the context of applications like Word where there are so many things to choose from that the user is simply bewildered and will find it difficult to focus on the task they wish to complete, i.e. (1). But I rather like the other definitions too, especially (3) which conveys the opposite message.

Query Analyser vs. SQL Management Studio

I’m a big fan of using SQL Server 2005’s Management Studio for writing my TSQL regardless if my database is SQL 2000 or 2005. However, I recently hit a strange problem. I’d written a number of scripts to automate a very dull tasks and published it to my colleagues. However, whenever they ran the script they got lots of errors complaining about incorrectly terminated strings. The only thing we could see that was different was I was using SQL Management Studio and he was using Query Analyser. Once we both used Management Studio it worked fine. I’m quite surprised by this since I always thought they’d simply send the text in their editor to SQL. But obviously that isn’t true, looks like some sort of parsing or transforming is going on. Very odd. If I have time I’ll get a network sniffer on the job but for now I’ll simply issue this warning…you can’t assume the scripts you write in Management Studio will work in Query Analyser.

SQL Server Dynamic Help

I guess the title says it all! Did you know that, like Visual Studio, SQL Server Management Studio also has Dynamic Help? Help->Dynamic Help.

Remote Desktop Clent 2.0 for the Mac

Downloaded the Beta of Remote Desktop Client 2.0. It is loads better than the previous one. A quick tip, if you want to use console mode then simply type /console after the name of the machine, i.e.

MyRemoteMachine /console

It hasn’t been without problems, currently I only get a blank screen after a tried to start in full screen mode. I’ll see what happens after a reboot.
[Edit] Setting the colour depth down and then back-again seemed to stop that problem.

Javascript, scope fun with the for loop

I thought I’d share a bit of JavaScript or a little Got-Me, consider the following;

function A()
{
  for (index = 0; index < 10; index++)
  {
       alert(index);
       B();
       alert(index);
  }
  alert(‘finish’);
}

function B()
{
  for (index = 0; index < 10; index++)
  {
       // do nothing
  }
}


I ran this on Firefox/Safari and got….

0,9, finish
I was surprised on two counts, 1. Why didn’t the for loop work? 2. Why the shared scope? Anyway assuming I had accidentally created a global I corrected it and it worked as expected…

for (var index = 0; index < 10; index++)

Xml and XPath on the client via JavaScript

This weekend, "I’ve been mostly…", looking at how to process XML in a
client browser using JavaScript. Normally I wouldn’t entertain the
thought but with the recent push of AJAX and various cross-browser
script libraries I thought I’d take another look. NB. This doesn’t
consider JSON, I’ll post on that option later.

XmlDocument

The
core requirement to most XML processing (ignoring SAX style processing)
is the document. AJAX has helped here and it seems that the majority of
browsers implement some form of Dom but they do suffer from
inconsistencies.

HttpRequest

Probably the most
reliable object is  HttpRequest, which is a necessity for AJAX. There
is a cross-browser issue but it’s fairly simple to solve…
if(window.XMLHttpRequest && !(window.ActiveXObject))…
    httpRequest = new XMLHttpRequest();

if(window.ActiveXObject)
    httpRequest = new ActiveXObject("Msxml2.XMLHTTP");

after
that the objects seem to use the same API. There does seem to be some
drawbacks with this approach,  you need a web site (or service) to get
the XML from and the lack of XPath support.

XmlDocument revisited

"But
I don’t want to fetch my XML from a site, I created it on the client",
I hear you cry. Well most browsers support the following…
if (document.implementation && document.implementation.createDocument)…
    xmlDoc = document.implementation.createDocument("", "", null);

if (window.ActiveXObject)
    xmlDoc = new ActiveXObject("Microsoft.XMLDOM");

similar
in nature to the previous cross-browser test. These have good DOM
support and you can create/append elements as you’d expect. The load
method seemed a bit flaky.  Now this maybe my code so I’ll hold off
saying it doesn’t work…but it didn’t seem to! But creating the DOM
manually seemed fine so it is still useful, especially for sending data
back. However, the biggest issue seems to be XPath

XPath

XPath is a very useful API for searching XML but its support in
browsers is very patchy. Mozilla based browsers offer a good implementation…
var paragraphCount = document.evaluate( ‘count(//p)’, xmlDocument, null, XPathResult.ANY_TYPE, null );

and it includes NodeIterator for a selectNodes style enumerating.
Microsoft has the more usual (I’m an MS developer) SelectNodes, again
very easy to abstract out the difference in API. However…Safari. The
Konqueror based browser simply doesn’t like XPath. So what to do? I
considered writing my own query engine but I don’t really want to waste
my time re-inventing the wheel. So I had a look for a script library,
but it seems like XPath libraries and are still in their infancy. Of
the ones out there XML for SCRIPT
looked promising but still not 100%. At this point I realised that for
my specific needs I could get away with a bit of DOM walking so I left
it there.

Conclusion

My conclusion is that XML support in the browser is ok if you base your
work around HttpRequest. But if you want to do XPath then look for an
alternative.