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

<p>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.</p>

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

<hr>

<p>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.</p>

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

<p>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.</p>

<p>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.</p>

<hr>

<p>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.</p>

<p>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.</p>

<p>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.</p>

[{'answer_id': 17892, 'author': 'Mark Cidade', 'author_id': 1659, 'author_profile': 'https://Stackoverflow.com/users/1659', 'pm_score': 1, 'selected': False, 'text': '<p>You can either use remoting or WCF. See <a href="http://msdn.microsoft.com/en-us/library/aa730857(VS.80).aspx#netremotewcf_topic7" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/aa730857(VS.80).aspx#netremotewcf_topic7</a>.</p>\n'}, {'answer_id': 17905, 'author': 'lubos hasko', 'author_id': 275, 'author_profile': 'https://Stackoverflow.com/users/275', 'pm_score': 2, 'selected': False, 'text': '<p>You can try <a href="http://msdn.microsoft.com/en-us/magazine/cc163617.aspx" rel="nofollow noreferrer">Managed Spy</a> and for programmatic access <strong>ManagedSpyLib</strong></p>\n\n<blockquote>\n  <p>ManagedSpyLib introduces a class\n  called ControlProxy. A ControlProxy is\n  a representation of a\n  System.Windows.Forms.Control in\n  another process. ControlProxy allows\n  you to get or set properties and\n  subscribe to events as if you were\n  running inside the destination\n  process. Use ManagedSpyLib for\n  automation testing, event logging for\n  compatibility, cross process\n  communication, or whitebox testing.</p>\n</blockquote>\n\n<p>But this might not work for you, depends whether ControlProxy can somehow access the event you\'re after within your third-party application.</p>\n\n<p>You could also use <a href="http://www.codeproject.com/KB/msil/reflexil.aspx" rel="nofollow noreferrer">Reflexil</a></p>\n\n<blockquote>\n  <p>Reflexil allows \n  IL modifications by using the powerful\n  Mono.Cecil library written by Jb\n  EVAIN. Reflexil runs as Reflector plug-in and\n  is directed especially towards IL code\n  handling. It accomplishes this by\n  proposing a complete instruction\n  editor and by allowing C#/VB.NET code\n  injection.</p>\n</blockquote>\n'}, {'answer_id': 17924, 'author': 'Coincoin', 'author_id': 42, 'author_profile': 'https://Stackoverflow.com/users/42', 'pm_score': 0, 'selected': False, 'text': "<p>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?</p>\n\n<p>If <em>you</em> are planning on doing interprocess communication, the first question you should ask yourself is: Is it really necessary?</p>\n\n<p>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.</p>\n\n<p>This brings the question, what is it you are trying to do exactly? What is this third party application you have no control over?</p>\n\n<p>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.</p>\n"}, {'answer_id': 17950, 'author': 'Mark Cidade', 'author_id': 1659, 'author_profile': 'https://Stackoverflow.com/users/1659', 'pm_score': 0, 'selected': False, 'text': '<p>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.</p>\n\n<p>See <a href="http://www.code-magazine.com/article.aspx?quickid=0605061" rel="nofollow noreferrer">http://www.code-magazine.com/article.aspx?quickid=0605061</a>.</p>\n'}, {'answer_id': 17977, 'author': 'Anders Sandvig', 'author_id': 1709, 'author_profile': 'https://Stackoverflow.com/users/1709', 'pm_score': 5, 'selected': True, 'text': '<p>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 <a href="http://en.wikipedia.org/wiki/Inter-process_communication" rel="noreferrer">Inter-process Communication (IPC)</a>. There exists many standard ways of doing IPC, the most common being files, pipes, (network) sockets, <a href="http://en.wikipedia.org/wiki/Remote_procedure_call" rel="noreferrer">remote procedure calls (RPC)</a> and shared memory. On Windows it\'s also common to use <a href="http://msdn.microsoft.com/en-us/library/aa931932.aspx" rel="noreferrer">window messages</a>.</p>\n\n<p>I am not sure how this works for .NET/C# applications on Windows, but in native Win32 applications you can <a href="http://msdn.microsoft.com/en-us/library/ms644990.aspx" rel="noreferrer">hook on to the message loop of external processes and "spy" on the messages they are sending</a>. If your program generates a message event when the desired function is called, this could be a way to detect it.</p>\n\n<p>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).</p>\n'}, {'answer_id': 70006448, 'author': 'Martin', 'author_id': 15784095, 'author_profile': 'https://Stackoverflow.com/users/15784095', 'pm_score': 0, 'selected': False, 'text': '<p>also WM_COPYDATA might be possible, see <a href="https://social.msdn.microsoft.com/Forums/en-US/eb5dab00-b596-49ad-92b0-b8dee90e24c8/wmcopydata-event-to-receive-data-in-form-application?forum=winforms" rel="nofollow noreferrer">https://social.msdn.microsoft.com/Forums/en-US/eb5dab00-b596-49ad-92b0-b8dee90e24c8/wmcopydata-event-to-receive-data-in-form-application?forum=winforms</a>\nI\'m using it for similar Purose (to notify that options have been changed)</p>\n<p>In our C++/Cli-scenario (MFC-)programs communicate vith WM_COPYDATA with Information-String in COPYDATASTRUCT-Member lpData\n(Parameterlist like &quot;Caller=xyz Receiver=abc Job=dosomething&quot;). also a C#-App can receive WM_COPYDATA-messages as shown in the link. Sending WM_COPYDATA from C# (to known Mainframe-Handle) is done by a cpp/cli-Assembly, (I didnt proove how sending WMCOPYDATA can bei done in C#).</p>\n<p>PS in Cpp/Cli we send AfxGetMainWnd()-&gt;m_hWnd as WPARAM of WMCOPYDATA-Message and in C# (WndProc) m.WParam can be used as adress to send WM_COPYDATA</p>\n'}]
['https://Stackoverflow.com/questions/17878', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/493/']