Late Binding of Event using Extensibility.EDTExtensibility2

R

Roberto Huezo

Hello, my name is Roberto. I have used late binding of events using Delegate
in other projects before,
but my current project uses another approach. On the first project I use,
for example:

Assembly assembly = Assembly.LoadFrom(myapp);
UserControl myusercontrol = assembly.CreateInstance("MyAppNamespace") as
UserControl;
EventInfo eventinfo = myusercontrol.GetType().GetEvent("MyAppResize",
(BindingFlags.Public | BindingFlags.Instance));
Delegate hanlder = Delegate.CreateDelegate(eventinfo.EventHandlerType, this,
"this_MyAppResize");
eventinfo.AddEventHandler(myusercontrol, handler);

* where myapp is a string with the path to my project.exe; "MyAppResize" is
an event in my project; "this_MyAppResize" is a method in THIS file

On my current project, which is a MS Office Add-In, I need to use the
Extensibility.EDTExtensibility2 classes and using object for the loading
application (it can be any MS Office program). The entry point of my dll is
the method
OnConnection(object application, Extensibility.ext_ConnectMode connectMode,
object addInInst, ref System.Array custom)
*Where application is the calling application (Word, Excel, Outlook, etc)
and from this moment on I am forced to use the following synthax to call
methods and get or set properties:
string appName = (string)applicationType.InvokeMember("Name",
BindingFlags.GetProperty, null, applicationObject, null);

oRange.GetType().InvokeMember("NumberFormat", BindingFlags.SetProperty,
null, oRange, new object[]{"@"});

oRange.GetType().InvokeMember("Select",BindingFlags.InvokeMethod,null,oRange,
null);

In summary, I can get/set properties, and InvokeMethods with parameters, but
I can not bind an event from the applicationObject variable above.
Has anyone ran into this problem?

Thank you in advance

Roberto
 
E

Eugene E. Starostin

Hello Roberto,

We use eventsynks to handle events in our add-in express. For example, this
is the connectto method of word evens synkhelper:


internal bool ConnectTo(object app, object events)
{
if (m_Cookie != 0) return false;
if (events is AddinExpress.MSO.ADXWordAppEvents)
this.events = events as AddinExpress.MSO.ADXWordAppEvents;
try
{
Guid guid = new Guid(Consts.WordAppEventsID);
UCOMIConnectionPointContainer oConnPointContainer =
(UCOMIConnectionPointContainer)app;
oConnPointContainer.FindConnectionPoint(ref guid, out m_oConnectionPoint);
if (m_oConnectionPoint != null)
{
m_oConnectionPoint.Advise(this, out m_Cookie);
}
oConnPointContainer = null;
}


Regards from Belarus
Eugene Starostin
 
R

Roberto Huezo

Hello Eugene,

thank you for the tip! It worked! It took a while to implement, but I got it
running. I see that you are part of the ADX Team...good job guys, it is not
easy connecting events of COM object this way.

I just have another question concerning the Sink Events exposed in the
Interop COM Wrapper. I found out that I have to include the events which I
need to catch in my project in the form of methods like this:

[DispId(1558)]
public void SheetSelectionChange(object o, object r)
{
....
}

I looked at the Interop.Excel.dll using 'ildasm' and found the AppEvents for
Excel but could not find reference to this number (1568), other than
'01 00 16 06 00 00 00 00'

I found a 'listing' for PowerPoint for its public Events under:
http://support.microsoft.com/kb/308825/EN-US/
and was hoping to find one for Excel. The reason is that I need to catch
other events and I don't have these DispIds, in other words I can't catch
them...do you have or do you know where I can find something like that?

I hope I could explain myself good enough, thank you very much in advance

Best Regards
Roberto Huezo


Eugene E. Starostin said:
Hello Roberto,

We use eventsynks to handle events in our add-in express. For example,
this is the connectto method of word evens synkhelper:


internal bool ConnectTo(object app, object events)
{
if (m_Cookie != 0) return false;
if (events is AddinExpress.MSO.ADXWordAppEvents)
this.events = events as AddinExpress.MSO.ADXWordAppEvents;
try
{
Guid guid = new Guid(Consts.WordAppEventsID);
UCOMIConnectionPointContainer oConnPointContainer =
(UCOMIConnectionPointContainer)app;
oConnPointContainer.FindConnectionPoint(ref guid, out m_oConnectionPoint);
if (m_oConnectionPoint != null)
{
m_oConnectionPoint.Advise(this, out m_Cookie);
}
oConnPointContainer = null;
}


Regards from Belarus
Eugene Starostin
---
ADX Team
www.add-in-express.com - A new visual RAD tool for
developing COM add-ins, Smart Tags and RTD Servers


Roberto Huezo said:
Hello, my name is Roberto. I have used late binding of events using
Delegate in other projects before,
but my current project uses another approach. On the first project I use,
for example:

Assembly assembly = Assembly.LoadFrom(myapp);
UserControl myusercontrol = assembly.CreateInstance("MyAppNamespace") as
UserControl;
EventInfo eventinfo = myusercontrol.GetType().GetEvent("MyAppResize",
(BindingFlags.Public | BindingFlags.Instance));
Delegate hanlder = Delegate.CreateDelegate(eventinfo.EventHandlerType,
this,
"this_MyAppResize");
eventinfo.AddEventHandler(myusercontrol, handler);

* where myapp is a string with the path to my project.exe; "MyAppResize"
is
an event in my project; "this_MyAppResize" is a method in THIS file

On my current project, which is a MS Office Add-In, I need to use the
Extensibility.EDTExtensibility2 classes and using object for the loading
application (it can be any MS Office program). The entry point of my dll
is
the method
OnConnection(object application, Extensibility.ext_ConnectMode
connectMode,
object addInInst, ref System.Array custom)
*Where application is the calling application (Word, Excel, Outlook, etc)
and from this moment on I am forced to use the following synthax to call
methods and get or set properties:
string appName = (string)applicationType.InvokeMember("Name",
BindingFlags.GetProperty, null, applicationObject, null);

oRange.GetType().InvokeMember("NumberFormat", BindingFlags.SetProperty,
null, oRange, new object[]{"@"});

oRange.GetType().InvokeMember("Select",BindingFlags.InvokeMethod,null,oRange,
null);

In summary, I can get/set properties, and InvokeMethods with parameters,
but I can not bind an event from the applicationObject variable above.
Has anyone ran into this problem?

Thank you in advance

Roberto
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top