Tuesday, November 18, 2008

Adding a Method to the server.


We have now created an empty COM object. As of yet, it's still a useless object because it doesn't do anything. We will create a simple method called Beep() which causes the system to beep once. Our COM method will call the Win32 API function ::Beep(), which does pretty much what you would expect.

  • Go to "Class View" tab. Select the IBeepObj interface. This interface is represented by a small icon that resembles a spoon.

  • Right click the IBeepObj interface. Select "Add Method" from the menu.
  • At the "Add Method to Interface" dialog, enter the following and press OK. Add the method "Beep" and give it a single [in] parameter for the duration. This will be the length of the beep, in milliseconds.


  • "Add Method" has created the MIDL definition of the method we defined. This definition is written in IDL, and describes the method to the MIDL compiler. If you want to see the IDL code, double click the "IBeepObj" interface at the "Class View" tab. This will open and display the file BeepServer.IDL. No changes are necessary to this file, but here's what our interface definition should look like.
 
interface IBeepObj : IUnknown
{
     [helpstring("method Beep")]
          HRESULT Beep([in] LONG duration);
};
 

The syntax of IDL is quite similar to C++. This line is the equivalent to a C++ function prototype. We will cover the syntax of IDL in the next issue.

  • Now we're going to write the C++ code for the method. The AppWizard has already written the empty shell of our C++ function, and has added it to the class definition in the header file (BeepServer.H).

Open the source file BeepObj.CPP. Find the //TODO: line and add the call to the API Beep function. Modify the Beep() method as follows:

 
STDMETHODIMP CBeepObj::Beep(LONG duration)
{
     // TODO: Add your implementation code here
     ::Beep( 550, duration );
     return S_OK;
}
 
  • Save the files and build the project.

We now have a complete COM server. When the project finishes building, you should see the following messages:

 
--------------------Configuration: BeepServer - Win32 Debug--------------------
Creating Type Library...
Microsoft (R) MIDL Compiler Version 5.01.0158
Copyright (c) Microsoft Corp 1991-1997. All rights reserved.
Processing D:\UnderCOM\BeepServer\BeepServer.idl
BeepServer.idl
Processing C:\Program Files\Microsoft Visual Studio\VC98\INCLUDE\oaidl.idl
oaidl.idl
.
.
Compiling resources...
Compiling...
StdAfx.cpp
Compiling...
BeepServer.cpp
BeepObj.cpp
Generating Code...
Linking...
   Creating library Debug/BeepServer.lib and object Debug/BeepServer.exp
Performing registration
 
BeepServer.dll - 0 error(s), 0 warning(s)
 

This means that the Developer Studio has completed the following steps:

  • This means that Executed the MIDL compiler to generate code and type libraries
  • This means that Compiled the source files
  • This means that Linked the project to create BeepServer.DLL
  • This means that Registered COM components
  • This means that Registered the DLL with RegSvr32 so it will automatically load when needed.

Let's look at the project that we've created. While we've been clicking buttons, the AppWizard has been generating files. If you look at the "FileView" tab, the following files have been created:

Source File

Description

BeepServer.dsw

Project workspace

BeepServer.dsp

Project File

BeepServer.plg

Project log file. Contains detailed error information about project build.

BeepServer.cpp

DLL Main routines. Implementation of DLL Exports

BeepServer.h

MIDL generated file containing the definitions for the interfaces

BeepServer.def

Declares the standard DLL module parameters: DllCanUnloadNow, DllGetClassObject, DllUnregisterServer

BeepServer.idl

IDL source for BeepServer.dll. The IDL files define all the COM components.

BeepServer.rc

Resource file. The main resource here is IDR_BEEPDLLOBJ which defines the registry scripts used to load COM information into the registry.

Resource.h

Microsoft Developer Studio generated include file.

StdAfx.cpp

Source for precompiled header.

Stdafx.h

Standard header

BeepServer.tl b

Type Library generated by MIDL. This file is a binary description of COM interfaces and objects. The TypeLib is very useful as an alternative method of connecting a client.

BeepObj.cpp

Implementation of CBeepObj. This file contains all the actual C++ code for the methods in the COM BeepObj object.

BeepObj.h

Definition of BeepObj COM object.

BeepObj.rgs

Registry script used to register COM components in registry. Registration is automatic when the server project is built.

BeepServer_i.c

Contains the actual definitions of the IID's and CLSID's. This file is often included in cpp code.

There are several other proxy/stub files that are generated by MIDL.

In just a few minutes, we have created a complete COM server application. Back in the days before wizards, writing a server would have taken hours. Of course the down-side of wizards is that we now have a large block of code that we don't fully understand. In the next section we will look at the generated modules in detail, and then as a whole working application.

0 comments: