⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch13.htm

📁 用VC开发activeX控件的电子书,很不错的
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<!--last modified on Tue, Apr 15, 1997 1:30 PM-->
<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 13</TITLE>
</HEAD>

<BODY TEXT="#000000" BGCOLOR="#FFFFFF">

<H1>Chapter 13<BR>
Creating ActiveX COM Objects and Custom Interfaces Using ATL</H1>

<UL>
	<LI><A HREF="#Heading1">Creating ActiveX COM Objects and Custom Interfaces Using
	ATL</A>
	<UL>
		<LI><A HREF="#Heading2">Reaping Benefits of the ActiveX Template Library</A>
		<UL>
			<LI><A HREF="#Heading3">Support for Multiple Server Types</A>
			<LI><A HREF="#Heading4">Threading Models Supported by ATL</A>
			<LI><A HREF="#Heading5">Tear-Off Interfaces</A>
			<LI><A HREF="#Heading6">Implementing Interface Aggregation</A>
			<LI><A HREF="#Heading7">Built-In Support for Error Handling</A>
		</UL>
		<LI><A HREF="#Heading8">Creating a COM Server Using ATL</A>
		<UL>
			<LI><A HREF="#Heading9">Using the ATL COM Wizard to Create a COM Server</A>
			<LI><A HREF="#Heading10">Examining the Results of the ATL COM Wizard</A>
			<LI><A HREF="#Heading11">Implementing the COM Server Access Functions</A>
			<LI><A HREF="#Heading12">Listing 13.1 AtlCustomBass.def--Library Definition File
			for CAtlCustomBass</A>
			<LI><A HREF="#Heading13">Listing 13.2 AtlCustomBass.cpp--ATL Implementation of DLL
			Access Functions for COM Server</A>
			<LI><A HREF="#Heading14">Using IDL to Create Object Definitions</A>
			<LI><A HREF="#Heading15">Listing 13.3 AtlCustomBass.idl--Adding Custom Interface
			Methods to the Project Interface Definition File</A>
			<LI><A HREF="#Heading16">Implementing the COM Interface</A>
			<LI><A HREF="#Heading17">Listing 13.4 AtlCustomBass.h--C++ Class Definition for the
			AtlCustomBass COM Server</A>
			<LI><A HREF="#Heading18">Listing 13.5 CAltCustomBass1.cpp--Listing of CAltCustomBass1.cpp,
			Which Implements the COM Interfaces for IFish and IBass</A>
			<LI><A HREF="#Heading19">Using Object Maps to Specify COM Objects</A>
		</UL>
		<LI><A HREF="#Heading20">When to Use the ActiveX Template Library</A>
		<LI><A HREF="#Heading21">From Here...</A>
	</UL>
</UL>

<P>
<HR SIZE="4">

<H1><A NAME="Heading1"></A>Creating ActiveX COM Objects and Custom Interfaces Using
ATL</H1>

<UL>
	<LI><B>Benefits of using ATL</B> 
<SPACER TYPE="VERTICAL" SIZE="2">
 ATL allows for the creation of many types of COM servers and COM models.
	<P>
	<LI><B>Easy creation of COM interfaces and classes</B> 
<SPACER TYPE="VERTICAL" SIZE="2">
 The ATL project wizard produces template code for the creation of COM classes and
	interfaces.
	<P>
	<LI><B>Implementing tear-off interfaces</B> 
<SPACER TYPE="VERTICAL" SIZE="2">
 A tear-off interface is an interface that is allocated on demand rather than upon
	COM class allocation.
	<P>
	<LI><B>Using custom interfaces in ATL</B> 
<SPACER TYPE="VERTICAL" SIZE="2">
 Most COM objects support more than one interface; using ATL makes support for more
	than one interface an easy task.
	<P>
	<LI><B>When to use ATL for COM server development</B> 
<SPACER TYPE="VERTICAL" SIZE="2">
 You'll find situations where using other techniques, such as the MFC framework,
	provides more benefits than using ATL.
</UL>

<P>In the past, designing COM objects was relegated to the use of large application
frameworks such as MFC or to building your own components. Using large application
frameworks eases the construction and implementation of COM objects but requires
a significant amount of overhead code for the building and distribution of the COM
servers. Conversely, building your own COM server framework results in fast, lightweight
objects but requires a significant amount of up-front programming to implement the
control.</P>
<P>To address this dilemma, Microsoft created the ActiveX Template Library (ATL),
which provides a middle ground between large application frameworks and building
your own COM objects. ATL is a set of template-based C++ classes that simplifies
the programming of COM objects. ATL provides the necessary COM foundation, allowing
the focus to be on programming the functionality of your objects. ATL is shipped
with Visual C++ 5.0 and is backward compatible with Visual C++ 4.1 and 4.2. ATL can
be downloaded separately from the Microsoft Web site at <A HREF="http://www.microsoft.com" target="_top"><B>http://www.microsoft.com</B></A>.
<BR>
<BR>
<IMG SRC="bar.gif" WIDTH="480" HEIGHT="6" ALIGN="BOTTOM" BORDER="0"></P>


