Click here to Skip to main content
15,880,972 members
Articles / Programming Languages / C#

ComObject Visualizer

Rate me:
Please Sign up or sign in to vote.
4.77/5 (12 votes)
20 Apr 2010CPOL2 min read 26.8K   556   15   3
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:

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

...

C#
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:
    C#
    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.

C#
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:

C#
[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)


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

Comments and Discussions

 
QuestionVisual Studio 2010 Pin
Member 86459528-Dec-11 6:20
Member 86459528-Dec-11 6:20 
AnswerRe: Visual Studio 2010 Pin
jwize117-Jan-12 9:32
jwize117-Jan-12 9:32 
GeneralMy vote of 5 Pin
Hristo-Bojilov21-Apr-10 2:20
Hristo-Bojilov21-Apr-10 2:20 

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

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