Sunday, April 28, 2013

Traversing JavaScript objects in .NET

I’ve been playing around with hosting a Google Map in a .NET WinForms control, for no good reason other than to see if it can be done. The key part of this integration is calling JavaScript on a web page from .NET. This appears reasonably straightforward, the WebBrowser control has a Document.InvokeScript method that does the trick. If your JavaScript function returns a value, you can pick this up from the return value of InvokeScript.

This works great for simple types, but what if your JavaScript function returns a more complex type? This is where things get a bit more tricky. The returned object has a type of System.__ComObject and initially it looks like this has no useful methods on it. This is the object that is used when calling any COM object, but generally you’ll be able to import a type library to create a .NET friendly wrapper around the raw object. This obviously isn’t the case here.

So my first thought was to find the real underlying type of the __ComObject. This piece of code helped out here. It turns out the actual underlying type was a JScriptTypeInfo but there’s very little information out there about what this type does or how to use it.

But it turns out there’s a much simpler way to access the returned object, cast the returned object as IReflect and use its methods to get properties etc. So the code for my .NET property looks like this

    [Category("Map")]
    public LatLng Centre
    {
      get 
      {
        object centre = webBrowser.Document.InvokeScript("getCentre");
        if (centre == null)
          return new LatLng(0, 0);

        IReflect reflect = centre as IReflect;
        double lat = (double)reflect.InvokeMember("lat", BindingFlags.InvokeMethod, null, 
          centre, null, null, null, null);
        double lng = (double)reflect.InvokeMember("lng", BindingFlags.InvokeMethod, null, 
          centre, null, null, null, null);

        return new LatLng(lat, lng);
      }
      set
      {
        webBrowser.Document.InvokeScript("setCentre", 
          new object[] { value.Latitude, value.Longitude });
      }
    }

Another approach to this would be to have two JavaScript functions, getCentreLat and getCentreLng, which just return simple types, but that could get cumbersome with really complex types.

I’m not sure if it’s possible to pass complex objects into JavaScript but I haven’t needed that yet. Again, for really complex types, passing in each part of them could get messy.

A dog’s life

WP_000028

It’s a hard life for a dog. It all starts when the sun comes up. “I must inform my master that the sun has come up. Woof woof…”.

Some time later “… woof, woof. Ah good, my master is up to see that the sun has come up, I can now go back to sleep”. Thank you dog!

Wednesday, April 24, 2013

Around the world in 7 years

According to Endomondo, I’ve cycled 2668 miles since I signed up with them, which is just over a tenth of the distance around the world. According to my circle drawing page, this would take me from my house to the North Pole. I’m quite pleased with that.

The distance round the earth is about 24,900 miles, depending on how you measure it. I try to cycle 300 miles a month, so the time it will take me to cycle the whole way round the earth is 24,900/300 which is 83 months, or 6.9 years. So in about 6.1 years I’ll have completed my virtual trip. I’m hoping Endomondo is still around to record my achievement!

Tuesday, January 29, 2013

404s and Google Webmaster Tools

Trying to fix all the things Google Webmaster Tools complains about would drive you insane. Case in point, it will warn you about soft 404s, which are URLs that don’t exist (or contain no useful content) but don’t return a 404 HTTP error code.

I had a few of these on my site where a rebuild of the postcode database meant a few old postcodes had gone missing (here’s one). Although they weren’t any links to them from anywhere, Google never forgets about pages it’s crawled. I guess I could ask Google to remove all these URLs from its index, but I have a life.

So I started returning 404 errors from these pages, hoping it would make Webmaster Tools happy and make the web a better place. Then a few days later I got a warning message from Google telling me there had been an increase in not found errors on my site. Duh, well yeh, because that’s what you told me to do.

So all I’ve managed to do is move my errors from the soft 404 bucket to the not found bucket. Ho hum. It seems like keeping Webmaster Tools happy is actually impossible.  

Sunday, January 27, 2013

The quickest way to charge a Nokia Lumia 800

I can’t say I’ve ever considered the differences between charging my Nokia Lumia 800 via USB and via the AC adaptor. If I had thought about, I would probably have assumed they charge at just the same rate. But this morning when I was thinking about going on a bike ride, I realised my phone was out of juice, so wanted to charge it ASAP. A little research showed using the AC adaptor would be much quicker than plugging into USB.

But in case you don’t believe me (And to be honest not believing random people on the internet is probably a good thing), you can check it out for yourself. From the phone dialler, type ##643# and the diagnostics tool will be installed. Run this app and look at the battery status section whilst charging. I found the AC adaptor was providing about twice as much power as the USB connection.

Friday, January 04, 2013

Post Office Recorded Signed For–Mmm, not so much

I needed to send my driving licence off to a company to prove I am who I say I am. Something to do with money laundering, although I’m not entirely convinced sending them my driving licence proves I’m not a criminal. Maybe I’m just a money launderer who can drive?

Anyway, wanting some peace of mind, I decided to use the Post Office’s ‘Recorded Signed For’ service which has “guaranteed signature on delivery” according to their website. That was on 17th December. Over 2 weeks later, when I try and track my package, the Post Office website tells me

Item BY260667256GB was posted at 46 Hawks Road KT1 3EG on 17/12/12 and is being progressed through our network for delivery.

The thing is I know this isn’t true, since the driving licence arrived back at my house a few days ago. So much for a guaranteed signature on delivery. I wonder if the status of my item will get updated to

Item BY260667256GB was posted at 46 Hawks Road KT1 3EG on 17/12/12 and we haven’t got an effing clue where it is.

Friday, November 23, 2012

Endomondo discovers how to get my money

When I logged into Endomondo the other day I discovered that in the process of redesigning their website, they had also decided to hide some of the useful stats from us users who weren’t paying them any money.

I could have started shouting and screaming at this point, since they’d had the temerity to take away my free toys. But frankly $20 a year for a very useful service is a price worth paying if it helps them to keep the lights on.

Of course this approach only works for sites that offer a valuable service, it probably wouldn’t work for quite a few sites (I’m looking at you Facebook!)

Wednesday, November 21, 2012

Windows 8 – How bad are these apps?

For most of my time I’ve been using Windows 8 much as I used previous versions of Windows and stayed well away from the new interface and apps. But I’ve recently started playing with some of the apps and frankly it’s all quite disappointing.

For a start, every app seems to take an age to fire up. This makes no sense, I have a reasonably specced laptop and yet apps take longer to load up than on my phone, even though the apps do the same thing! I really hope this is a problem with the apps, rather than the underlying platform, since at least the apps can get updated quite easily.

The other problem is some of these apps are pretty buggy. We’ve been playing solitaire on our Windows PCs for over twenty years and I can’t remember it ever crashing before, but it’s just happened about 5 times in the space of a few minutes. Ah well, maybe one of those updates advertised in the store is for solitaire? Hard to say since the store keeps telling me I’m not connected to the internet, even though I definitely am. And when I do manage to access it, I can’t actually get to the updates.

I’m not sure the good folk at Apple will be quaking in their boots at the moment.