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

📄 ch03.htm

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

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

<H1>Chapter 3<BR>
<A NAME="Heading1"></A>Creating ActiveX Automation Servers Using MFC</H1>

<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 3.1 MFCSERVER.ODL--Dispinterface and CoClass ODL
			Entries</A>
			<LI><A HREF="#Heading5">Listing 3.2 TRACKER.H--Add the Class Factory Support with
			the Macro DECLARE_OLECREATE</A>
			<LI><A HREF="#Heading6">Listing 3.3 TRACKER.CPP--Add the Class Factory Implementationwith
			the IMPLEMENT_OLECREATE Macro</A>
		</UL>
		<LI><A HREF="#Heading7">Registry</A>
		<UL>
			<LI><A HREF="#Heading8">Server Registration</A>
			<LI><A HREF="#Heading9">Server Unregistration</A>
		</UL>
		<LI><A HREF="#Heading10">Sample Server Support Code</A>
		<UL>
			<LI><A HREF="#Heading11">Listing 3.4 TRACKER.H-- Sample Server Support Code Added
			to the Header File</A>
			<LI><A HREF="#Heading12">Listing 3.5 TRACKER.CPP--Updated Source File</A>
		</UL>
		<LI><A HREF="#Heading13">Adding Methods</A>
		<UL>
			<LI><A HREF="#Heading14">Listing 3.6 MFCSERVER.ODL--Updated ODL Entry for OutputLines
			Method</A>
			<LI><A HREF="#Heading15">Listing 3.7 TRACKER.H--New Member Variable Added to the
			Tracker Class</A>
			<LI><A HREF="#Heading16">Listing 3.8 TRACKER.CPP--Member Initialization in the Constructor</A>
			<LI><A HREF="#Heading17">Listing 3.9 TRACKER.CPP--OutputLines Method Implementation</A>
		</UL>
		<LI><A HREF="#Heading18">Adding Properties</A>
		<UL>
			<LI><A HREF="#Heading19">Listing 3.10 TRACKER.CPP--Indent Property Implementation</A>
		</UL>
		<LI><A HREF="#Heading20">Generating OLE Exceptions</A>
		<UL>
			<LI><A HREF="#Heading21">Listing 3.11 MFCSERVER.ODL--Error Enumeration</A>
			<LI><A HREF="#Heading22">Listing 3.12 TRACKERERROR.H--Tracker Error Constants</A>
			<LI><A HREF="#Heading23">Listing 3.13 TRACKER.CPP--Exception Handling Code Added
			to the Source Files</A>
		</UL>
		<LI><A HREF="#Heading24">Dual-Interface</A>
		<UL>
			<LI><A HREF="#Heading25">Listing 3.14 MFCSERVER.ODL--ODL Changes to Support Dual-Interface</A>
			<LI><A HREF="#Heading26">Listing 3.15 TRACKER.CPP--ODL-Generated Header File Is Added
			to the Tracker Source File</A>
			<LI><A HREF="#Heading27">Listing 3.16 TRACKER.H--Interface Macro Update of the Tracker
			Class Definition</A>
			<LI><A HREF="#Heading28">Listing 3.17 TRACKER.CPP--Interface Implementation of the
			ITracker Interface</A>
			<LI><A HREF="#Heading29">Listing 3.18 TRACKER.CPP--IDispatch Function Implementation
			for a Dual-Interface Server</A>
			<LI><A HREF="#Heading30">Listing 3.19 TRACKER.CPP--ITracker Function Implementation</A>
		</UL>
		<LI><A HREF="#Heading31">Generating Dual-Interface OLE Exceptions</A>
		<UL>
			<LI><A HREF="#Heading32">Listing 3.20 ERRORINFOMACROS.H--ISupportErrorInfo Helper
			Macros</A>
			<LI><A HREF="#Heading33">Listing 3.21 TRACKER.CPP--ISupportErrorInfo Include File</A>
			<LI><A HREF="#Heading34">Listing 3.22 TRACKER.H--ISupportErrorInfo Class Declaration</A>
			<LI><A HREF="#Heading35">Listing 3.23 TRACKER.CPP--ISupportErrorInfo Interface Implementation</A>
			<LI><A HREF="#Heading36">Listing 3.24 TRACKER.CPP--Custom Interface Exception Handling
			Code</A>
		</UL>
		<LI><A HREF="#Heading37">Server Instantiation Using C++</A>
		<LI><A HREF="#Heading38">Shared Servers</A>
		<UL>
			<LI><A HREF="#Heading39">Listing 3.25 TRACKER.H--Shared Object Member Variable Added
			to the CTracker Class</A>
			<LI><A HREF="#Heading40">Listing 3.26 TRACKER.CPP--CLSID Declaration</A>
			<LI><A HREF="#Heading41">Listing 3.27 TRACKER.CPP--RegisterActiveObject Added to
			the CTracker Constructor</A>
			<LI><A HREF="#Heading42">Listing 3.28 TRACKER.CPP--RevokeActiveObject Added to the
			Server</A>
		</UL>
		<LI><A HREF="#Heading43">Single Instance Servers</A>
		<UL>
			<LI><A HREF="#Heading44">Listing 3.29 SHAREDOBJECT.H--Shared Server Class Factory
			Header File</A>
			<LI><A HREF="#Heading45">Listing 3.30 SHAREDOBJECT.CPP--Shared Server Implementation
			File</A>
			<LI><A HREF="#Heading46">Listing 3.31 TRACKER.H--Shared Server Class Factory Support
			Added to the Class Definition</A>
			<LI><A HREF="#Heading47">Listing 3.32 TRACKER.CPP--Shared Server Update to Class
			Implementation</A>
			<LI><A HREF="#Heading48">Listing 3.33 TRACKER.CPP--Shared Server Release Implementation</A>
		</UL>
		<LI><A HREF="#Heading49">From Here...</A>
	</UL>
