.NET Framework - 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?
ComInterfaceType.InterfaceIsIDispatch
(1)
ComInterfaceType.InterfaceIsDual
(1)
Visual Studio
(1)
RequestComAddInAutomationService
(1)
BindingFlags.InvokeMethod
(1)
Office
(1)
VSTO
(1)
StandardOleMarshalObject
(1)
Colbert Zhou [MSFT] replied to Avishay Ben-Zvi
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
Avishay Ben-Zvi replied to Colbert Zhou [MSFT]
I posted the message again at
http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/2751428b-ac4c-4110-b13e-34f81f335015
Avishay
Is Visual Studio self-hosting ? .NET Framework Does Microsoft use Visual Studio IDE, Visual Studio Debugger, Visual Studio Linker and Visual Studio compiler for developing Visual Studio ? Or is Visual Studio not good enough for them and are they in fact using something else ? Visual Studio Development Discussions Visual Studio (1) Linker (1) Don't spread it around but they
Visual Studio vs Visual Web Developer .NET Framework Hi! The Visual Web Developer is better than Visual Studio 2005 for web development? All the Ajax examples are developed in Visual Web Developer. Thanks ASP.NET Discussions Visual Studio 2005 (1) Visual Studio (1) Office (1) Compiler (1) Library (1) Control (1) Coverage (1) Database (1) I think you're confusing the products here. Visual Web Developer IS the web design portion of Visual Studio. Visual Studio includes a number
Upgrade Visual Studio 6 to Visual Studio 2008? .NET Framework I have Visual Studio 6 Professional. Can I install the upgrade version of Visual Studio 2008 Professional? I cannot find this info on Microsoft's website. Visual Studio General Discussions Visual Studio 2008 (1) Visual Studio (1) ArchibaldMartel (1) If VS6 is not listed as a possible upgrade, then the be a licensed user of one of the following products: * An earlier version of Microsoft Visual Studio * Any other developer tool (including free developer tools, such as Visual Studio Express Editions
Visual Basic / Visual Studio 2008 Embedded SQL .NET Framework What is the easiest (as most like simple embedded SQL) to declare and open / then process a cursor in Visual Basic / Visual Studio 2008? I wish it were as simple as it was in MF Cobol or and its flavors where your could just use EXEC SQL . . . . . . . . . . END EXEC. VB.NET Discussions Visual Studio 2008 (1) Office (1) Table1.ClassOffice (1) DateTime (1) ADO.NET (1) ADODB (1 can do it. for ext - Yes, but what is the "Classic ADO" syntax / implementation in Visual Studio / Basic 2008? On Mar 12, 11:22 am, "aaron.ke. . .@gmail.com" <aaron.ke. . .@gmail.com> - -Yes, but what is the "Classic ADO" syntax / implementation in Visual - -Studio / Basic 2008? Be careful about listening to him. He was trolling pretty heavily in your original request? If you clarify, someone will probably help you. RobinS. GoldMail.com keywords: Visual, Basic / Visual, Studio, 2008, Embedded, SQL description: What is the easiest (as most like simple