📄 ch02.htm
字号:
<P>ATL by itself can create small, lightweight servers that are easy to use and modify.
ATL is limited to the creation of COM-based objects only and is lacking in generic
or utility class support. Combined with MFC and the Standard Template Library (STL),
ATL is a very powerful framework for ActiveX development. For those developers who
are willing to take the time to learn it, ATL is by far the best route.</P>
<P>The BaseCtl also allows you to create small, lightweight servers. Unfortunately,
the BaseCtl lacks support from Microsoft as a product which impacts its capability
to be a practical long-term development tool. It has, however, a very straightforward
and easy-to-understand architecture that is helpful to those developers who are learning
OLE and ActiveX from the ground up and who are not afraid of all the gory details.
<B><BR>
<BR>
Types of Automation Servers</B><SPACER TYPE="HORIZONTAL" SIZE="10"><B> </B>When creating
an Automation Server, you must decide how to implement the server relative to how
it is going to be used. You can create two basic types of servers: DLL-based and
EXE-based. <B><I><BR>
<BR>
DLL-based Servers</I></B><SPACER TYPE="HORIZONTAL" SIZE="10">If the server does not
have to run as a stand-alone application and performance is a critical issue, you
should implement your server as a DLL. A DLL-based server is typically referred to
as an in-process server because of how it normally executes relative to its controller.</P>
<P><B><I>EXE-based Servers </I></B><SPACER TYPE="HORIZONTAL" SIZE="10">If the server
application does have a requirement to run as a stand-alone application, you must
implement it as an EXE. An EXE-based Automation Server is typically referred to as
a local Server or out-of-process Server, and it will execute in its own process space.
<B><BR>
<BR>
Types of Server Execution </B><SPACER TYPE="HORIZONTAL" SIZE="10">Automation Servers
also have an execution model that is independent of how the server is written. Automation
Servers can execute either in-process, locally or remotely, relative to the application
that has invoked or is using it. <B><I><BR>
<BR>
In-Process Execution </I></B><SPACER TYPE="HORIZONTAL" SIZE="10">An in-process Server
is called <I>in-process</I> because it executes within the same process space as
that of the application that created it. Only DLL-based automation Servers can execute
as in-process, but that is not a guarantee. This is very important to note when using
nested objects or shared objects. If an object is created in a process space, say
Process A, and it is passed to another application in another process space, Process
B, the Server in Process A will execute as a local Server relative to the application
in Process B regardless of the fact that the Server in Process A is a DLL-based Server.
This issue is very critical since more times than not in-process Servers are used
to improve performance of the application using them.</P>
<P><B><I>Local Execution </I></B><SPACER TYPE="HORIZONTAL" SIZE="10">Local execution
is when an Automation Server is executing in a process space other than the process
space of the controller application. As was stated earlier, a DLL-based Server may
execute locally to its controller, depending on how it was created versus which application
is using it. The main issue with local Servers is performance since all of the method
calls have to cross process boundaries. This condition requires additional code overhead
to move data back and forth between the Server and its Controller.</P>
<P><B><I>Remote Execution </I></B><SPACER TYPE="HORIZONTAL" SIZE="10">Remote Execution
is when the Server is executing on a machine other than the application that is controlling
it. As with local Servers, performance is an issue with this type of execution.
<H3><A NAME="Heading14"></A>ActiveX Controls</H3>
<P>An ActiveX Control, for all intents and purposes, is still the same OCX or OLE
Control that you have come to know and love over the past several years. In fact,
the only change with the coming of ActiveX is a decrease in the requirements to qualify
as a control.</P>
<P>To qualify as an ActiveX Control, a component must be a COM Object, implement
an <TT>IUnknown</TT> interface, and support registration and unregistration through
the exported functions, DLLRegisterServer and DLLUnregisterServer. That's it! Pretty
easy, right? Well, not really.</P>
<P>Even though your component qualifies as an ActiveX control, if all it supports
is the preceding features, it will not do much more than take up space on your hard
disk. If it needs UI, persistence, events, or any other feature common to controls,
the control must implement other categories of interfaces. The exact requirements
are in the <I>OLE Control and Control Container Guidelines, Version 1.1</I> published
by Microsoft. All of the guidelines for ActiveX development are available on the
Internet at the Microsoft Web site or on the ActiveX SDK CD.</P>
<P>You have three tools at your disposal for creating ActiveX controls: MFC, ATL,
and BaseCtl. As we pointed out earlier in the section "ActiveX Automation Servers,"
each tool has its strengths and weaknesses. With ActiveX Controls you only have one
option when creating and executing the control: as a DLL and in-process to the Control's
container application. Even though the extension of the Control is .ocx, it is still
in fact just a .dll.
<H2><A NAME="Heading15"></A>Support Tools Needed for Building ActiveX Components</H2>
<P>When creating your ActiveX Components, a few tools are essential to successful
ActiveX development. Most of these tools are automatically installed as part of the
Visual C++ development environment. As ActiveX development matures, more and more
tools will become available. Using as many tools as you can will greatly improve
your understanding of how your components work, as well as improve their overall
implementation.
<H3><A NAME="Heading16"></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 (IDL) files
into C code, which is then compiled into the project by the Visual C++ compiler.</P>
<P>The MIDL compiler also provides support for marshaling interfaces across process
boundaries. Starting with Visual C++ 4.0, the MIDL compiler started shipping as a
standard component of Visual C++. The MIDL compiler is also available via the Win32
SDK from Microsoft.
<H3><A NAME="Heading17"></A>Mktyplib</H3>
<P>Mktyplib is a type library compiler for compiling ODL files. Mktyplib is the predecessor
to MIDL and produces the same type of output as MIDL. The use of Mktyplib will decrease
as Microsoft moves from ODL to IDL files.
<H3><A NAME="Heading18"></A>GUIDGEN</H3>
<P>GUIDGEN is a tool that is used to generate Global Unique Identifiers (GUID), which
can be used for Interface IDs, Class IDs or any other 128-bit UUID, such as an RPC
interface. GUIDGEN is installed only when the OLE development option is selected
during the Visual C++ installation. When GUIDGEN is run, a GUID is created and placed
in the Windows clipboard. After running the GUIDGEN application, the resultant GUID
is pasted from the clipboard into the code that needs a GUID.
<H3><A NAME="Heading19"></A>RegEdit</H3>
<P>RegEdit, 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>
RegEdit</B> is a very 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="Heading20"></A>Registration Server</H3>
<P>The Registration Server 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 or if the ActiveX
SDK is installed.
<H3><A NAME="Heading21"></A>Ole2View</H3>
<P>Ole2View is a program that lists ActiveX components relative to their function
and category. Ole2View is extremely helpful in the test and use of your components.
Ole2View can be used to determine whether your component is registered properly and
the type and number of interfaces your component defines. It even allows you to instantiate
and access various components and interfaces within your application, and now it
has expanded support for configuring components to use DCOM. When attempting to determine
what is the cause of problems with a troublesome component, Ole2View is invaluable.
<H3><A NAME="Heading22"></A>Adding the Tools to the Visual C++ Development Environment</H3>
<P>In order to maximize development productivity, the tools needed for COM programming
should be integrated into the Visual C++ environment. Each of the tools needed can
be added to the IDE's (Integrated Development Environment) <U>T</U>ools menu. The
following section illustrates how to incorporate the tools into the IDE.</P>
<P>Adding GUIDGEN to the Visual C++ environment enables the generation of a UUID
from a single menu command. The generated UUID is placed in the Windows clipboard
and must be pasted into the project code.</P>
<P>To add GUIDGEN to the Visual C++ environment:
<OL>
<LI>Select the <U>C</U>ustomize command from the <U>T</U>ools menu. Select the Tools
tab from the Customize dialog.
<P>
<LI>In the <U>M</U>enu Contents list box, scroll to the bottom of the list, and select
a blank line, and type <B>&Generate New UUID</B>.
<P>
<LI>In the Command edit box, type <B>GUIDGEN.EXE</B>.
<P>
<LI>Clear all text from the <U>A</U>rguments edit box.
<P>
<LI>Clear all text from the <U>I</U>nitial Directory edit box.
<P>
<LI>Click the Close button to add the entry to the Tools menu.
</OL>
<P>In addition to the GUIDGEN program, you may want to consider adding the registry
editor, the ability to unregister a component, and any other tool that you find useful
for your development.
<H2><A NAME="Heading23"></A>From Here...</H2>
<P>All of the frameworks described in this chapter have their strengths and weaknesses.
MFC allows for rapid component creation and implementation and the level of support
built into the VC++ IDE for MFC is beyond comparison with ATL or BaseCtl. MFC offers
a very large and robust class library for solving most, if not all, of your development
problems. MFC does, however, suffer from the problem that it is everything to everyone,
which results in a slower application or one that cannot deviate from the "norm"
fairly easily.</P>
<P>ATL provides a small and deliberate framework for creating ActiveX components.
ATL, however, falls very short in the area of common class and utility support, which
is the thing that is MFC's strength. In addition, ATL's integration with the VC++
IDE also leaves room for improvement.</P>
<P>BaseCtl is similar to ATL in that it is focused specifically on small, fast component
development. Like ATL, it lacks the same common class and utility support that makes
MFC so attractive. BaseCtl has an added negative of being considered only as a sample
and not as a supported product by Microsoft.</P>
<P>The level of experience of the development team and the intended life cycle of
the code and applications will also affect the decision of which tool to choose to
create ActiveX components. Take the time to investigate all of the options available
to you before deciding on a platform and a direction.</P>
<P>The following chapters examine in detail how to implement some of the components
described so far using MFC, ATL, and BaseCtl. Even with all of the information presented
so far, the only true way to know that you have made the right choice is to do it
yourself.
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -