Making COM Debugging less painful with OzCode

Making COM Debugging less painful with OzCode

While debugging COM objects has gotten a bit easier in Visual Studio 2010 with the addition of the Dynamic View, it still isn’t quite my idea of a picnic. One problem that kept driving me crazy is the inability to easily view collections through the on-hover datatip.

Not seeing a forest or any trees

For example, while debugging an application that automates Microsoft Word via the COM Automation model, I wanted to see the collection of open documents in Word.

Here’s what I got:

As you can see, viewing the documents in the DataTip was not very useful – I got to see how many documents there are, but no information at all about the documents themselves.

Call me Mr. X-Ray Eyes

Through the years, I developed a habit of dealing with this by doing a lot of typing in the Watch window: I would go in, write an expression that gets me the item at index 1 of the collection, then copy paste that expression and change ’1′ to ’2′, and so forth:

This was useful for looking at a few items, but once I needed to see more than a few, this approach became an eye-squinting, fingers-tiring nightmare.

Thankfully, I recently discovered that I could turn any COM collection into an IEnumerable, by using LINQ’s Cast method. I then decided to add a Custom Expression on Word’s Documentsclass, which converts it to a List<Document>, that can easily be viewed in the debugger. I used the comment syntax to give my custom expression a friendly name, “Documents List”:

Once I did this,  I was able to get at the Document objects easily:

Sadly, it is still hard to tell from this view, which document each object in the list actually represents.

So close and yet so very far away

The reason we see the documents objects this way is the unfortunate fact that COM object don’t have their own ToString() method. Because of this, .NET automatically generates a string based on the type of the object, in this case, “{Microsoft.Office.Interop.DocumentClass}”.

Luckly, with OzCode we can quickly create a more helpful display using stars.

I wanted to see next to each document, the name of the document file. Since I wasn’t sure which field contains the file name,  I just used OzCode’s search to find it, and then starred it:

And presto! I now have a way of viewing Word’s Documents collection that I can actually work with:

Comments powered by Disqus