Accessing a COM add-in (again)

Add-in Express™ Support Service
That's what is more important than anything else

Accessing a COM add-in (again)
 
Hugo Ribeiro




Posts: 29
Joined: 2009-04-27
Hi.

I'm trying to create code that will use Excel.COMAddIns to get a reference to my add-in so I can access a public property available in my AddinModule.

The code goes like this:


private static Guid GetRibbonInstanceId()
{
  object ribbonInstance = Context.Current.FindRibbonAddin();
  if (ribbonInstance == null)
  {
    throw new CommunicationException();
  }

  return (Guid)Reflector.GetTypePropertyValue(
    ribbonInstance.GetType(), ribbonInstance, "InstanceId");
}

public object FindRibbonAddin()
{
  object index = RibbonServiceCommon.RibbonAddinProgId;
  AddinExpressOffice.COMAddIn addIn =
    this.ExcelApplication.COMAddIns.Item(ref index);
  if (addIn != null)
  {
    return addIn.Object;
  }

  return null;
}


GetTypePropertyValue will call this method to get the PropertyInfo from the type:


public static PropertyInfo GetTypeProperty(Type type, string propertyName)
{
  return type.GetProperty(
    propertyName,
    BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
}


The problem is that the type is System._ComObject and it's not my own AddinModule.
In fact if it were I could have followed a different (simpler) approach, casting addIn.Object to IAddinCom, which is an interface that my AddinModule implements and that includes the property I'm looking fore (no reflection required).

What is the problem? Why isn't addIn.Object of the same type of my AddinModule class?

Thanks in advance.
Posted 12 Aug, 2009 09:05:51 Top
Sergey Grischenko


Add-in Express team


Posts: 7187
Joined: 2004-07-05
Hi Hugo.

The AddinModule class is the managed object. Moreover it is created in a separate domain. So you can't cast it to the AddinModule type using the 'as' operator. That is why the addIn.Object property returns an instance of the unmanaged proxy class.
Posted 12 Aug, 2009 11:31:58 Top
Hugo Ribeiro




Posts: 29
Joined: 2009-04-27
OK. But you say in multiple threads that I can use reflection to execute public methods or properties.

But addIn.Object.GetType()... doesn't expose those public properties...

That's the original problem...
Posted 12 Aug, 2009 12:42:29 Top
Sergey Grischenko


Add-in Express team


Posts: 7187
Joined: 2004-07-05
Hugo, I can't reproduce it. Please send me a sample code.
Posted 12 Aug, 2009 12:47:37 Top
Sergey Grischenko


Add-in Express team


Posts: 7187
Joined: 2004-07-05
E.g. the code below works fine on my PC:

public string MyProperty
{
get
{
return "My Text";
}
}

private void adxCommandBarButton1_Click(object sender)
{
object index = "MyAddin48.AddinModule";

Office.COMAddIn addIn =
this.ExcelApp.COMAddIns.Item(ref index);
if (addIn != null)
{
MessageBox.Show((string)addIn.Object.GetType().InvokeMember("MyProperty", System.Reflection.BindingFlags.GetProperty, null, addIn.Object, null));
}
}
Posted 12 Aug, 2009 13:00:26 Top