views:

1754

answers:

7

Suppose I have two applications written in C#. The first is a third party application that raises an event called "OnEmailSent".

The second is a custom app that I've written that I would like to somehow subscribe to the "OnEmailSent" even of the first application.

Is there any way that I could somehow attach the second application to an instance of the first application to listen for "OnEmailSent" event?

A: 

You can either use remoting or WCF. See http://msdn.microsoft.com/en-us/library/aa730857(VS.80).aspx#netremotewcf_topic7.

Mark Cidade
+1  A: 

You can try Managed Spy and for programmatic access ManagedSpyLib

ManagedSpyLib introduces a class called ControlProxy. A ControlProxy is a representation of a System.Windows.Forms.Control in another process. ControlProxy allows you to get or set properties and subscribe to events as if you were running inside the destination process. Use ManagedSpyLib for automation testing, event logging for compatibility, cross process communication, or whitebox testing.

But this might not work for you, depends whether ControlProxy can somehow access the event you're after within your third-party application.

You could also use Reflexil

Reflexil allows IL modifications by using the powerful Mono.Cecil library written by Jb EVAIN. Reflexil runs as Reflector plug-in and is directed especially towards IL code handling. It accomplishes this by proposing a complete instruction editor and by allowing C#/VB.NET code injection.

lubos hasko
A: 

I might be missing something, but from what I understand of how remoting works is that there would need to be a server defining some sort of contract that the client can subscribe to.

I was more thinking about a scenario where someone has written a standalone application like outlook for example, that exposes events that I would like to subscribe to from another application.

I guess the scenario I'm thinking of is the .net debugger and how it can attach to executing assemblies to inspect the code whilst it's running.

lomaxx
A: 

What's the nature of that OnEmailSent event from that third party application? I mean, how do you know the application is triggering such an event?

If you are planning on doing interprocess communication, the first question you should ask yourself is: Is it really necessary?

Without questioning your motives, if you really need to do interprocess communication, you will need some sort of mechanism. The list is long, very long. From simple WM_DATA messages to custom TCP protocols to very complex Web services requiring additional infrastructures.

This brings the question, what is it you are trying to do exactly? What is this third party application you have no control over?

Also, the debugger has a very invasive way of debugging processes. Don't expect that to be the standard interprocess mechanism used by all other applications. As a matter of fact, it isn't.

Coincoin
A: 

You can implement a similar scenario with SQL Server 2005 query change notifications by maintaing a persistent SqlConnection with a .NET application that blocks until data changes in the database.

See http://www.code-magazine.com/article.aspx?quickid=0605061.

Mark Cidade
+1  A: 

In order for two applications (separate processes) to exchange events, they must agree on how these events are communicated. There are many different ways of doing this, and exactly which method to use may depend on architecture and context. The general term for this kind of information exchange between processes is Inter-process Communication (IPC). There exists many standard ways of doing IPC, the most common being files, pipes, (network) sockets, remote procedure calls (RPC) and shared memory. On Windows it's also common to use window messages.

I am not sure how this works for .NET/C# applications on Windows, but in native Win32 applications you can hook on to the message loop of external processes and "spy" on the messages they are sending. If your program generates a message event when the desired function is called, this could be a way to detect it.

If you are implementing both applications yourself you can chose to use any IPC method you prefer. Network sockets and higher-level socket-based protocols like HTTP, XML-RPC and SOAP are very popular these days, as they allow you do run the applications on different physical machines as well (given that they are connected via a network).

Anders Sandvig
A: 

So for further clarification, my specific scenario is that we have a custom third party application written in c# that raises an "OnEmailSent" event. We can see the event exists using reflector.

What we want to do is have some other actions take place when this component sends an email.

The most efficient way we can think of would be to be able to use some form of IPC as anders has suggested and listen for the OnEmailSent event being raised by the third party component.

Because the component is written in C# we are toying with the idea of writing another C# application that can attach itself to the executing process and when it detect the OnEmailSent event has been raise it will execute it's own event handling code.

lomaxx