📄 ch04.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<!--last modified on Tue, Apr 15, 1997 9:46 AM-->
<HTML>
<HEAD>
<!-- This document was created from RTF source by rtftohtml version 3.0.1 -->
<META NAME="GENERATOR" Content="Symantec Visual Page 1.0">
<META NAME="Author" Content="Steph Mineart">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=iso-8859-1">
<TITLE>Chapter 4</TITLE>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF">
<H1>Chapter 4<BR>
Creating ActiveX Automation Servers Using ATL</H1>
<UL>
<LI><A HREF="#Heading1">Creating ActiveX Automation Servers Using ATL</A>
<UL>
<LI><A HREF="#Heading2">Creating the Basic Project</A>
<LI><A HREF="#Heading3">Adding an Automation Interface to the Application</A>
<UL>
<LI><A HREF="#Heading4">Listing 4.1 ATLSERVER.IDL--IDL File for the ATL Server Sample</A>
</UL>
<LI><A HREF="#Heading5">Registry</A>
<UL>
<LI><A HREF="#Heading6">Listing 4.2 ATLCONTROLWIN.RGS--Sample Registry Script File
for the</A><A HREF="#Heading7">CTracker Server Object</A>
<LI><A HREF="#Heading8">Listing 4.3 TRACKER.RGS--Updated Tracker.rgs File</A>
</UL>
<LI><A HREF="#Heading9">Sample Server Support Code</A>
<UL>
<LI><A HREF="#Heading10">Listing 4.4 Tracker.H--Sample Server Support Code Added
to the</A> <A HREF="#Heading11">Tracker Header File</A>
<LI><A HREF="#Heading12">Listing 4.5 TRACKER.CPP--Sample Server Support Code Added
to the Source File</A>
</UL>
<LI><A HREF="#Heading13">Adding Methods</A>
<UL>
<LI><A HREF="#Heading14">Listing 4.6 ATLSERVER.IDL--OutputLines Method Added to the
IDL File by the ATL ClassWizard</A>
<LI><A HREF="#Heading15">Listing 4.7 TRACKER.H--OutputLines Function Prototype Added
to the</A>
<LI><A HREF="#Heading16">Tracker.h File</A>
<LI><A HREF="#Heading17">Listing 4.8 TRACKER.CPP--OutputLines Function Implementation
Added </A><A HREF="#Heading18">to the Tracker.cpp File</A>
<LI><A HREF="#Heading19">Listing 4.9 TRACKER.H--m_1Indent Member Variable Added to
the</A> <A HREF="#Heading20">Class Definition</A>
<LI><A HREF="#Heading21">Listing 4.10 TRACKER.CPP--Member Initialization in the Constructor</A>
<LI><A HREF="#Heading22">Listing 4.11 TRACKER.CPP--OutputLines Function Implementation
Added to the Source File Tracker.cpp</A>
</UL>
<LI><A HREF="#Heading23">Adding Properties</A>
<UL>
<LI><A HREF="#Heading24">Listing 4.12 TRACKER.IDL--Indent Property Added to the IDL</A>
<LI><A HREF="#Heading25">Listing 4.13 TRACKER.H--Indent Property Function Pair Prototypes
Added to the Tracker.h File</A>
<LI><A HREF="#Heading26">Listing 4.14 TRACKER.CPP--Indent Property Function Pair
Implementation Added to the Tracker.cpp File</A>
<LI><A HREF="#Heading27">Listing 4.15 TRACKER.CPP--Indent Property Implementation</A>
</UL>
<LI><A HREF="#Heading28">Generating OLE Exceptions</A>
<UL>
<LI><A HREF="#Heading29">Listing 4.16 ATLSERVER.IDL--Error Enumeration Added to the
IDL File</A>
<LI><A HREF="#Heading30">Listing 4.17 TRACKER.CPP--Rich Error Information Added to
Tracker</A>
<LI><A HREF="#Heading31">Implementation</A>
</UL>
<LI><A HREF="#Heading32">Dual-Interface</A>
<LI><A HREF="#Heading33">Generating Dual-Interface OLE Exceptions</A>
<LI><A HREF="#Heading34">Server Instantiation Using C++</A>
<LI><A HREF="#Heading35">Shared Servers</A>
<UL>
<LI><A HREF="#Heading36">Listing 4.18 STDAFX.H--Shared Object Support Classes and
Macros</A>
<LI><A HREF="#Heading37">Listing 4.19 TRACKER.H--Shared Server Support Added to Ctracker
Class</A>
</UL>
<LI><A HREF="#Heading38">Single Instance Servers</A>
<UL>
<LI><A HREF="#Heading39">Listing 4.20 STDAFX.G--Single Instance Server Support Added
to StdAfx.h</A>
<LI><A HREF="#Heading40">Listing 4.21 TRACKER.H--Single Instance Server Support Added
to CTracker Class</A>
</UL>
<LI><A HREF="#Heading41">From Here...</A>
</UL>
</UL>
<P>
<HR SIZE="4">
<H1><A NAME="Heading1"></A>Creating ActiveX Automation Servers Using ATL</H1>
<UL>
<LI><B>Server registration support</B>
<SPACER TYPE="VERTICAL" SIZE="2">
Automation servers depend on the registry for information about how they are implemented
and used.
<P>
<LI><B>Adding methods and properties</B>
<SPACER TYPE="VERTICAL" SIZE="2">
Methods allow the user of the server to access specific functionality in the server.
Properties provide a uniform mechanism for getting and setting the state of a server's
data variables.
<P>
<LI><B>OLE exceptions</B>
<SPACER TYPE="VERTICAL" SIZE="2">
Unlike MFC, ATL does not support OLE exceptions through a C++ exception; rather
it takes advantage of OLE error mechanisms.
<P>
<LI><B>Dual-interface</B>
<SPACER TYPE="VERTICAL" SIZE="2">
ATL servers implement dual- interface as a standard feature of the application.
<P>
<LI><B>Creating servers using C++</B>
<SPACER TYPE="VERTICAL" SIZE="2">
C++ can be used to launch servers from the application in which they are defined.
<P>
<LI><B>Shared and single instance servers</B>
<SPACER TYPE="VERTICAL" SIZE="2">
Using a server that is already running is usually the choice of the controller of
the server, not the implementer. With a single instance server, the server implementer
is responsible for the reuse of a server that is already running.
</UL>
<P>The ActiveX Template Library (ATL) was created to answer the need for lightweight
and fast ActiveX COM components. In addition to creating COM Objects and Controls,
ATL can be used to create Automation Servers with a minimum of effort and overhead.
ATL consists of a set of template classes that are a relatively new concept to most
developers; however, the focus of this chapter is on creating Automation Servers.
Please see your favorite C++ book or manual for more information regarding the definition
and use of templates.</P>
<P>ATL is intended to solve the problems of COM com-ponent development and is not
an attempt to be an all-encompassing class library for creating applications. ATL
was designed to work with other class libraries, such as MFC or the Standard Template
Library (STL), that provide basic classes for string manipulation, array, lists,
memory management, and so on. You, the developer, have the freedom to choose which
class library best suits your needs and combine it with ATL to create small, fast
COM applications. In this chapter, you will create a simple in-process Automation
Server using ATL and MFC for logging string data to a file. The use of MFC will allow
you to concentrate on how to implement your server rather than on how to find alternative
methods to functions and classes you are familiar with. You could just as easily
opt not to use MFC, but then you would have to use the Windows API directly to implement
anything related to the OS. As it is, using MFC will not add much to your application
in terms of size.</P>
<P>As you proceed through the chapter, you will expand on your implementation, highlighting
some of the more advanced concepts of Automation Server creation using ATL.
<H2><A NAME="Heading2"></A>Creating the Basic Project</H2>
<P>When creating an Automation Server, the first step is to create a <I>basic project
</I>upon which you will build your application's features and functionality. Like
MFC, ATL has an AppWizard for creating the basic ATL project.</P>
<P>From the <U>F</U>ile menu, select the <U>N</U>ew menu item. In the New dialog
(see fig. 4.1), select the Projects tab. The Projects tab allows you the opportunity
to define several aspects of how the application will be created, for example, the
type of application to create, the name of the application, and the location where
you want the project created. For the type, select ATL COM AppWizard; enter the Project
<U>n</U>ame ATLServer, and the Lo<U>c</U>ation will be <TT>C:\que\ActiveX\ ATLServer</TT>.
Click the OK button to start the ATL COM AppWizard so you can further define the
properties of your server. <A HREF="Art/04/dfig01.jpg"><B>FIG. 4.1</B></A> <I><BR>
Define the new ATL server project in the New dialog.</I></P>
<P>In the ATL COM AppWizard -- Step 1 of 1 dialog (see fig. 4.2), select a Server
Type of <U>D</U>ynamic Link Library (DLL), and check the Support <U>M</U>FC check
box. Click the <U>F</U>inish button to continue. <A HREF="Art/04/dfig02.jpg"><B>FIG.
4.2</B></A> <I><BR>
Define the basic architecture of the ATL COM object with the ATL COM AppWizard.</I></P>
<P>The New Project Information dialog (see fig. 4.3) is used to confirm the settings
that were selected for the project prior to the creation of the actual source files.
This is the last step in the ATL COM AppWizard. "But wait," you say, "I
haven't defined any of my server properties." The ATL COM AppWizard takes a
slightly different approach from that of MFC. Only the basic source files are created
with the AppWizard, the remainder of the project is defined by the ATL ObjectWizard--thus
allowing for much better control of the project implementation versus MFC since the
developer can add any number of ActiveX Servers, Controls, or plain COM objects after
the basic project is created. After you have confirmed your project settings, click
the OK button to close the ATL COM AppWizard and create the ATLServer project. <A
HREF="Art/04/dfig03.jpg"><B>FIG. 4.3</B></A> <I><BR>
Confirm the new project settings with the New Project Information dialog.</I></P>
<P>The ATL AppWizard generates all of the basic files that are needed to create a
DLL-based ATL Automation Server. Table 4.1 lists all of the files that are created
and a brief explanation of their purpose.
<TABLE BORDER="1" WIDTH="100%">
<CAPTION><B>Table 4.1 </B><SPACER TYPE="HORIZONTAL" SIZE="10"><B>Basic Source Files Created
by the ATL AppWizard</B></CAPTION>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><B>Filename</B></TD>
<TD ALIGN="LEFT" VALIGN="TOP"><B>Description</B></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP">ATLServer.clw</TD>
<TD ALIGN="LEFT" VALIGN="TOP">VC++ project file.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP">ATLServer.cpp</TD>
<TD ALIGN="LEFT" VALIGN="TOP">The main application source file and entry point for the DLL.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP">ATLServer.def</TD>
<TD ALIGN="LEFT" VALIGN="TOP">Standard application DEF file. This file contains the function export declarations
needed for all in-process servers.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP">ATLServer.dsp</TD>
<TD ALIGN="LEFT" VALIGN="TOP">VC++ project file.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP">ATLServer.dsw</TD>
<TD ALIGN="LEFT" VALIGN="TOP">VC++ project file.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP">ATLServer.idl</TD>
<TD ALIGN="LEFT" VALIGN="TOP">Interface Definition Language file, which is used to create the type library for
the server.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP">ATLServer.ncb</TD>
<TD ALIGN="LEFT" VALIGN="TOP">VC++ project file.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP">ATLServer.rc</TD>
<TD ALIGN="LEFT" VALIGN="TOP">Standard resource file.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP">StdAfx.cpp</TD>
<TD ALIGN="LEFT" VALIGN="TOP">Standard precompiled header source file.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP">ATLServerps.def</TD>
<TD ALIGN="LEFT" VALIGN="TOP">Proxy/Stub DLL definition file.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP">ATLServerps.mk</TD>
<TD ALIGN="LEFT" VALIGN="TOP">Proxy/Stub MK file.</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP">StdAfx.h</TD>
<TD ALIGN="LEFT" VALIGN="TOP">Standard precompiled header file. All of the MFC-specific include files are added
here.</TD>
</TR>
</TABLE>
<H2><A NAME="Heading3"></A>Adding an Automation Interface to the Application</H2>
<P>To be an <I>Automation Server</I>,<I> </I>an application must contain at least
one or more <TT>IDispatch</TT>-based interfaces. You will use the ATL ObjectWizard
to add your automation server interfaces to your application. From the <U>I</U>nsert
menu, select the New <U>A</U>TL Object menu item. Within the ATL ObjectWizard dialog
(see fig. 4.4), select the Objects item in the left panel to display the types of
ATL components that can be added. Your implementation will be an Automation Server,
so select the Simple Object icon. See the ATL documentation for more information
on the other types of objects that can be created. Click the <U>N</U>ext button to
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -