Adding Connection Points C# Help

Adding support for COM events to the .NET components requires some changes to the implementation of your .NET class. Offering COM events is not a simple matter of using the event and delegate
keywords; it is necessary to add some more COM interop attributes.

First, you have to add an interface to the .NET project: IMathEvents.

This interface is the source or outgoing interface for the component, and will be implemented by the sink object in the client.

A source interface must be either a dispatch or a custom interface. A scripting client supports only dispatch interfaces.

Dispatch interfaces are usually preferred as source interfaces:

public interface IMathEvents
[Displd(46200)] v~d CalculationCompleted();

Next, you have to add a delegate. The delegate must have the same signature and return type as the method in the outgoing interface. If you have multiple methods in your source jnterface, for each one that differs in its arguments, you have to specify a  separate delegate. Because the COM client does not have to  access this delegate directly, the delegate can be marked with the attribute.
[ComVisible(false) ]:
public delegate void CalculationCompletedEventHandler();

With the class DotnetComponent, a source interface must be specified. This can be done with the attribute  ComSourcelnterfaces].

Add the attribute [ComSourcelnterfaces], and specify the outgoing interface declared earlier. You can add more than one source  interface with different constructors of .lile attribute class; however, the only client language that supports more than one source interface is C++. Visual Basic 6.0 clients support only one  source interface.

public DotnetComponent()
[Progld ( “Wrox. DotnetComponent” ) ]
‘Iccmvf s Ibl e (true) ]
public class DotnetComponent : IWelcome, IMath

Inside the class DotnetComponent, you have to declare an event for every method of the source

The type of the method must be the name of the delegate, and the name of the event must exactly the same as the name of the  method inside the source interface.

You can add the event calls to the Add () and Sub () methods. This step is the normal .NET way to invoke events, as discussed.

public int Add(int vall, int val2)
int result = vall + val2;
if (CalculationCompleted != null)
return result;

public int Sub(int vall, int va12)
int result = vall – va12;
if (CalculationCompleted != null)
return result;

The name of the event must be the same as the name of the method inside the source interface.

Otherwise, the events cannot be mapped for COM clients.



Posted on October 27, 2015 in Interoperability

Share the Story

Back to Top
Share This