</UL>

<P>
<HR SIZE="4">

<UL>
	<LI><B>Methods and properties</B> 
<SPACER TYPE="VERTICAL" SIZE="2">
 MFC Class Wizard will greatly reduce the time required to implement your server.
	<P>
	<LI><B>OLE exceptions</B> 
<SPACER TYPE="VERTICAL" SIZE="2">
 MFC uses the class COleDispatchException for generating errors.
	<P>
	<LI><B>Dual-interface</B> 
<SPACER TYPE="VERTICAL" SIZE="2">
 The user of the Automatic Server users can choose between an IDispatch or COM interface
	when accessing the server. Dual-interface is not supported directly by MFC.
	<P>
	<LI><B>Dual-interface OLE exceptions</B> 
<SPACER TYPE="VERTICAL" SIZE="2">
 In dual-interface support, OLE exceptions are generated differently from standard
	MFC COleDispatchExceptions.
	<P>
	<LI><B>Creating servers using C++</B> 
<SPACER TYPE="VERTICAL" SIZE="2">
 C++ can be used to launch servers from the application they are defined in.
	<P>
	<LI><B>Shared and single instance servers</B> 
<SPACER TYPE="VERTICAL" SIZE="2">
 Accessing an already running server may be required. With a single instance server,
	the server implement is responsible for the reuse of an already running server.
</UL>

