Click here to Skip to main content
Licence CPOL
First Posted 20 Apr 2010
Views 6,843
Downloads 164
Bookmarked 12 times

ComObject Visualizer

By | 20 Apr 2010 | Article
Visual Studio Visualizer for objects whose type is System.__ComObject
CodeSnippet

ComObject Visualizer

Introduction

Recently I worked on a project which dealt with Excel and PowerPoint applications and encountered the impossibility to view type of COM object while debugging. I tried to use GetType but it returned a __ComObject which by itself doesn't contain any helpful information about the underlying interface.

I've decided to create a visualizer for Visual Studio, which helps to work with COM objects while debugging. Therefore this article will describe a way to identify supported by COM object interfaces.

Background

Below is an example of code which finds a shape in Excel and casts it to TextBox, a control of Office.Forms:

  using Microsoft.Office.Interop.Excel;
  using TextBox=Microsoft.Vbe.Interop.Forms.TextBox;

...

Shape shape = sheet.Shapes.Item("ctool");
OLEObject oleObject = (OLEObject) shape.OLEFormat.Object;
TextBox TextBox = (TextBox) oleObject.Object;

...

The code snippet is simple but I've spent much time before I found an approach. The reason for that were two issues:

  • On the internet, they advice to work with Office.Forms controls in such a way:
    Shape shape = sheet.Shapes.Item("ctool");
    TextBox TextBox = (TextBox) shape.OLEFormat.Object;

    In spite of advice, this piece of code throws “Unable to cast” exception.

  • It is not an easy way to identify underlying type of COM object in VS using only basic tools.

Using the Code

The main idea of type identification is connected with IUnknown interface. This interface is supported by all COM objects and has QueryInterface method. To identify supported interface is needed to iterate through all currently loaded interfaces and query if COM object supports it. Below is the source code for ComObjectSource class which performs it.

  public class ComObjectSource : VisualizerObjectSource
  {
    public override void GetData(object target, Stream outgoingData)
    {
      Assembly[] assemblies = Thread.GetDomain().GetAssemblies();
      List<type> types = new List<type>();

      foreach (Assembly assembly in assemblies)
      {
        types.AddRange(
          FindInterfaceTypesForComObject(assembly, target));
      }

      new BinaryFormatter().Serialize(outgoingData, types);
    }

    public IEnumerable<type> FindInterfaceTypesForComObject
			(Assembly Assembly, object ComObject)
    {
      IntPtr iuknw = Marshal.GetIUnknownForObject(ComObject);

      foreach (Type type in Assembly.GetTypes())
      {
        Guid uid;

        if (!type.IsInterface ||
            (uid = type.GUID) == Guid.Empty)
        {
          continue;
        }

        IntPtr ipointer = IntPtr.Zero;
        Marshal.QueryInterface(iuknw, ref uid, out ipointer);

        if (ipointer != IntPtr.Zero)
        {
          yield return type;
        }
      }
    }
  }

To register a visualizer for System.__ComObject types, you need to add the following:

[assembly: System.Diagnostics.DebuggerVisualizer(
typeof(Visualizer), 
typeof(ComObjectSource), 
TargetTypeName = "System.__ComObject, mscorlib, 
	Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089", 
Description = "ComObject Visualizer")]

In order to use this visualizer, you need to drop *.dll file in <Visual Studio Install Dir>\Common7\Packages\Debugger\Visualizers or <My Documents Dir>\Visual Studio 2005\Visualizers.

Points of Interest

I no longer spend more time with type identification of COM objects. So I wish the same thing to you. ;-)

History

  • [21.02.2008] - Initial publication of the article

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Volodymyr Goncharov

Software Developer
Aspeo
Germany Germany

Member

Currently I am working as C#/ASP.NET developer at Aspeo.
Volodymyr's professional profile at LinkedIn

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionVisual Studio 2010 PinmemberMember 8645956:20 28 Dec '11  
AnswerRe: Visual Studio 2010 Pinmemberjwize19:32 17 Jan '12  
GeneralMy vote of 5 PinmemberHristo Bojilov2:20 21 Apr '10  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 20 Apr 2010
Article Copyright 2010 by Volodymyr Goncharov
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid