Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / May 2006

Tip: Looking for answers? Try searching our database.

MethodInfo.Invoke on unknown method of ActiveX control at runtime

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
yoga weazel - 10 May 2006 15:07 GMT
OK so weird scenario but it's what the client wants so here it goes...

I have a bunch of ActiveX/OCX controls which I have run through AxImp to
generate wrappers.
These controls all have a somewhat generic interface and each include a
subset of Event Handlers including:

LoadApp
LaunchApplication
ChangeTitle
Resize
Unload

Resize and Unload are generic EventHandlers and I have written code that
works fine for these but the others are custom event handlers.  As an
example, for the control MyControl the LaunchApplication event handlers would
have the following in the wrapper class Types:

void add_LaunchApplication(__MyControl_LaunchApplicationEventHandler)
void remove_LaunchApplication(__MyControl_LaunchApplicationEventHandler)

additionally, the ctor for __MyControl_LaunchApplicationEventHandler is
defined as:

__MyControl_LaunchApplicationEventHandler(object object, IntPtr method)
__GridDisplay_LaunchApplicationEventHandler(object object, IntPtr method)  
<-- other example
__LabelControl_LaunchApplicationEventHandler(object object, IntPtr method)  
<-- other example

I got the above information from Reflection API on MyControl, the above
methods and event handler at runtime.

So at runtime I have a instance of the MyControl wrapper control and
interrogate it to get the MethodInfo instance for add_LaunchApplication then
interogate further to get the ParameterInfo for the method parameter as
follows:

01 For Each meth As System.Reflection.MethodInfo In
(ctrl.GetType().GetMethods())
02   Select Case (meth.Name.ToLower())
03     Case "add_launchapplication"
04
05       Dim args() As Object
06       ReDim args(0)
07
08       Dim ctorArgs() As Object
09       ReDim ctorArgs(1)
10       ctorArgs(0) = ctrl.TopLevelControl
11       ctorArgs(1) = ???what here???
12
13       args(0) =
Activator.CreateInstance(meth.GetParameters()(0).ParameterType, _
14                                           Reflection.BindingFlags.Public, _
15                                           Nothing, _
16                                           ctorArgs, _
17                                           Nothing)
18       meth.Invoke(ctrl, _
19                   Reflection.BindingFlags.InvokeMethod, _
20                   Nothing, _
21                   args, _
22                   Nothing)
23
24   End Select
25 Next

I can call the method directly, that's fine, but I still need to instantiate
an instance of the event handler which I will not know until runtime.  The
lines I'm having problems with are 08-17.  I am assuming that the first ctor
arg (object) is the instance object mosting the event handler method and the
second (method) is a pointer to the event handler method.  But how do I get
this value at runtime?  Anyone have any suggestions?  Am I overlooking an
obvious alternative?  Thanks.

Mike
yoga weazel - 10 May 2006 18:25 GMT
As additional information I ran AxImp with the /source option on the OCX
controls and generated the C# events/delegates.  Below is a sample:

public event @__Filter_ChangeTitleEventHandler ChangeTitle;
public delegate void @__Filter_ChangeTitleEventHandler(object sender,
                                                                       
@__Filter_ChangeTitleEvent e);
public class @__Filter_ChangeTitleEvent
{
       public string newTitle;
       
       public @__Filter_ChangeTitleEvent(string newTitle)
       {
           this.newTitle = newTitle;
       }
}

So my event handler method has to have a signature like:

public void HandleChangeTitle(object sender,
                                           __Filter_ChangeTitleEvent e)
{
   // do something here
}

and for every single control I would have to have a method like the above.  
The only difference is the second parameters type would change to __<control
name>_ChangeTitleEvent for each method.  I can't do anything like define one
method with two object parameters as a generic handler.  Anyone have any
ideas?  It would be GREATLY appreciate at this point!  Thanks.

Mike

> OK so weird scenario but it's what the client wants so here it goes...
>
[quoted text clipped - 71 lines]
>
> Mike

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.