Archive for May, 2010

Book Review: Proust Was a Neuroscientist

May 30th, 2010

I recently purchased this book based on some of positive reviews I had read on Amazon.com and elsewhere, and because I read Mr. Lehrer’s blog. I now wish that I had followed the negative reviews. Almost immediately it seemed that Mr. Lehrer was stretching various scientific evidence and theories very thin to match up to his thesis that some artists predicted scientific breakthroughs (such as Proust). I tried to dismiss these instances as artistic fancy, but when the author including information about DNA that is 10 to 15 years out-of-date and flat out wrong, I just could not continue. If Mr. Lehrer is going to compare hard science with art then he should have researched the fields about which he writes, or had better fact-checking of the book.

I ultimately quit this book because while I read a lot about science, and was able to cross-check that things in this book were wrong according to what experts in the field wrote, I am not a scientist nor am I a literary scholar. If Mr. Lehrer is misrepresenting, lying, or simply so bad at science writing that he is giving me bad information, how am I to know? Long story short: Mr. Lehrer lost my trust. I read books by good authors, that are often scientists, to learn more about the world. I *trust* that their professional credentials mean that they will provide me with accurate information. I don’t need to unintentionally poison my mind with outright incorrect information.

COM: Everytime I think I am out, it pulls me back in. (Silverlight + DLR)

May 24th, 2010

Silverlight 4 introduces some nice functionality on top of the Out Of Browser (OOB) capabilities introduced in Silverlight 3.  The application can now be granted a higher level of trust which allows it to operate outside the Silverlight sandbox file system, and to interact with COM.  To test the capabilities of this new functionality I immediately tried to take it file system access far beyond the sandbox; I wanted to enumerate all of the drives on the client machine.  I quickly ran into a wall.  Silverlight does not expose access to drives through its IO libraries.  Why?  I read several answers to this question, none of which satisfied me, especially given the fact that the app can simply access these drives through COM!

Luckily .Net 4.0 / Silverlight 4.0 / C# 4.0 comes with the Dynamic Language Runtime (DLR).  I had previous used the new dynamic pseudo-type that ties into the DLR to call Iron Python.  I figured I would see if I could use this same technique to call into COM. Back in ancient times (circa 1990s) I called into COM all the time from VB and ASP. I knew the libraries were all there to do what I needed to do.

I was not surprised to find I wasn’t the first person down this path, and quickly encountered multiple articles and postings about working with the file system via COM this exact way.  With the knowledge that it was possible I plowed forward.

To access COM from Silverlight one uses the AutomationFactory to instantiate a COM library.  I wanted to use the good ol’ Scripting.FileSystemObject.  In my test case I had a Silverlight app running locally OOB with elevated privileges.  This Silverlight app has a single list box which will display the drive letters once I can get access.  Via COM, the code looks like this:

dynamic fs = AutomationFactory.CreateObject("Scripting.FileSystemObject");

foreach (dynamic drive in fs.Drives)
{
  filesListBox.Items.Add(drive.DriveLetter);
}

Its really that simple. The dynamic keyword and COM do all the heavy lifting. The most work I had to do was lookup the object model to use in the FSO, as no intellisense is available. The runtime does late binding onto these properties via the DLR. This did a nice end-route around the missing Silverlight IO libraries and provided me with a nice list of drives.