Actually, hope is not yet lost. You can create an event on a COM object outside the class of the object. This functionality is actually provided by COM itself, albeit indirectly.
In COM, events work on a publish / subscribe model. A COM object that has events (an "event source") publishes events, and one or more other COM objects subscribe to the event, attaching an event handler to the original object (handlers are called "event receivers"). Typically, the source object raises an event by simply looping through all event receivers and calling the appropriate handler method.
So how does this help you? It so happened that COM allows you to query the source of the event for a list of all the objects of the event receiver that are currently subscribed to the events of the original object. After you have a list of event receiver objects, you can simulate raising an event by calling each of the event handlers of the receiver object.
Note: I oversimplify the details and take some terminology liberally, but that there is a short (and somewhat politically incorrect) version of how events work in COM.
You can use this knowledge to raise events on a COM object from external code. In fact, all this can be done in C # with support for interacting with COM in the System.Runtime.Interop and System.Runtime.Interop.ComTypes .
EDIT
I wrote a utility class that allows you to create events on a COM object from .NET. It is pretty easy to use. Here is an example of using the event interface from your question:
MyObj legacyComObject = new MyObj();
The following is the code for the ComEventUtils class (as well as the SafeIntPtr helper class), since I am paranoid and would like to have a good way to deal with IntPtr needed for COM related code):
Disclaimer I have not fully tested the code below. The code performs manual memory management in several places, and so it is likely that it could introduce memory leaks into your code. In addition, I did not add error handling to the code, because this is just an example. Use with caution.
using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; using COM = System.Runtime.InteropServices.ComTypes; namespace YourNamespaceHere {
Mike spross
source share