Visual C++ 6 Unleashed

Visual C++ 6 Unleashed

By MICKEY WILLIAMS and David Bennett

Connection Points

Connection points are used by connectable objects to establish bidirectional communication. Connectable objects communicate with their clients through connection-point interfaces—back-channel interfaces that allow a COM server to notify its clients of events. Two interfaces are implemented by a connectable object:

Most ActiveX controls are connectable objects. Other types of COM objects can implement the IConnectionPointContainer and IConnectionPoint interfaces, however, and thus become connectable objects. Figure 31.4 illustrates the coupling between a connectable client and server.

31fig04.gif

Figure 31.4 The interfaces used to implement connectable objects.

A client interested in a specific IConnectionPoint interface first invokes QueryInterface, requesting the IConnectionPointContainer interface from the server. If an interface pointer is returned, the particular IConnectionPoint interface is requested through IConnectionPointContainer. If the request is successful, the client passes a pointer to its notification sink to the server through the IConnectionPoint interface. The server transmits event notifications to the client using the pointer to the notification sink.

The simplest way to support connection points in a project built by using ATL is to select the Support Connection Points option when using the ATL Object Wizard. A definition of an outgoing interface will be added to the project IDL file. Listing 31.5 is a fragment of an IDL file with an example of an outgoing interface for a COM class named Bothway.

Example 31.5. Defining an Outgoing Interface for a Connectable Object

library CONNECTEXAMPLELib
{
    importlib("stdole31.tlb");
    importlib("stdole2.tlb");

    [
        uuid(FCB636B8-FD81-4AB2-B455-9F2BEDA22FBF),
        helpstring("_IBothwayEvents Interface")
    ]
    dispinterface _IBothwayEvents
    {
        properties:
        methods:
    };

    [
        uuid(F7706E85-129F-4B2D-A694-90EF33F3357D),
        helpstring("Bothway Class")
    ]
    coclass Bothway
    {
        [default] interface IBothway;
        [default, source] dispinterface _IBothwayEvents;
    };
};

Note that the outgoing interface is named _IBothwayEvents. The outgoing interface is prefixed with _I and has Events added to the name of the COM class.

To implement connection-point methods for your COM class, you must follow these steps:

  1. Add methods and properties to the IDL definition for the outgoing interface.
  2. Compile the IDL using the MIDL compiler to create a type library containing the connection-point information. The easiest way to do this is to build the ATL project.
  3. After the build is complete, right-click the ATL class in the Class View window, and select Implement Connection Points from the pop-up menu. A dialog box appears, as Figure 31.5 shows.

    31fig05.gif

    Figure 31.5 Implementing connection points for a COM server.

    The Implement Connection Point dialog box contains a list of interfaces you can use to implement connection points. In most cases, this dialog box contains only one entry.

  4. Check the outgoing interface and click OK.

The connection-point class is created with a name generated by prefixing CProxy to the interface name. For the _IBothwayEvents example, the generated class is named CProxy_IBothwayEvents. The connection-point class contains member functions that can be used to generate events that are sent out to the client. These functions are named by prefixing Fire_ to the name of the outgoing interface method defined in IDL. For an outgoing method defined as SayHey in IDL, the connection-point class contains a function with the following name:

Fire_SayHey()

The new connection-point class is added to the multiple inheritance list for the control. You then can fire any event directly. Here's an example:

if(eyesShut && snoringLoudly)
    Fire_DadIsSleeping();

Connection points were developed for use in ActiveX controls. ActiveX controls use connection points to coordinate the interfaces used between controls and their containers.

Share ThisShare This

Informit Network