Using and Implementing a Connectable Object: CONNECT

Now that you understand the theory and mechanics of connection points, let's look at them in practice through the Connect sample, which you'll find in CHAP04\CONNECT. This is really something of a contrived example because the client, source, and sink are all contained within the same program. Connection points are most useful when you have clients and objects separated by DLL or process boundaries, but we won't see how to work with custom service components until the next chapter. Nevertheless, this sample illustrates all the principles of connection points and provides a complete reference implementation of all the interfaces (including enumerators) on appropriate objects.

For the Connect sample, we'll need some sort of interface for notifications and events. For the purpose of our sample, let's define the interface IDuckEvents as follows (the compiling form of which is found in the file INTERFAC.H):


interface IDuckEvents : IUnknown
{
HRESULT Quack(void);
HRESULT Flap(void);
HRESULT Paddle(void);
};

This interface is also assigned IID_IDuckEvents (00021145-0000-C000-000000000046) in INC\BOOKGUID.H.

The main application in the Connect sample, considered the "client" portion of the program, is the C++ class CApp, which creates a window and processes menu commands. During initialization, CApp instantiates two sink objects using the C++ class CDuckEvents. This class inherits the member functions of IDuckEvents directly. Initially these sinks are not connected to any objects. The Object menu on the Connect sample window allows you to create and destroy a connectable source object (through the CConnObject and CConnectionPoint classes) as well as to connect and disconnect either sink (#1 or #2) from that source. Nothing much will happen, of course, until we can cause the source to actually fire some of its events, which is the purpose of the Trigger menu on the main window. From this menu, you can cause a Quack, Flap, or Paddle event and see the effect on whatever sinks are connected. If you haven't connected anything, nothing will, in fact, happen. If you have either or both sinks connected, you'll see messages in the window. If you are running on a Win32 system and have a sound board, you'll get to hear what's in the WAV files that come with this sample. Surprise!3

Note: To compile these samples, you'll need the OLECTL.H and OLECTLID.H header files from the Visual C++ Control Development Kit. However, you don't need any import libraries from the CDK.

3 I'll be honest. These sounds are only my imitations of the ducks that paddle around the lake near my house. I couldn't get one to come inside and give me voice samples.