Module 8: Solution Design and the Component Object Model 249
COM Standard
"
Binary standard for object interaction
"
Part specification
"
Part implementation
COM, as a binary standard for object interaction and addresses many
component challenges. It is a binary standard because it is in part specification-
oriented and in part implementation-oriented.
"
COM is specification-based in that it defines objects by using a language in
a location-independent manner. In addition, it defines how to locate and
identify components, as well as instantiated objects.
"
COM is also implementation-based in that it provides system services that
know how to locate components, load them into memory, execute their
methods, and access their properties. Additionally, COM can perform
interprocess and remote communications.
"
COM defines the exact binary representation of an interface. As a result,
any programming language or tool that supports COM must create object
interfaces that strictly correspond to this standard binary representation.
Slide Objective
To define COM and
introduce the COM
standard.
250 Module 8: Solution Design and the Component Object Model
COM Components
"
Three basic packaging methods:
$
Windows NT services
$
Executable
$
Dynamic-link library
"
Multiple physical locations:
$
In-process
$
Local components
$
Remote components
"
Binary files used to instantiate COM objects
There are three packaging methods for COM components: a Microsoft
Windows
®
service, an executable file, or a dynamic-linked library (DLL).
Windows services are simply applications that are required to always be
running. Windows executable files are often used if an application must provide
a user interface in addition to furnishing COM objects. In most development
scenarios, components are packaged as DLLs.
Additionally, components are described by their location relative to their calling
application, which is referred to as a client or consumer. In-process components
are implemented as DLLs and execute within the same process as the client.
Local components are often executable files or a Windows service. They are
executed on the same computer as the client, but in a separate process. Remote
components can be packaged as required, as well as located and executed on a
separate computer from the calling client.
A COM component is a binary unit of compiled application code that can be
used to instantiate COM objects into the memory of a computer. For a given
COM class identifier — also called a unique class identifier (CLSID) — a
component will include the COM class, the code to instantiate and execute a
class object, and usually the code required to create installation entries into a
system’s registry.
Slide Objective
To introduce COM
component information.
Delivery Tip
Although components are
sometimes called servers,
we avoid confusion with
server computers by
maintaining the original
term, “component,”
throughout this module.
Delivery Tip
For the remainder of this
section relating to COM, we
will focus on components
implemented as DLLs
because these components
are most prevalent in n-tier
applications.
Module 8: Solution Design and the Component Object Model 251
COM Interface Basics
"
Interface contracts
"
Interface Description Language
"
Available COM interfaces
$
IUnknown
$
IClassFactory
$
IDispatch
A COM interface is a contract between a provider of services, the COM class,
and the consumer of those services (the client). The interface is the only means
of communication between the provider and consumer. It defines how messages
and information will be accepted by the component, as well as how information
will be returned to the client.
Interface Description Language (IDL) is a language similar in structure to C++,
which describes the exact syntax of an interface. Interface definitions begin
with an interface keyword; interface attributes are contained in square brackets
preceding the interface keyword.
To be defined as a COM interface, an interface must satisfy the following
requirements:
"
A unique identifier must identify the interface.
"
The interface must ultimately derive from the special interface IUnknown.
"
Once published, the interface must be immutable. In other words, the
interface cannot be changed.
Instead of using string identifiers alone to identify COM interfaces, COM
implements globally unique identifiers (GUIDs). GUIDs are 128-bit, system-
generated integers that uniquely identify components. The algorithm used to
generate GUIDs is statistically guaranteed to generate unique identification
numbers.
The IUnknown interface defines the fundamental behavior for COM interfaces.
Clients can rely on this behavior because all COM interfaces derive from
IUnknown. The IUnknown interface helps resolve the technical challenge of
providing a standard means to interact with objects. It also provides three
important features of interaction: interface navigation, interface versioning, and
object lifetime management.
Slide Objective
To describe a COM
interface.
Lead-in
COM interfaces provide the
means for accessing COM
components.
Delivery Tip
According to the COM
specification, GUIDs can be
generated at the rate of
10,000,000 per second per
computer for the next 3,240
years without risk of
duplication.
252 Module 8: Solution Design and the Component Object Model
IClassFactory is a standard interface for communicating with class objects. As
a result, clients need only one mechanism to create any type of COM object.
The most important method of IClassFactory is CreateInstance, which creates
an object and returns a specified interface pointer to the object.
IDispatch is the standard COM interface that provides automation and
programmatic access to an object. By implementing IDispatch, components
can expose any number of functions to clients. Clients access all functionality
through a single well-known function.
Developers using certain development tools, such as Microsoft Visual Basic
®
,
are shielded from the complexity of defining interfaces using IDL. Their
compilers manage the creation, assignment, and registration of the interfaces.
Module 8: Solution Design and the Component Object Model 253
COM Classes
"
Name and define a public interface
"
Provide
$
A means to instantiate objects
$
Separation of the interface from the implementation
A COM class defines the public interface that may be accessed by a client and
defines how to access the interface from a client. A COM class is also given a
CLSID and a string identifier called a programmatic id (ProgID). Every COM
class has an associated class object (also known as a class factory) that provides
the ability to create instances of a specific COM class. These instances of a
COM class differ from most language-based classes in that after an instance has
been created, its specific class is no longer relevant to the client. All interaction
with this instantiated object occurs through interface pointers. These pointers do
not allow access to the implementation classes used to instantiate the object or
to the internal code that allows the object to perform work.
The rigorous separation of interface and implementation is a key feature of
COM. This “black box” concept greatly simplifies the client coding effort
because the client does not need to know how the components work, only that
they do work.
Slide Objective
To introduce COM classes.
Lead-in
A COM class is simply an
instance of a named COM
interface.
254 Module 8: Solution Design and the Component Object Model
COM Objects
"
Are run-time instances of a COM class
"
Simplify access to application logic and information
"
Are accessible by multiple clients
"
Provide a means for life-cycle management
"
Reduce client application code
As with most object-oriented models, COM objects are run-time instances of a
particular COM class. These objects live in memory and are executed by a
computer microprocessor.
When COM objects are instantiated, their underlying code and actual class
structure are hidden from the client. Because the application logic and
information structure are hidden, the client is provided with a significantly
simplified interface and the ability to perform complex application logic, as
well as given access to complex information structures. This simplification also
reduces the amount of code required by the client application.
Many clients can use an instantiated object simultaneously, and a single client
can use the same instantiated object multiple times. Only a pointer is given to
the client or clients to provide access to the instantiated object.
As mentioned, a single client may need to use multiple occurrences of the same
object at the same time. For efficiency, a duplicate copy of the object’s
executable code is typically not created in memory. As pointers are given to the
client, the object increments and decrements a reference counter through the
IUnknown interface each time a new instance of the same object is required.
COM’s life-cycle management of the object allows for more efficient use of
computer resources and provides COM with a mechanism for knowing when an
object is no longer being used (that is, when its reference count is zero).
Slide Objective
To describe a COM object.
Lead-in
A COM object is a run-time
instance of a COM class.
Delivery Tip
Technically, the client
receives a pointer to a
pointer to a v-table of
pointers when using
IClassFactory. When using
IDispatch, the client must
query the interface each
time to get the information.
Module 8: Solution Design and the Component Object Model 255
Activity 8.1: Simulating Component Communication
This activity will demonstrate the specific way in which COM components
communicate with one another.
In this activity, you will simulate a COM component communicating with other
COM components.
After completing this lab, you will be able to:
"
Demonstrate how COM technologies enable component-based solutions to
function.
Slide Objective
To introduce the activity.
256 Module 8: Solution Design and the Component Object Model
!
!!
!
Application Development and COM
In this section
In this section
"
COM and Windows DNA
"
Calling COM Components
"
COM Threading Model
"
COM and Marshalling
In this section, you will learn about the application development issues
surrounding COM specification. You will learn how to use COM components
and how a COM component’s internal code can function.
Slide Objective
To explain the purpose of
the section and what
students will learn in the
section.
Module 8: Solution Design and the Component Object Model 257
COM and Windows DNA
"
Foundation for layer independence
"
Public interface with standard behaviors
"
Client language independence
"
Creation language independence
By definition, Windows Distributed interNet Applications (Windows DNA)
uses COM as the foundation for interapplication and intraapplication
communication. Because Windows DNA architecture recommends a
cooperating, multiple-layer architecture, COM provides the communication
mechanism between the presentation, business, and data layers.
COM components publish a public interface that may be accessed by any
service layer. COM also provides the standard communication mechanism for
applications requiring access to additional system services, such as Microsoft
Transaction Services, within the Windows DNA business service layer.
The nature of COM’s public interface and the fact that COM provides a “black-
box” execution standard (meaning that how the component executes is not
revealed or accessible from the client) allow the use of any development tool or
language to execute a COM component. Consequently, the appropriate
development tool can be chosen for each service layer.
Finally, COM provides a standard way for the operating system to publish a
component’s public interface and process components. As a result, as long as a
development tool supports the creation of COM components, any tools can be
used to code COM components. This choice of independent component coding
also allows the development team to use different tools to create components
for each service layer.
Slide Objective
To explain how COM relates
to Windows DNA.
258 Module 8: Solution Design and the Component Object Model
Calling COM Components
"
Reference the component
"
Instantiate an object
"
Call the object’s interfaces
"
Destroy the object
To use a COM component, the component must first be referenced. How
reference is accomplished depends on the development environment, but it is
generally as simple as a menu choice or a declaration of a variable. Typically,
the ProgID is used to assign the component a variable name within the
application.
After the component is referenced, an object may be instantiated, incrementing
its reference count. This instantiation provides the client with access to the
object’s public interfaces. The client knows these interfaces through IDispatch
or IClassFactory.
When the object is instantiated, its properties can be set, its methods can be
activated, or events can be raised directly from the client.
An object should be released after it has served its purpose. The release process
decrements the object’s reference count and recycles the object pointers,
thereby freeing the memory that the client required. When the released object’s
reference count indicates that no additional objects are being accessed, the
memory associated with the object is returned by COM to the operating system.
Not releasing objects properly is a common problem and often noted as
“memory leaks.”
Slide Objective
To explain the steps
required to call a COM
component.
Không có nhận xét nào:
Đăng nhận xét