Add a function in my .NET application as an event handler in another .NET application
We have a .net application that we didn't develop, but use. I can tell using reflector that this application has 1) a static variable in the form main form that is a reference to that "main" form and 2) has an event that is fired that I want to be able to take action based upon.
I know what I'd do if it was in my application (mainForm.DefaultInstance.OnAlarm += myAlarmHandler;) - is it possible to do this even though it's not within my application? If it helps, there can only ever be one instance of the application I want to interface with.
Further if I can do that, is it possible to call other methods within that application as if it was that application itself that fired them (esp. button on click methods) and set the text of wi开发者_如何学编程nform textboxes?
Thanks for any help with this!
You may need to look at PostSharp, as it may help you make this modification, by weaving in a new aspect at runtime.
http://www.postsharp.org/aop-net/runtime-weaving
The nice part is that while you are experimenting you aren't changing the program on the drive, as all the changes happen when the assembly is loaded.
A problem is that you are probably going to be violating your license agreement, in that any changes to their program is prohibited, so you may want to talk with your company's lawyers about this.
If you can inject an aspect into the program then it would need to call out to your program, perhaps as a webservice call, to pass the information, unless you just want it to start your program when the event happens, passing in certain parameters.
I haven't tried this, as I tend to dislike runtime weaving, and am waiting for PostSharp 2.0 before I do much more experimenting with this tool, though I have used AspectJ extensively, on Java.
Can't think of an easy way to do this. However if I was to try then I'd add a reference to that app from mine and then use a publisher policy that redirects that app to my other app when it its executed. Then you delegate to the in the first app but from your exe.
Another thought is to ILMERGE the two exes (or yours and a child assembly of the other app) and write your own IL module loader to execute your code to add you extension point in.
Anytime you can execute in the same space as the other code then you probably have rights to do anything. However if the app is string named then you'll probably have a tonne of other issues to deal with.
You could write a profiler for this application that sat outside and sent a notification when a particular method was called.
Here's a really good article on how to do this. I think the "Receiving Method Entry and Exit Notifications" is probably the part of the article you want:
http://msdn.microsoft.com/en-us/magazine/cc301725.aspx
If you aren't comfortable with a little C++, there is a related question on SO that addresses using the profiling API with a managed wrapper:
.Net Profiling API approaches
It sounds to me like you want to do code injection into the other application. You can use unmanaged code (or a fair amount of p/invoke) to inject code into the other application with WriteProcessMemory. You can get hold of a good example by looking at the Snoop source.
精彩评论