<P>MFC and Visual C++ (VC++) provide a very simple and easy to use framework for
creating ActiveX Automation Servers. In fact, the VC++ development environment's
AppWizard and ClassWizard are implemented with this in mind. Creating and manipulating
automation interfaces is one of VC++'s primary functions.</P>
<P>In this chapter, you will create a simple in-process Automation Server using MFC
for logging string data to a file. Throughout this chapter, you can use an application
such as Visual Basic (VB) to test your implementation. VB is perfect for accessing
Automation Servers since it takes so little time and code to do so.</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.
<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. MFC
provides an AppWizard that greatly simplifies this process. The AppWizard consists
of a set of structured dialogs and choices that, in the end, will result in a set
of files representing the basic application's project.</P>
<P>To create the basic project, you need to open the VC++ Integrated Development
Environment (IDE) and from the <U>F</U>ile menu, select the <U>N</U>ew menu item.
Select the Projects tab in the New dialog, and select MFC AppWizard (dll) as the
type. Enter the Project <U>n</U>ame MFCServer, and set the Lo<U>c</U>ation to the
\Que\ActiveX\MFCServer directory (see fig. 3.1). Click OK to continue. <B><BR>
<BR>
</B><A HREF="Art/03/cfig1r.jpg"><B>FIG. 3.1</B></A> <BR>
<I>Select the application type, name, and location of your new project.</I></P>
<P>In the MFC AppWizard -- Step 1 of 1 dialog, you define the specifics about how
your application is going to be created (see fig. 3.2). For the type of DLL to create,
select <U>R</U>egular DLL with MFC statically linked, which results in a slightly
larger application but one that should load faster because you won't have to load
the MFC DLLs whenever the server is launched. Also, check the A<U>u</U>tomation check
box--since that is the reason you are creating the application in the first place.
Click the <U>F</U>inish button to continue. <B><BR>
<BR>
</B><A HREF="Art/03/cfig2r.jpg"><B>FIG. 3.2</B></A> <BR>
<I>Define the specific application features in the MFC AppWizard -- Step 1 of 1 dialog.</I></P>
<P>The New Project Information dialog allows you to review your choices before creating
the actual project (see fig. 3.3). Click OK to complete the creation of your project.
<B><BR>
<BR>
</B><A HREF="Art/03/cfig4r.jpg"><B>FIG. 3.3</B></A> <I><BR>
Confirm the project settings in the New Project Information dialog.</I></P>
<P>The MFC AppWizard will create all the basic files that are needed to create a
DLL-based Automation Server. Table 3.1 lists all of the files that are created for
you and a brief explanation of what they are used for. 
<TABLE BORDER="1" WIDTH="100%">
	<CAPTION><B>Table 3.1 </B><SPACER TYPE="HORIZONTAL" SIZE="10"><B>Basic Source Files Created
		by the MFC 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">MFCServer.clw</TD>
		<TD ALIGN="LEFT" VALIGN="TOP">VC++ project file.</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP">MFCServer.cpp</TD>
		<TD ALIGN="LEFT" VALIGN="TOP">Main application source file and entry point for the DLL.</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP">MFCServer.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">MFCServer.dsp</TD>
		<TD ALIGN="LEFT" VALIGN="TOP">VC++ project file.</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP">MFCServer.dsw</TD>
		<TD ALIGN="LEFT" VALIGN="TOP">VC++ project file.</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP">MFCServer.h</TD>
		<TD ALIGN="LEFT" VALIGN="TOP">Main application header file.</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP">MFCServer.ncb</TD>
		<TD ALIGN="LEFT" VALIGN="TOP">VC++ project file.</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP">MFCServer.odl</TD>
		<TD ALIGN="LEFT" VALIGN="TOP">Standard Object Definition Language (ODL) file.</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP">MFCServer.rc</TD>
		<TD ALIGN="LEFT" VALIGN="TOP">Standard resource file.</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP">ReadMe.txt</TD>
		<TD ALIGN="LEFT" VALIGN="TOP">Text file that describes the project.</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP">Resource.h</TD>
		<TD ALIGN="LEFT" VALIGN="TOP">Resource header 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">StdAfx.h</TD>
		<TD ALIGN="LEFT" VALIGN="TOP">Standard precompiled header file. All the MFC-specific include files are added here.</TD>
	</TR>
	<TR ALIGN="LEFT" rowspan="1">
		<TD ALIGN="LEFT" VALIGN="TOP">Res\MFCServer.rc2</TD>
		<TD ALIGN="LEFT" VALIGN="TOP">Standard resource 2 file. This file contains all of the resource information that
			cannot be edited directly by VC++.</TD>
	</TR>

⌨️ 快捷键说明

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