Avishay Ben-Zvi
2009-08-05 09:01:01 UTC
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?
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?