There is a similar piece of code on my site at:

http://www.markrowlinson.co.uk/articles.php?id=9

It derives from the very same thread but doesn't use any API's. Could be identical to Ivan's really! Just thought I'd post the link to see if it sheds any light on your problem....