Adding Connection Points C# Help

To see how COM events can be handled in a .NET application, first the COM component must be extended.

Implementing a COM event in an ATLclass using attributes looks very similar to the events.

NET,although the functionlity is different.

First, you have to add another interface to the interface definition file COMDemo .idl. The interface_ICompletedEvents is implemented by the client, which is the .NET application, and called by the component.

In this example, the method Completed () is called by the component when the calculation is ready.

Such an interface is also known as an outgoing interface. An outgoing interface must either be dispatch or a custom interface.

Dispatch interfaces are supported by all clients.

The custom attribute with the 10 OF21F359-ABB4-41eB-9A7B-360110E602F9 defines the name of this interface that will created in the RCW.The outgoing interface must also be written to the interfaces supported by the component inside the coclass section, and marked as a source interface:

You can use a wizard to create an implementation that fires the event back to the client, Open the classview, select the class CCornDemo, open the context menu, and start the Implement Connection Point Wizard Select the source interface lCornpletedEvents for implementation with the connection point.

Figure 24-11

Figure 24-11

The wizard creates the proxy class CProxy_lCornpletedEvents to fire the events to the client. Also, the class CCOMDerno is changed, The class now inherits from lConnectionPointCont,ainerlmpl and the proxy class.

The interface lConnectionPointContainer is added to the interface map, and a connection point map is added to the source interface lCompletedEvents.

Finally,themethod Fire_Completed () from the proxy class can be called inside the methods Add () and Sub () in thefileCOMDemo. cpp:

STDMETHODIMP CCOMDemo::AddCLONG vall. LONG val2, LONG· result)
(
-result = vall + val2;
Fire_CompletedC);
return S_OK;
}
STDMETHODIMP CCOMDemo::SubCLONG vall. LONG.vaI2. LONG· result)
(
·result = vall – val2;
Fire_CompletedC);
return S_OK;

After rebuilding the COM DLL, you can change the .NET client to use these COM events just like a normal .NET  event:

static void Main()
(
COMDemo obj = new COMDemoC);
IWelcome welcome = ohj;
Console.WriteLine(welcome.GreetingC”Christian”»;
obj.Completed +=
delegate
(
Console.WriteLineC”Calculation completed”); »;
IMath math =,CIMath)welcome;
int result = math.Add(3. 5);
Console.WriteLineCresult);
Marshal.ReleaseComObjectCmath);

As you can see, the RCW offers automatic mapping from COM events to .NET events.COM events can be used similarly .NET events in a .NET client.

Posted on October 27, 2015 in Interoperability

Share the Story

Back to Top