Reputation: 167
I'm trying to attach an Event Handler to an Event of a COM Object. But I get an InvalidOperationException.
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");
}
The Output is:
event invocation for COM objects requires event to be attributed with DispIdAttribute
at System.Runtime.InteropServices.ComAwareEventInfo.GetDataForComInvocation(EventInfo eventInfo, Guid& sourceIid, Int32& 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.
I have also tried to use the commented out code, but got the same Exception.
Attaching the events like this should work, a popular Tool using the same COM interface is doing it this way.
What is wrong with my Code?
Upvotes: 4
Views: 2493
Reputation: 6950
As luck would have it, I recently encountered this exact same error with this exact same COM library.
The problem is caused by the Embed Interop Types
setting in the prosim reference.
I discovered this fact via the discussion here.
So, in the project references section, check the properties for the prosim reference. Make sure Embed Interop Types
is not set.
Upvotes: 8