![]() |
![]() |
|||||||
|
Login Change Info Logout
DOWNLOADS |
Microsoft's Distributed Component Object Model Revealed Adapted from Understanding DCOM Copyright © 1998 by William Rubin and Marshall Brain Understanding a Simple DCOM Server So far we've looked at how to use COM through a client application. To the client, the mechanics of COM programming are pretty simple. The client application asks the COM subsystem for a particular component, and it is magically delivered. There's a lot of code required to make all this behind-the-scenes component management work. The actual implementation of the object requires a complex choreography of system components and standardized application modules. Even using MFC the task is complex. Most professional developers don't have the time to slog through this process. As soon as the COM standard was published, it was quickly clear that it wasn't practical for developers to write this code themselves. When you look at the actual code required to implement COM, you realize that most of it is repetitive boilerplate. The traditional C++ approach to this type of complexity problem would be to create a COM class library. And in fact, the MFC OLE classes provide most of COMs features. There are however, several reasons why MFC and OLE were not a good choice for COM components. With the introduction of ActiveX and Microsoft's Internet strategy, it was important for COM objects to be very compact and fast. ActiveX requires that COM objects be copied across the network fairly quickly. If you've worked much with MFC you'll know it is anything but compact (especially when statically linked). It just isn't practical to transmit huge MFC objects across a network. Perhaps the biggest problem with the MFC/OLE approach to COM components is the complexity. OLE programming is difficult, and most programmers never get very far with it. The huge number of books about OLE is a testament to the fact that it is hard to use. Because of the pain associated with OLE development, Microsoft created a new tool called ATL (Active Template Library). For COM programming, ATL is definitely the most practical tool to use at the present. In fact, using the ATL wizard makes writing COM servers quite easy if you don't have any interest in looking under the hood. The examples here are built around ATL and the ATL Application Wizard. This chapter describes how to build an ATL based server and gives a summary of the code that the wizard generates. Where's the Code? One of the things that takes some getting used to about writing ATL servers is that they don't look like traditional programs. A COM server is really a collaboration between several disparate components:
It can be difficult to look at an ATL based COM application and see it as a unified whole. Even when you know what it's doing, there are still big chunks of the application that you can't see. Most of the real server logic is hidden deep within the ATL header files. You won't find a single main() function that manages and controls the server. What you will find is a thin shell that makes calls to standard ATL objects. In the following section we're going to put together all the pieces required to get the server running. First we will create the server using the ATL COM AppWizard. The second step will be to add a COM object and a Method. We'll write an In-Process server because it's one of the simpler COM servers to implement. An In-process server also avoids having to build a proxy and stub object. Building a DLL Based (In-Process) COM Server An In-Process server is a COM library that gets loaded into your program at run-time. In other words, it's a COM object in a Dynamic Link Library (DLL). A DLL isn't really a server in the traditional sense, because it loads directly into the client's address space. If you're familiar with DLLs, you already know a lot about how the COM object gets loaded and mapped into the calling program. Normally a DLL is loaded when LoadLibrary() is called. In COM, you never explicitly call LoadLibrary(). Everything starts automatically when the client program calls CoCreateInstance(). One of the parameters to CoCreateInstance is the GUID of the COM class you want. When the server gets created at compile time, it registers all the COM objects it supports. When the client needs the object, COM locates the server DLL and automatically loads it. Once loaded, the DLL has a class factory to create the COM object. CoCreateInstance() returns a pointer to the COM object, which is in turn used to call a method (in the example described here, the method is called Beep().) A nice feature of COM is that the DLL can be automatically unloaded when it's not needed. After the object is released and CoUninitialize() is called, FreeLibrary() will be called to unload the server DLL. If you didn't follow all that, it's easier than it sounds. You don't have to know anything about DLL's to use COM. All you have to do is call CoCreateInstance(). One of the advatages of COM is that it hides these details so you don't have to worry about this type of issue. There are advantages and disadvantages to In-process COM servers. If dynamic linking is an important part of your system design, you'll find that COM offers an excellent way to manage DLL's. Some experienced programmers write all their DLL's as In-process COM servers. COM handles all the chores involved with the loading, unloading, and exporting DLL functions and COM function calls have very little additional overhead. Our main reason for selecting an In-process server is somewhat more prosaic: It makes the example simpler. We won't have to worry about starting remote servers (EXE or service) because our server is automatically loaded when needed. We also avoid building a proxy/stub DLL to do the marshalling. Unfortunately, because the In-Process server is so tightly bound to our client, a number of the important "distributed" aspects of COM are not going to be exposed. A DLL server shares memory with it's client, whereas a distributed server would be much more removed from the client. The process of passing data between a distributed client and server is called marshaling. Marshaling imposes important limitations on COM's capabilities that we won't have to worry about with an in-proc server. Creating the 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):
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. Adding a COM object and a Method Now we'll proceed with the definition of the COM object, the interface, and the methods. This class is named BeepObj and has an interface called IBeepObj:
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.
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. 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;
}
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:
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:
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. Summary The server code was almost entirely generated by the ATL wizards. It provides a working implementation of the server. We examined a DLL based server, but the process is almost identical for all server types. This framework is an excellent way to quickly develop a server application because you don't have to know the myriad of details required to make it work. In the next article of this series, we will look at In-Proc Servers and ATL code.
|
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Questions or Comments? devcentral AT iticentral DOT com PRIVACY POLICY |