<BLOCKQUOTE>
	<P><B>NOTE:</B> Users of Visual C++ 4.0 must upgrade to version 4.1 or higher of
	the Visual C++ compiler in order to use the ActiveX Template Library.

</BLOCKQUOTE>

<P><IMG SRC="bar.gif" WIDTH="480" HEIGHT="6" ALIGN="BOTTOM" BORDER="0"><BR>
<BR>
The goal of ATL is to allow for the easy creation of small, fast COM servers. This
goal has been achieved by the following:

<UL>
	<LI>Eliminating the need for static libraries (LIBs) or Dynamic Link Libraries (DLLs)
	<P>
	<LI>Eliminating the need for C runtime library start-up code
</UL>

<P>Static library and DLL dependencies are removed by providing all of the source
code for the ATL libraries. The source code for ATL is a set of C++ class templates.
The small set of ATL code gets compiled into the COM server during the building process.
The overhead of ATL in an in-process COM server is less than 5K.
<H2><A NAME="Heading2"></A>Reaping Benefits of the ActiveX Template Library</H2>
<P>The ActiveX Template Library is the first attempt to create a C++ framework with
the sole intention of creating COM objects. Since COM object creation was the primary
goal of the framework, ATL has been trimmed of unnecessary baggage such as bloated
UI components. The use of ATL provides developers with a number of benefits including
the following:

<UL>
	<LI>Support for multiple types of COM servers, including in-process servers, local
	servers, service servers, and DCOM servers
	<P>
	<LI>Support for multiple-threading models, including the standard single-threading
	model, apartment-model threading, and free-threading
	<P>
	<LI>Various interface types, including custom COM interfaces, dual interfaces, tear-off
	interfaces, and <TT>IDispatch</TT> (OLE automation) interfaces
	<P>
	<LI>Enumeration support through the <TT>IEnumXXX</TT> interface
	<P>
	<LI>OLE error-handling through the <TT>IErrorInfo</TT> interface
</UL>

<H3><A NAME="Heading3"></A>Support for Multiple Server Types</H3>
<P>One of the biggest benefits of using ATL is the support for the creation of multiple
COM server types. When using the ATL wizard, the shell classes for each server type
are automatically created during project initialization. ATL provides support for
the following types of COM servers.

<UL>
	<LI>In-process server--An <I>in-process server</I> is implemented as a DLL that exists
	and can be accessed only on the computer in which the server is installed. In-process
	servers are typically small and fast and are the most common type of COM server.
	An example of an in-process server is a spelling-checker COM object.
	<P>
	<LI>Local server--A <I>local server</I> is implemented as an EXE. Like the in-process
	server, the local server can be accessed only on the computer in which the server
	is installed. The business charting application Visio is an example of a local server.
	<P>
	<LI>Service server--A <I>service server</I> is implemented as an EXE and can be run
	only on the Windows NT operating system. Services are analogous to UNIX daemons,
	which are background tasks that are running but not directly controlled by the system
	user. A service is accessible to all users of the system and is started during system
	boot-up, as opposed to traditional programs, which are started only <I>after</I>
	a user logs on to the computer. A database server that supports COM is an example
	of a service server.
	<P>
	<LI>Remote server--A <I>remote server</I> is implemented as an EXE and is accessible
	from remote computers using either DCOM or Remote Automation. Remote servers take
	advantage of the distributed computing by allowing the server to provide services
	to client applications not located on the same computer as the server. A database
	server is also a great example of a remote server.
</UL>

<H3><A NAME="Heading4"></A>Threading Models Supported by ATL</H3>
<P>ActiveX has different threading models that can be utilized by COM servers. The
ATL library provides built-in support for these different types of threading. Each
model provides different capabilities, and care must be taken when deciding which
model will be supported by the COM server.

<UL>
	<LI>Single-threading model--This model is by far the most restrictive of all models
	supported by ActiveX. This model provides support for only one thread to create,
	use, and access OLE objects. This model is obsolete and is supported only because
	the original OLE architecture was implemented on the 16-bit Windows 3.x platform,
	which did not support multiple threads. This is the default threading model for COM
	servers.
	<P>
	<LI>Apartment-model threading--This model fills the gap between the single-threaded
	model and the free-threaded model. While multiple threads can be utilized to access
	COM objects, care must be taken in how and when these objects are accessed. Each
	thread using COM objects must call the OLE initialization routines. In apartment-model
	threading, global variables accessed by each thread must be protected against simultaneous
	access.
	<P>
	<LI>Free-threading model--The free-threading model is by far the most flexible and
	unrestrictive. This model allows multiple threads to implement, access, and use COM
	objects. This mode of threading is supported <I>only</I> in the Windows NT 4.0 environment.
</UL>

<P><IMG SRC="bar.gif" WIDTH="480" HEIGHT="6" ALIGN="BOTTOM" BORDER="0"></P>


<BLOCKQUOTE>
	<P><B>CAUTION:<BR>
	</B>When implementing free-threading servers, the burden for protecting data within
	a COM class from simultaneous updates or access by multiple threads falls on the
	programmer. Multiple threads may be attempting to access local data within the same
	instance of a COM object. ATL does <I>not</I> provide built-in data access synchronization.
	The use of Win32 synchronization objects such as events, semaphores, mutexes, and
	critical sections is needed for protecting COM class data.

