?? ch12.htm
字號:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<!--last modified on Tue, Apr 15, 1997 11:44 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 12</TITLE>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF">
<H1>Chapter 12<BR>
Creating ActiveX COM Objects and Custom Interfaces Using MFC</H1>
<UL>
<LI><A HREF="#Heading1">Creating ActiveX COM Objects and Custom Interfaces Using
MFC</A>
<UL>
<LI><A HREF="#Heading2">Anatomy of a COM Object</A>
<LI><A HREF="#Heading3">Tools Needed for Building COM Objects</A>
<UL>
<LI><A HREF="#Heading4">MIDL Compiler</A>
<LI><A HREF="#Heading5">GUIDGEN</A>
<LI><A HREF="#Heading6">RegEdit</A>
<LI><A HREF="#Heading7">Registration Server</A>
<LI><A HREF="#Heading8">Adding the Tools to the Visual C++ Development Environment</A>
</UL>
<LI><A HREF="#Heading9">Defining COM Interfaces Using IDL</A>
<UL>
<LI><A HREF="#Heading10">Creating the IFISH Project</A>
<LI><A HREF="#Heading11">Creating the Interface Definition</A>
<LI><A HREF="#Heading12">Listing 12.1 IFISH.IDL--Interface Definition for IFish</A>
<LI><A HREF="#Heading13">Listing 12.2 IBASS.IDL--Interface Definition for IBass</A>
<LI><A HREF="#Heading14">Compiling the Interface Definition Files</A>
<LI><A HREF="#Heading15">Creating a Definition File</A>
<LI><A HREF="#Heading16">Listing 12.3 DLL LIBRARY--Definition File for IFISH.DLL</A>
<LI><A HREF="#Heading17">Adding the RPC Libraries to the Interface Project</A>
<LI><A HREF="#Heading18">Listing 12.4 RPCHELP.C--Compiler Pragmas Used for Referencing
RPC Libraries</A>
<LI><A HREF="#Heading19">Registering the Interfaces</A>
<LI><A HREF="#Heading20">Listing 12.5 IFISH.REG--Contexts of IFISH.REG File Used
to Register the Interfaces Supported by the IFISH.DLL</A>
</UL>
<LI><A HREF="#Heading21">Implementing the Interface</A>
<UL>
<LI><A HREF="#Heading22">Using the Visual C++ AppWizard to Create the COM Object</A>
<LI><A HREF="#Heading23">Accessing In-Process COM Objects</A>
<LI><A HREF="#Heading24">Listing 12.6 BASS.DEF--BASS Definition File with the COM
Support Functions Explicitly Exported</A>
<LI><A HREF="#Heading25">Listing 12.7 BASS.CPP--DLlGetClassObject Implementation
Code Inserted by the MFC AppWizard</A>
<LI><A HREF="#Heading26">Listing 12.8 DLLCanUnloadNow--Implementation Code Inserted
by the MFC AppWizard</A>
<LI><A HREF="#Heading27">Listing 12.9 DllRegisterServer--Implementation Code Inserted
by the MFC AppWizard</A>
<LI><A HREF="#Heading28">Creating a Class that Implements COM Interfaces</A>
<LI><A HREF="#Heading29">Listing 12.10 COMMACROS.H--COM Macros Used for Accessing
the IUnknown Implementation of CCmdTarget</A>
<LI><A HREF="#Heading30">Listing 12.11 BASSID.H--Header File bassid.h that Contains
the Implementation of CLSID for the CBass Class</A>
<LI><A HREF="#Heading31">Listing 12.12 BASS.H--Header File for the CBass Class (bass.h)</A>
<LI><A HREF="#Heading32">Listing 12.13 BASS.CPP--Complete Implementation File for
CBass object (Bass.cpp)</A>
</UL>
<LI><A HREF="#Heading33">Using the Interface</A>
<UL>
<LI><A HREF="#Heading34">OLE Initialization and Shutdown Functions</A>
<LI><A HREF="#Heading35">Listing 12.14 COMTEST.CPP--Initialization and Removal of
OLE Libraries within an MFC Application</A>
<LI><A HREF="#Heading36">COM Object Access Functions</A>
<LI><A HREF="#Heading37">Listing 12.15 Comtestview.cpp--Test Function Used for Accessing
the IFish and IBass Interfaces</A>
</UL>
<LI><A HREF="#Heading38">From Here...</A>
</UL>
</UL>
<P>
<HR SIZE="4">
<H1><A NAME="Heading1"></A>Creating ActiveX COM Objects and Custom Interfaces Using
MFC</H1>
<UL>
<LI><B>Supporting COM with MFC</B>
<SPACER TYPE="VERTICAL" SIZE="2">
While MFC does not utilize COM directly, the MFC architects did provide support
mechanisms that make adding COM functionality an easy task.
<P>
<LI><B>Adding COM tools to the Visual C++ development environment</B>
<SPACER TYPE="VERTICAL" SIZE="2">
The implementation of COM Objects requires new tools, such as the MIDL compiler,
that are outside the scope of traditional application development.
<P>
<LI><B>Creating a basic Component Object using MFC</B>
<SPACER TYPE="VERTICAL" SIZE="2">
Examine the implementation and details of the two types of component objects, in-process
(DLL) and out-of-process (EXE).
<P>
<LI><B>Defining a COM interface using the Interface Definition Language (IDL)</B>
<SPACER TYPE="VERTICAL" SIZE="2">
The Interface Definition Language is used for defining your COM Object interface.
<P>
<LI><B>Setting up and installing COM Objects</B>
<SPACER TYPE="VERTICAL" SIZE="2">
Installing and using a COM Object is a straightforward process, but you need to
be aware of a few trouble spots.
</UL>
<P>ActiveX is a strategic technology base for Internet programming and distributed
computing. While ActiveX is the successor for OLE (Object Linking and Embedding),
OLE still forms the foundation of ActiveX programming. The basis for ActiveX is to
provide an object-oriented solution for solving problems encountered in developing
operating systems and applications. ActiveX provides the specifications necessary
to create component software that ultimately benefits the computing industry.</P>
<P>At the core of ActiveX is an extremely powerful and extensible architecture called
the <I>Component Object Model (COM)</I>. COM provides a simple yet elegant solution
for solving complex software problems such as accessing objects and services outside
of application boundaries and version control. COM solves these problems through
the use of binary components that are running in the system rather than by developing
source code components within an application.</P>
<P>If you are using the Visual C++ compiler from Microsoft, chances are very high
that you are also using the Microsoft Foundation Classes (MFC) as the building blocks
for your applications and components. MFC is a powerful set of base classes that
provide a framework of abstractions into the Windows SDK for developing Win32 applications
and components.</P>
<P>Classes within the MFC framework are not directly derived from COM interfaces.
However, the architects of MFC have provided direct support for adding COM to any
MFC-based component or application. The roots for supporting COM within MFC lie in
wrappers called <I>Interface maps. </I>Interface maps are similar to message maps
(which are used for distributing Windows messages to MFC classes) in both concept
and execution.
<H2><A NAME="Heading2"></A>Anatomy of a COM Object</H2>
<P>COM Objects give software developers the ability to leverage object-oriented software
techniques for solving application and operating system development issues. The COM
specification is not geared toward a specific language, although C++ is a natural
choice when developing COM Objects. Four basic components compose a COM Object:
<UL>
<LI>Classes--A <I>class</I> is a data structure with a set of interfaces used for
accessing and manipulating the data structure. This definition is analogous to C++
class. The difference is that COM allows a class created in any language to be registered
with the operating system and to be used in a language-independent manner.
<P>
<LI>Objects--An <I>object</I> is an instance of a class created during program execution.
In C++, an object is typically created via the <TT>new</TT> operator. When using
COM, COM Objects are created through the function <TT>CoCreateInstance</TT>. Many
instances of an object can be created.
<P>
<LI>Interfaces--An <I>interface </I>is a group of functions (often called methods)
that are part of a class. The interface functions are used to directly manipulate
the data in a class. ActiveX is based on a set of COM interfaces. Of the set of ActiveX
interfaces, the two that must be supported by COM Objects are <TT>IUnknown</TT> and
<TT>IClassFactory</TT>.
<P>
<LI>GUIDs--A GUID (Global Unique Identifier) is an 8-byte number that provides a
unique identifier for each COM Object. <TT>GUID</TT>s are generated by a tool called
<TT>GUIDGEN</TT>. Each COM class <I>must</I> have two <TT>GUID</TT>s, one for the
Class ID and one for the Interface ID.
</UL>
<P>The <I>class ID </I>(<TT>CLSID</TT>) is an identifier for the COM class. This
key is registered in the Windows Registry and contains a pointer (path) to where
the DLL (Dynamic Link Library) or EXE containing the class can be located. The <TT>CLSID</TT>
can be found in the Windows Registry under the path HKEY_CLASSES_ROOT\CLSID.</P>
<P>The <I>Interface ID </I>(<TT>IID</TT>) is an identifier for the interface to the
class. The <TT>IID</TT> is used by applications to query and invoke the methods into
the class. The <TT>IID</TT> is also contained in the Windows Registry and can be
found in the path HKEY_CLASSES_ROOT\Interface. Figure 12.1 illustrates the relationship
among class, interfaces, and <TT>IID</TT>. <BR>
<BR>
<A HREF="Art/12/ifig01.jpg"><B>FIG. 12.1</B></A> <BR>
<I>Relationship of COM Classes and Interfaces.</I></P>
<P>What COM provides to software developers is an object-oriented solution for building
and maintaining software solutions. Programmers using non-object-oriented languages
such as Visual Basic can develop and use COM components to build software solutions.</P>
<P>COM also provides a unique solution to the version control problems present in
many of today's software solutions. Since COM Objects are binary components, developers
do not have to worry about new or updated versions of a component being placed on
a computer where their application is running. The reason for this is that COM deals
with interfaces. If an interface is enhanced, new methods can be added to the interface,
or additional interfaces can be obtained without breaking an existing application.
COM's solution to version control provides a great method for upgrading applications
while preserving legacy systems.
<H2><A NAME="Heading3"></A>Tools Needed for Building COM Objects</H2>
<P>When creating your COM Objects, a few tools must be installed on your computer.
Most of these tools are automatically installed as part of the Visual C++ development
environment.
<H3><A NAME="Heading4"></A>MIDL Compiler</H3>
<P>The Microsoft MIDL compiler is now a standard component of the Microsoft Visual
C++ environment. The MIDL compiler compiles COM interface definitions into C code,
which is then compiled into the project by the Visual C++ compiler. Figure 12.2 illustrates
the purpose of the MIDL compiler. <B><BR>
<BR>
</B><A HREF="Art/12/ifig02.jpg"><B>FIG. 12.2</B></A> <I><BR>
Inputs and outputs of the MIDL compiler.</I></P>
<P>The MIDL compiler also provides support for marshaling interfaces across process
boundaries. Starting with Visual C++ 4.0, the MIDL compiler was shipped as a standard
component of Visual C++. The MIDL compiler is also available with the Win32 SDK from
Microsoft.
<H3><A NAME="Heading5"></A>GUIDGEN</H3>
<P><TT>GUIDGEN</TT> is a tool used to generate Global Unique Identifiers (<TT>GUID</TT>),
which can be used for Interface IDs, Class IDs, or any other 128-bit <TT>UUID</TT>,
such as an <TT>RPC</TT> interface. <TT>GUIDGEN</TT> is installed when the OLE development
option is selected during the Visual C++ installation. When <TT>GUIDGEN</TT> is run,
you must select the proper format for the <TT>UUID</TT> and then press the New <TT>GUID</TT>
button to copy the <TT>UUID</TT> to the Windows Clipboard. After running the <TT>GUIDGEN</TT>
application, the resulting <TT>GUID</TT> is pasted from the Clipboard into the code
that needs a <TT>GUID</TT>. <BR>
<BR>
<IMG SRC="bar.gif" WIDTH="480" HEIGHT="6" ALIGN="BOTTOM" BORDER="0"></P>
<BLOCKQUOTE>
<P><B>NOTE:</B> The tool <TT>GUIDGEN</TT> is also installed by default if the option
Typical is selected during the Visual C++ 5.0 installation.
</BLOCKQUOTE>
<P><IMG SRC="bar.gif" WIDTH="480" HEIGHT="6" ALIGN="BOTTOM" BORDER="0">
<H3><A NAME="Heading6"></A>RegEdit</H3>
<P><TT>RegEdit</TT> or the registration editor is a standard component of both the
Windows 95 and Windows NT operating systems. The registration editor is used for
browsing and altering operating system and application settings. The registration
editor can also be used for installing and registering your COM Objects. <BR>
<BR>
<IMG SRC="bar.gif" WIDTH="480" HEIGHT="6" ALIGN="BOTTOM" BORDER="0"></P>
<BLOCKQUOTE>
<P><B>CAUTION:<BR>
</B><TT>RegEdit</TT> is a powerful tool and must be used with extreme caution by
experienced users. If used improperly, systems can be damaged, resulting in a loss
of data or a malfunctioning computer.
</BLOCKQUOTE>
<P><IMG SRC="bar.gif" WIDTH="480" HEIGHT="6" ALIGN="BOTTOM" BORDER="0"><BR>
<BR>
In Windows 95, this program is called regedit.exe. In Windows NT, this program is
called regedt32.exe.
<H3><A NAME="Heading7"></A>Registration Server</H3>
<P>The <I>registration server </I>is an application that can be used to register
the settings of a COM Object in the Windows registry without the need to create a
separate registration file. The application is called regsvr32.exe and is automatically
installed if the OLE development option is selected during Visual C++ installation
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -