Click here to Skip to main content
Click here to Skip to main content

ComObject Visualizer

, 20 Apr 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
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. Wink | ;-)

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)

Share

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

Comments and Discussions

 
QuestionVisual Studio 2010 PinmemberMember 86459528-Dec-11 7:20 
AnswerRe: Visual Studio 2010 Pinmemberjwize117-Jan-12 10:32 
GeneralMy vote of 5 PinmemberHristo Bojilov21-Apr-10 3:20 

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.

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