</BLOCKQUOTE>

<P><IMG SRC="bar.gif" WIDTH="480" HEIGHT="6" ALIGN="BOTTOM" BORDER="0">
<H3><A NAME="Heading5"></A>Tear-Off Interfaces</H3>
<P>A <I>tear-off interface, </I>a new concept introduced in the ATL framework, is
an optimization of a regular COM interface in that it doesn't actually exist until
it is instantiated by a call to <TT>QueryInterface</TT> on your object for that interface.
Since the interface does not exist until asked for, it does not consume system memory
resources.</P>
<P>When the <TT>Release</TT> method is called on the interface and the reference
count on that interface returns to zero, the interface is removed from memory. Typically,
tear-off interfaces are used only for those interfaces that are expected to be used
less often than others, such as <TT>ISupportErrorInfo</TT>. <BR>
<BR>
<IMG SRC="bar.gif" WIDTH="480" HEIGHT="6" ALIGN="BOTTOM" BORDER="0"></P>


<BLOCKQUOTE>
	<P><B>NOTE:</B> Tear-off interfaces should not be used for commonly used interfaces
	because the overhead of memory allocation and deallocation and memory fragmentation
	would outweigh the benefits of the interface.

</BLOCKQUOTE>

<P><IMG SRC="bar.gif" WIDTH="480" HEIGHT="6" ALIGN="BOTTOM" BORDER="0"><BR>
<BR>
To implement tear-off interfaces, declare a class that inherits from all the interfaces
you want to implement in the tear-off, as well as from <TT>CComTearOffObjectBase&lt;class
Owner&gt;</TT>, where <I>Owner</I> is the class of the main object. Then provide
a normal <TT>BEGIN_COM_MAP...END_COM_MAP()</TT> specification of interfaces in the
tear-off, and use the <TT>COM_INTERFACE_ENTRY_TEAR_OFF</TT> macro in the main object's
COM map.
<H3><A NAME="Heading6"></A>Implementing Interface Aggregation</H3>
<P>You can implement aggregation in ATL servers with very little work. <I>Aggregation</I>
is when an object exposes another object's interface pointer as its own. For example,
if an application has a pointer to interface A and needs to access interface B, and
if interface A supports aggregation, the application can call <TT>QueryInterface</TT>
on interface A to obtain interface B. The only penalty imposed for supporting aggregation
is needing a somewhat larger server. The benefit is the flexibility to expose interfaces
from objects contained in the server.</P>
<P>In order to make a server aggregatable, use the macro <TT>DECLARE_AGGREGATABLE</TT>
in the COM object's class. If aggregration is not desired, use the macro <TT>DECLARE_NOT_AGGREGATABLE</TT>
to disable aggregration. By default, aggregration is supported by projects created
with the ATL COM AppWizard.
<H3><A NAME="Heading7"></A>Built-In Support for Error Handling</H3>
<P>ATL supports the OLE error reporting mechanism with the <TT>Error()</TT> member
function in the <TT>CComCoClass</TT> and <TT>CComISupportErrorInfoImpl</TT> classes.
These classes each have a member, <TT>InterfaceSupportsErrorInfo()</TT>, that indicates
whether returning rich error information is supported. By using this mechanism, custom
COM interfaces can provide helpful information to the end user if error situations
are encountered.
<H2><A NAME="Heading8"></A>Creating a COM Server Using ATL</H2>
<P>When using the ActiveX Template Library, the creation of COM servers is a trivial
task. The ATL installation creates an ATL COM AppWizard that can be accessed from
the Visual C++ development environment. The ATL COM AppWizard, like the MFC AppWizard,
presents the user with a step-by-step set of options for the creation of a COM server.
The end result of running the wizard is a ready-to-be-built project with all necessary
class template source code for the COM classes and interfaces that will be implemented
within the project. <BR>
<BR>
<IMG SRC="bar.gif" WIDTH="480" HEIGHT="6" ALIGN="BOTTOM" BORDER="0"></P>


<BLOCKQUOTE>
	<P><B>NOTE:</B> In <A HREF="ch12.htm">Chapter 12</A>, an interface library entitled
	<TT>IFish</TT> was created. The <TT>IFish</TT> and <TT>IBass</TT> interfaces built
	in that example will be constructed using the ATL library. The project <TT>AtlCustomBass</TT>
	will create a COM class <TT>CAtlCustomBass</TT>, used for accessing the <TT>IFish</TT>
	interfaces. The <TT>CAtlCustomBass</TT> class will be implemented as an in-process
	server.

</BLOCKQUOTE>

<P><IMG SRC="bar.gif" WIDTH="480" HEIGHT="6" ALIGN="BOTTOM" BORDER="0">
<H3><A NAME="Heading9"></A>Using the ATL COM Wizard to Create a COM Server</H3>
<P>To get started using the ATL COM AppWizard, a new project must be created. A new
project can be created by performing the following steps:

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -