Click here to Skip to main content
13,089,209 members (65,159 online)
Click here to Skip to main content
Add your own
alternative version


15 bookmarked
Posted 20 Apr 2010

ComObject Visualizer

, 20 Apr 2010
Rate this:
Please Sign up or sign in to vote.
Visual Studio Visualizer for objects whose type is System.__ComObject

ComObject Visualizer


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.


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)
        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)

      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(
TargetTypeName = "System.__ComObject, mscorlib, 
	Version=, 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. ;-)


  • [21.02.2008] - Initial publication of the article


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
Currently I am working as C#/ASP.NET developer at Aspeo.
Volodymyr's professional profile at LinkedIn

You may also be interested in...

Comments and Discussions

QuestionVisual Studio 2010 Pin
Member 86459528-Dec-11 6:20
memberMember 86459528-Dec-11 6:20 
AnswerRe: Visual Studio 2010 Pin
jwize117-Jan-12 9:32
memberjwize117-Jan-12 9:32 
GeneralMy vote of 5 Pin
Hristo Bojilov21-Apr-10 2:20
memberHristo 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.

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