This documentation is archived and is not being maintained.

Step 3: Implementing the Server

Visual Studio .NET 2003

In this step, you will add the functionality to make the class do something interesting. In the last two steps, you have created the CDispServ class, which now exposes a custom interface (IDispServ), and an event source (the _IDispServEvents event interface). Beyond this implementation, however, it does not actually do anything.

The main purpose of the CDispServ class is to receive data using the Send method and transmit the information using the Transfer event. Therefore, a DispClient object connected to a DispServer object can call Send through the IDispServ interface, passing it data. The Send method then fires the Transfer event, propagating the data to all connected DispClient objects. Previously, this required creating a connection point proxy class to fire the events. With the new events feature, the situation is simplified; you will add the Transfer event to the _IDispServEvents interface.

To add and define the Transfer event

  1. In Class View, click the DispServer project to expand the node.
  2. Double-click the _IDispServEvents interface.

    This opens the interface definition in the Code editor.

  3. Define the Transfer event for the _IDispServEvents interface by adding the following code to the _IDispServEvents interface definition:
    [id(1), helpstring("method Transfer")] HRESULT Transfer(VARIANT data);
    
  4. Make IDispatch a base class of the interface.

Your interface definition should now match the following definition:

__interface _IDispServEvents : public IDispatch
{
   [id(1), helpstring("method Transfer")] HRESULT Transfer(VARIANT data);
};

To fire the Transfer event, make a call to _IDispServEvents_Transfer from your event source. The form for firing an event is always InterfaceName_EventName.

To add and define the Send method

  1. In Class View, double-click the IDispServ interface.

    This opens the interface definition in the Code editor.

  2. Define the Send method for the IDispServ interface by adding the following code to the IDispServ interface definition:
    [id(1), helpstring("method Send")] HRESULT Send(VARIANT data);
    

Your interface definition should now match the following definition:

__interface IDispServ : IDispatch
{
   [id(1), helpstring("method Send")] HRESULT Send(VARIANT data);
};

To implement the Send method for the CDispServ class, scroll down to the bottom of DispServ.h and add the following code directly below the last public: section of the class:

STDMETHOD(Send)(VARIANT data)
{
   _IDispServEvents_Transfer(data);
   return (S_OK);
}

The full CDispServ class should now look like this:

class ATL_NO_VTABLE CDispServ : 
   public IDispServ
{
public:
   CDispServ()
   {
   }

   __event __interface _IDispServEvents;

   DECLARE_PROTECT_FINAL_CONSTRUCT()

   HRESULT FinalConstruct()
   {
      return S_OK;
   }
   
   void FinalRelease() 
   {
   }

public:
   STDMETHOD(Send)(VARIANT data)
   {
      _IDispServEvents_Transfer(data);
      return S_OK;
   }
};

The final task implements the CDispServ class as a singleton server, which means that all clients will connect to the same server.

To define CDispServ as a singleton server

  1. In Class View, double-click the CDispServ node.
  2. Add the following lines directly below the DECLARE_PROTECT_FINAL_CONSTRUCT() in class CDispServ:
    DECLARE_CLASSFACTORY_SINGLETON(CDispServ);
    

The server is now complete. You can build the server by selecting Build DispServer from the Build menu. Once the server is successfully built, it will register itself.

The Visual Studio development environment also provides wizards that let you add properties and methods. Just right click on the interface node in Class View and select a wizard from the context menu.

The next step adds a simple client object to the DispClient project.

On to Step 4 | Back to Step 2

See Also

Attributes Tutorial | ATL Article Overview | ATL Class Overview

Show: