Wednesday, November 19, 2008

Server using the ATL Wizard


We're going to create a very simple COM server in this example in order to eliminate clutter and help you to understand the fundamental principles behind COM very quickly. The server will only have one method -- Beep(). All that this method will do is sound a single beep - not a very useful method. What we're really going to accomplish is to set up all the parts of a working server. Once the infrastructure is in place, adding methods to do something useful will be extremely straightforward.

The ATL AppWizard is an easy way to quickly generate a working COM server. The Wizard will allow us to select all the basic options, and will generate most of the code we need. Below is the step-by step process for creating the server. In this process we will call the server BeepServer. All COM servers must have at least one interface, and our interface will be called IBeepObj. You can name your COM interfaces almost anything you want, but you should always prefix them with an 'I' if you want to follow standard naming conventions.

NOTE: If you find the difference between a COM "Object" , "Class", and "Interface" confusing at this point, you're not alone. The terminology can be uncomfortable initially, especially for C++ programmers. The feelings of confusion will subside as you work through examples. The word "coclass" for COM class is used in most Microsoft documentation to distinguish a COM class from a normal C++ class.

Here are the steps for creating a new COM server with the ATL Wizard using Visual C++ version 6 (it looks nearly identical in version 5 as well):

  • First, create a new "ATL COM AppWizard" project. Select File/New from the main menu.
  • Select the "Projects" tab of the "New" dialog. Choose "ATL COM AppWizard" from the list of project types. Select the following options and press OK.
  • Project Name: BeepServer
  • Create New Workspace
  • Location: Your working directory.

Figure 4-1 - Click to see the full size image. Use the Browser BACK button to return.

Figure 4-1

  • At the first AppWizard dialog we'll create a DLL based (In-process) server. Enter the following settings :
  • Dynamic Link Library
  • Don't allow merging proxy/stub code
  • Don't support MFC

Figure 4-2 - Click to see full-size image. Use your browser's BACK button to return.

Figure 4-2

  • Press Finish.

The AppWizard creates a project with all the necessary files for a DLL-based COM server. Although this server will compile and run, it's just an empty shell. For it to be useful it will need a COM interface and the class to support the interface. We'll also have to write the methods in the interface.

0 comments: