Discussion:
Add-in Event handler assignment failes in late binding
(too old to reply)
Avishay Ben-Zvi
2009-08-05 09:01:01 UTC
Permalink
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?
Colbert Zhou [MSFT]
2009-08-20 09:27:15 UTC
Permalink
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
Post by Avishay Ben-Zvi
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
// 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))]
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
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?
Avishay Ben-Zvi
2009-08-20 21:13:01 UTC
Permalink
I posted the message again at
http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/2751428b-ac4c-4110-b13e-34f81f335015

Avishay
Post by Colbert Zhou [MSFT]
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
Post by Avishay Ben-Zvi
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
// 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))]
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
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?
Loading...