65.9K
CodeProject is changing. Read more.
Home

ComObject Visualizer

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.77/5 (12 votes)

Apr 20, 2010

CPOL

2 min read

viewsIcon

27100

downloadIcon

560

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