ComInterfaceType.InterfaceIsIDispatch
(1)
ComInterfaceType.InterfaceIsDual
(1)
Visual Studio
(1)
RequestComAddInAutomationService
(1)
BindingFlags.InvokeMethod
(1)
Office
(1)
VSTO
(1)
StandardOleMarshalObject
(1)

Add-in Event handler assignment failes in late binding

Asked By Avishay Ben-Zvi
05-Aug-09 05:01 AM
Hello,

I wrote an excel VSTO application that includes two projects (in the same
solution). Add-in project and a Template project.
In the Add-In I added an event that will be registered by the template as
followed:

// Delegate for the event.
[ComVisible(false)]
public delegate void MyEventDelegate(object sender, EventArgs e);

//Interface to the exposed COM.
[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[Guid("37ea216c-8c17-43e5-a7a7-6ee426f261b3")]
public interface IMyObject
{
void eventDelegate(string sMessage);
}

// Event interface
[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface IMyObjEvent
{
[DispId(1)]
void MyEvent(object sender, EventArgs e);
}

// Finally... The object itself
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
[Guid("37ea216c-8c17-43e5-a7a7-6ee426f261b4")]
[ComSourceInterfaces(typeof(IMyObjEvent))]
public class MyObj :
StandardOleMarshalObject, IMyObject
{
public event MyEventDelegate eventDelegate;

internal void FireEvent(object sender, EventArgs e)
{
if (eventDelegate != null)
{
eventDelegate(sender, e);
}
}


public void MyFunc(string sMessage)
{
MessageBox.Show("Client passed " + sMessage);

}
}

In ThisAddin.cs I overrided RequestComAddInAutomationService in order to
receive instance of the object.

private MyObj m_oMyObj = null;
protected override object RequestComAddInAutomationService()
{
if (m_oMyObj == null)
{
m_oMyObj = new MyObj();
}
return m_oMyObj;
}

In my template project (actually in one of the worksheet???s active) I tried
to add handler to the eventDelegate event:
object sAddinEventName = "ExcelAddIn1";
COMAddIn pAddin = Application.COMAddIns.Item(ref sAddinEventName);
object objAddin = pAddin.Object;
if (null != objAddin)
{
objAddin.GetType().InvokeMember("MyFunc", BindingFlags.InvokeMethod,
null, objAddin, invokeArgs);

EventInfo[] pAllEvents =  objAddin.GetType().GetEvents();
EventInfo peInfo = objAddin.GetType().GetEvent("eventDelegate");
MyEventDelegate pAddSiteEvent = new MyEventDelegate(EventCode);
//peInfo.AddEventHandler(objAddin, pAddSiteEvent);
object[] invokeArgs = {"123"};
objAddin.GetType().InvokeMember("MyFunc", BindingFlags.InvokeMethod,
null, objAddin, invokeArgs);
}

objAddIn receive the instance to the object. InvokeMethod works!!! But
adding event handler to the event delegate failes.
pAllEvents displays 0 events and obviously has null value after GetEvent was
called.

Any idea what I am doing wrong?

Hello Avishay,The microsoft.public.vsnet.vstools.

Colbert Zhou [MSFT] replied to Avishay Ben-Zvi
20-Aug-09 05:27 AM
Hello Avishay,

The microsoft.public.vsnet.vstools.office is not a managed newsgroup. So our
tool did not pick it up and dispatch to our support engineer.

Would you mind posting it in the Visual Studio Tools for Office forum
http://social.msdn.microsoft.com/Forums/en-US/vsto/threads

Meantime, I have already began to research on this issue already and we can
have a future discussion in the Visual Studio Tools for Office forum. Is this
OK for you?


Ji Zhou
Microsoft Online Support Team

I posted the message again athttp://social.msdn.microsoft.

Avishay Ben-Zvi replied to Colbert Zhou [MSFT]
20-Aug-09 05:13 PM
I posted the message again at
http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/2751428b-ac4c-4110-b13e-34f81f335015

Avishay
Post Question To EggHeadCafe