Attaching an event handler to a Com event: InvalidOperationException (S7-PLCSIM)
<p>I'm trying to attach an Event Handler to an Event of a <a href="https://cache.industry.siemens.com/dl/files/855/1139855/att_29422/v1/S7WSPSCA.pdf" rel="nofollow">COM Object</a>. But I get an InvalidOperationException.</p>

<pre><code>using S7PROSIMLib;

private S7ProSim ps = new S7ProSim();

ps.Connect();
ps.SetScanMode(ScanModeConstants.SingleScan);
ps.BeginScanNotify();

try {
    ps.ScanFinished += Ps_ScanFinished;
    //IS7ProSimEvents_ScanFinishedEventHandler scanFinishedDelegate = new IS7ProSimEvents_ScanFinishedEventHandler(Ps_ScanFinished);
    //ps.ScanFinished += scanFinishedDelegate;
}
catch (InvalidOperationException ex)
{
    Console.WriteLine(ex.Source);
    Console.WriteLine(ex.InnerException);
    Console.WriteLine(ex.Message);
    Console.WriteLine(ex.StackTrace);
}

private void Ps_ScanFinished(object ScanInfo)
{
    Console.WriteLine("Event fired");
} 
</code></pre>

<p>The Output is:</p>

<pre><code>event invocation for COM objects requires event to be attributed with DispIdAttribute
   at System.Runtime.InteropServices.ComAwareEventInfo.GetDataForComInvocation(EventInfo eventInfo, Guid&amp; sourceIid, Int32&amp; dispid)
   at System.Runtime.InteropServices.ComAwareEventInfo.AddEventHandler(Object target, Delegate handler)
   at PiPLCSimBridge.Form1.Form1_Load(Object sender, EventArgs e) in C:\PiPLCSimBridge\Form1.cs:Line 72.
</code></pre>

<p>I have also tried to use the commented out code, but got the same Exception.</p>

<p>Attaching the events like this should work, a popular Tool using the same COM interface <a href="http://sourceforge.net/p/nettoplcsim/code/HEAD/tree/branches/multiinstace/nettoplcsim/csharp/NetToPLCSim/NetToPLCSim/PLCSim.cs#l49" rel="nofollow">is doing it this way.</a></p>

<p>What is wrong with my Code?</p>