Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Microsoft C# Professional Projects - Premier Press.pdf
Скачиваний:
177
Добавлен:
24.05.2014
Размер:
14.65 Mб
Скачать

796

Part IX

BEYOND THE LABS

 

 

 

In this chapter, you will learn about COM+, System.Messaging namespace, and asynchronous message queuing.

COM+

Before I introduce you to COM+, you need to understand the concepts of COM, MTS, and Windows DNA.

What Is COM?

As you might be aware, COM stands for component object model, and it is a model for construction of binary-compatible software components introduced by Microsoft. In simple terms, COM is a specification and implementation framework that allows you to create modular, object-oriented, customizable, and upgradeable distributed applications using a number of programming languages. COM enables you to develop components that can communicate with other components irrespective of the programming language or tool you choose to develop it.Therefore, COM allows you to concentrate on developing your application and not bother about the internals of the components.

COM allows clients to invoke services provided by COM-compliant components (COM objects). Services implemented by COM objects are exposed through a set of interfaces that represent the point of contact between clients and the object.

Why COM?

In order to understand the features provided by COM, you first need to understand the rationale for its existence. Binary code has been in use for a long time now. Libraries have been used in C and C++ from the very beginning. Windows programmers have been reusing DLLs (dynamically linked libraries). But there were other issues.

Libraries created using C/C++ compilers were often incompatible with executables created using a different C/C++ compiler. Secondly, DLLs were language

ADVANCED C# CONCEPTS

Chapter 34

797

 

 

 

 

dependent. Moreover, updating DLLs posed greater problems. Compatibility between versions was not achieved because of memory allocation reasons. Changed DLLs often broke executables designed for an earlier version.

Benefits of COM

COM was an alternative developed by Microsoft to overcome these problems. The major benefits of COM are:

COM components can provide functionality in a standard way.

COM provides for component interoperability.

COM provides a good versioning mechanism that allows one system component to be updated without requiring updates to other components in the system.

COM components can be implemented in a number of programming languages.

Before we proceed further, I will take you through some commonly used terms in COM.

Interfaces

An interface is a specification that defines the type of behavior a class must implement. An interface provides a group of related methods that are pure virtual functions. These methods are not implemented in the interface but are implemented in the class that implements the interface.The class that implements the interface methods is referred to as coclass. When the coclass is instantiated,the instance that is created is referred to as component object.

A class that inherits an interface must implement every member of the interface. An interface enables other programs to access the functionality provided by your component.

Consider the following example, where a COM object behaves like a clock. The clock object supports two interfaces, IClock and IAlarm. The IClock interface provides the methods to set and read current time. The IAlarm interface provides the alarm and methods.

798

Part IX

BEYOND THE LABS

 

 

 

GUID

COM objects and interfaces are specified using Microsoft IDL (interface defini - tion language). In order to avoid problems arising out of two components with same names, every COM component and interface has a GUID (globally unique identifier). A GUID is a 128-bit integer that is generated and assigned to every COM component and interface built. It uniquely identifies the component to the operating environment and other sof tware.

CLSID

Y

 

Class ID (CLSID) is a unique identifier that is associated with an OLE (Object

Linking and Embedding) object. If a classLis used to create more than one instance

 

A

of an object, the associated applicationFneeds to register its CLSID in the system

 

E

registry. This enables clients to locate and load the executable code associated with

the objects.

T

M

Marshaling

 

 

 

Marshaling is the process of packaging and sending interface method calls across thread or process boundaries.

Type Library

Type libraries are binary files (.tlb files) that include information about types and objects exposed by an ActiveX application. A type library can contain any of the following:

Information about data types

Description about one or more objects

Reference to type descriptions from other type libraries

Including the type library with the product ensures that information about objects in the library is made available to users of the application. Type libraries can be shipped as a standalone binary file or a resource in a DLL.

Team-Fly®

ADVANCED C# CONCEPTS

Chapter 34

799

 

 

 

 

Stub

