DCOM Sample: Calls COM Object in Windows Service from Multiple Clients

The DCOM sample demonstrates how to call a COM object implemented in a Windows service from multiple clients running on different computers. It is composed of several parts:

  • DrawServ   The Windows service that implements the COM object.

  • ATLDraw   The client that connects to the DrawServ COM object.

  • DrawCtl   A control version of ATLDraw. It has a Connect method (that takes a machine name string parameter), a Disconnect method, and a Clear method.

  • MFCCont   An MFC container for DrawCtl. Enter the name of the (possibly remote) server without quotes or slashes.

  • ATLCont   An ATL container for DrawCtl. Enter the name of the (possibly remote) server without quotes or slashes.

All computers must be running Windows 2000 or later for this sample to work.

Compare this sample with the ATL DCOM sample.

Security noteSecurity Note:

This sample code is provided to illustrate a concept and should not be used in applications or Web sites, as it may not illustrate the safest coding practices. Microsoft assumes no liability for incidental or consequential damages should the sample code be used for purposes other than as intended.

To get samples and instructions for installing them:

  • On the Visual Studio Help menu, click Samples.

    For more information, see Visual Studio Samples.

  • The most recent version and complete list of samples is available online from the Visual Studio 2008 Samples page.

  • You can also locate samples on your computer's hard disk. By default, samples and a Readme file are copied into a folder under \Program Files\Visual Studio 9.0\Samples\. For Express editions of Visual Studio, all samples are located online.

Building and Running the Sample

To build and run this sample

  1. Open the solution dcom.sln.

  2. Build the DrawServ and ATLDraw samples.

  3. Copy Drawserv.exe and Atldraw.exe to each computer that you want to run the sample from. Register the server on each computer by running DrawServ with the command-line argument /RegServer or -RegServer (case-insensitive). For example:

    DrawServ /RegServer
    

    (Register.dll must be registered for this to work.) The reason you need to copy the server to each client is that the CLSID for the server and the server type library need to be registered on the client.

  4. Start the service on the server by using the Services icon in Control Panel.

  5. Using DCOMCNFG on each client, select the Properties of the DrawServ Class. From the Location tab, select Run application on the following computer, then enter the name of the computer on which you are running the server object. Clear the Run application on this computer check box.

  6. Run ATLDraw and select Server. Connect from the menu on each client. Draw on the client window by holding the left mouse button down and dragging a line. The drawn line should appear on each client that is also connected to the same server. You can also use the Color command from the View menu to change the color for each client.

  7. If there is any problem connecting to a remote server (for example, if Tstcon32.exe does not call CoInitializeSecurity), run DCOMCNFG on the client and open the tab Default Properties. Set Default Authentication Level to None and Default Impersonation Level to Anonymous.

See Also

Tasks

DCOM Sample: Demonstrates Remotely Calling a COM Object

Reference

Compiler COM Support

Other Resources

Compiler COM Support Samples