Lesson 4 | Generating an in-process COM server |
Objective | Generate an ATL-based in-process COM server. |
Generating an in-process COM Server
Standard Three-Tier Client/Server Architecture
The three-tier architecture shown in Figure 1-1 has recently received a great deal of well-warranted publicity. Because this architecture is particularly applicable to COM and the Active Template Library (ATL), it is worth describing in some detail here. Basically, a client/server system can be divided into
three basic architectural parts, or tiers. Although these tiers don't necessarily correspond to separate physical locations on a network, they represent a logical separation of functionality that gives developers a great degree of flexibility, scalability, and performance.
Client Tier
The client tier provides the graphical user interface (GUI) for presenting and gathering information from the user.
This tier, which resides on a local machine connected to the Internet, an intranet, or a LAN, is generally implemented using a
custom front-end application or, increasingly, a set of Web pages displayed in a browser. Traditional COM-based technologies, such as Microsoft ActiveX controls,
Microsoft Active Documents, and OLE servers and containers, reside almost exclusively in this tier. Development tools such as MFC, Visual Basic, Microsoft
Visual FoxPro, and Microsoft Office are especially well suited for the client tier because of their rich support for user interfaces and OLE.
Middle Tier
The middle tier is a set of components that encapsulate the application logic and business rules of the system.
These components respond to user requests by performing specific business tasks. This tier, which typically resides on one or more
Microsoft Windows NT Server and Microsoft Windows 2000 Server machines, is the home of Microsoft Internet Information Server (IIS), Microsoft Active Server Pages (ASP), Microsoft Internet Server application programming interface (ISAPI) DLLs, lightweight COM objects,
Microsoft Component Services, formerly known as Microsoft Transaction Server (MTS) components, out-of-process COM servers, and
Distributed COM (DCOM) servers. In our opinion, the middle tier is where the power and beauty of COM really shine! Not coincidentally, the middle tier is also the tier best suited for development with ATL.
Data Source Tier
The data source tier defines, stores, and manages access to the system data. This tier, which may reside on a combination of servers and mainframe computers, is typically implemented using a database management system (DBMS) such as Microsoft SQL
Server. By separating the services of the data source from the other components in the system, the structure and access to the data can be modified, scaled, or even rearchitected with minimal effect on the client and middle tiers.
Generating an in-process COM server
Here are the steps required to generate an in-process ATL server:
- We start with Visual C++ and no project or files opened. Open the File menu.
- From the File menu, select New.
- The Projects tab of the New dialog box and the ATL COM AppWizard are selected. The next step is to name the project. Type
PhBook
as the project name. Then click OK.
- The ATL COM AppWizard dialog will come up. Make sure that Dynamic Link Library (DLL) is checked under server type. No other entries should be checked. Click Finish.
- The New Project Information dialog will come up. This lists the project, COM server name, DLL initialization code, IDL source, and ProxyStub makefile. Click OK.
- You have now generated a skeleton ATL-based in-process server. The Class View tab of the Workspace window is selected. Notice that the ATL COM AppWizard has generated all the required in-process server functions, a DLL entry point function (
DllMain
), and a
global instance of CComModule
in variable _Module
. This is the end of the simulation.
The ATL-generated code includes a global instance of CComModule
and the required in-process server functions.
Inside ATL-generated code
The ATL COM AppWizard generates a global instance of CComModule
and the required in-process server functions.
For this project, this code is in file PhBook.cpp. Following are excerpts from this file.
...
CComModule _Module;
BEGIN_OBJECT_MAP(ObjectMap)
END_OBJECT_MAP()
extern "C"
BOOL WINAPI DllMain(HINSTANCE hInstance,
DWORD dwReason, LPVOID /*lpReserved*/)
{
...
}
STDAPI DllCanUnloadNow(void)
{
return (_Module.GetLockCount()==0)
? S_OK : S_FALSE;
}
STDAPI DllGetClassObject(REFCLSID rclsid,
REFIID riid, LPVOID* ppv)
{
return _Module.GetClassObject(rclsid, riid, ppv);
}
STDAPI DllRegisterServer(void)
{
// registers object, typelib and
all interfaces in typelib
return _Module.RegisterServer(TRUE);
}
STDAPI DllUnregisterServer(void)
{
return _Module.UnregisterServer(TRUE);
}
Notice how each DLL function makes a call into _Module
.
BEGIN_OBJECT_MAP and END_OBJECT_MAP are macros. These are used to add a server's COM objects into the ATL framework. We will revisit object maps in the next lesson.
Generate in Process Server - Exercise
Essential COM