Stub is an interface-specific object that unpackages the parameters for that interface after they are marshaled across the process boundary and makes the requested method call.The stub runs in the address space of the receiver and communicates with a corresponding proxy in the sender’s address space.

Proxy

A proxy is an interface-specific object that packages parameters for that interface in preparation for a remote method call. A proxy runs in the address space of the sender and communicates with a corresponding stub in the receiver’s address space.

Standard Interfaces

Every component must implement at least two interfaces, the IUnknown and the IDispatch interfaces. The IUnknown interface has three methods, as follows:

AddRef

Release

QueryInterface

COM objects keep track of the number of interface pointers to the object that are in use. When the reference count reaches zero, it can be freed.However, the object is not explicitly freed;instead, all the objects interface pointers and the object frees itself after an appropriate time.

AddRef increments the reference count and Release documents it. The QueryInterface is the most important method. Because all interfaces inherit from IUnknown, all interfaces must implement QueryInterface. The QueryInterface method provides client access to other interfaces on an object.

IDispatch interface exposes objects, methods, and properties to other programs that support automation. COM components implement the IDispatch interface to enable access by automation clients.

800

Part IX

BEYOND THE LABS

 

 

 

Every COM object runs inside of a server. A single server can support multiple COM objects. A client can access COM objects provided by a server in one the following three ways:

In-process server. Clients can link directly to a library containing the server. The client and ser ver execute in the same process. Communication is accomplished through function calls.

RPC (remote procedure call ). The client can access a server running in a different process but on the same machine through an interprocess communication mechanism.

Remote Object Proxy. The client can access a remote server running on another machine. The network communication between client and server is accomplished through RPC. The mechanism supporting access to remote servers is called DCOM (Distributed COM).

Functioning of COM Objects

If the client and the server are in the same process, the sharing of data between the two is simple. However, when the server process is separate from the client process, as in a local server or remote server, COM must format and bundle the data in order to share it. This process of preparing the data is called marshaling. Marshaling is accomplished through a proxy object and a stub object that handle the cross-process communication for any particular interface. COM creates the stub in the object’s server process and has the stub manage the real interface pointer. COM then creates the proxy in the client’s process and connects it to the stub. The proxy then supplies the interface pointer to the client.

The client calls the interfaces of the server through the proxy, which marshals the parameters and passes them to the server stub. The stub unmarshals the parameters and makes the actual call inside the server object. When the call completes, the stub marshals return values and passes them to the proxy, which in turn returns them to the client. The same proxy/stub mechanism is used when the client and server are on different machines.

The internal implementation of marshaling and unmarshaling differs depending on whether the client and server operate on the same machine (COM) or on dif-

ADVANCED C# CONCEPTS

Chapter 34

801

 

 

 

 

ferent machines (DCOM). When a client wishes to create and use a COM object, the client performs the following steps:

1.It invokes the COM API to instantiate a new COM object.

2.COM locates the object implementation and initiates a server process for the object.

3.The server process creates the object and returns an interface pointer at the object.

4.The client can then interact with the newly instantiated COM object through the interface pointer.

COM Threading Model

Programming multithreading applications is quite a tedious task irrespective of the tool or the language being used. In an application that is single threaded, everything runs synchronously, that is, one after the other, whereas in case of multithreaded applications, the execution sequence is not so straightforward. All requests for a component method are queued up in the message pump, causing all requests to be executed simultaneously. Applications in which multiple threads are active at the same time can be asynchronous.

It is possible that methods in a component execute simultaneously. Data in a component can be changed or accessed by more than one thread, thus losing its co n- currency. Therefore, it becomes the responsibility of the component or the application to prevent such simultaneous access by multiple threads or to make the code thread savvy.

Because this is a likely scenario for COM components, the COM architecture provides various threading models, which allows you to create components that are inherently thread savvy but less flexible, or components that can handle threading issues themselves.

You can build COM components that have any of the following threading models:

Single threaded

Apartment threaded

Both single and apartment threaded

Free threaded