15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

.neT <strong>and</strong> CoM ❘ 701<br />

events<br />

.<strong>NET</strong> offers a callback mechanism with the <strong>C#</strong> keywords event <strong>and</strong> delegate (see Chapter 8, “Delegates,<br />

Lambdas, <strong>and</strong> Events”).<br />

Figure 26-6 shows the COM event-h<strong>and</strong>ling architecture. With COM events, the component has to<br />

implement the interface IConnectionPointContainer <strong>and</strong> one or more connection point objects (CPOs)<br />

that implement the interface IConnectionPoint. The component also defines an outgoing interface —<br />

ICompletedEvents in Figure 26-6 — that is invoked by the CPO. The client must implement this outgoing<br />

interface in the sink object, which itself is a COM object. During runtime, the client queries the server<br />

for the interface IConnectionPointContainer. With the help of this interface, the client asks for a CPO<br />

with the method FindConnectionPoint() to get a pointer to IConnectionPoint returned. This interface<br />

pointer is used by the client to call the Advise() method, where a pointer to the sink object is passed to the<br />

server. In turn, the component can invoke methods inside the sink object of the client.<br />

lConnectionPoint<br />

Client<br />

Server<br />

lConnectionPointContainer<br />

lConnectionPoint<br />

Sink<br />

lCompletedEvents<br />

CPO<br />

figure 26-6<br />

Later in this chapter, you learn how the .<strong>NET</strong> events <strong>and</strong> the COM events can be mapped so that COM<br />

events can be h<strong>and</strong>led by a .<strong>NET</strong> client <strong>and</strong> vice versa.<br />

marshaling<br />

Data passed from .<strong>NET</strong> to the COM component <strong>and</strong> the other way around must be converted to the<br />

corresponding representation. This mechanism is also known as marshaling. What happens here depends on<br />

the data type of the data that is passed: You have to differentiate between blittable <strong>and</strong> nonblittable data types.<br />

Blittable data types have a common representation with both .<strong>NET</strong> <strong>and</strong> COM, <strong>and</strong> no conversion is needed.<br />

Simple data types such as byte, short, int, long, <strong>and</strong> classes <strong>and</strong> arrays that only contain these simple<br />

data types belong to the blittable data types. Arrays must be one-dimensional to be blittable.<br />

A conversion is needed with nonblittable data types. The following table lists some of the nonblittable<br />

COM data types with their .<strong>NET</strong>-related data types. Nonblittable types have a higher overhead because of<br />

the conversion.<br />

Com daTa TyPe<br />

SAFEARRAY<br />

VARIANT<br />

BSTR<br />

IUnknown*, IDispatch*<br />

.neT daTa TyPe<br />

Array<br />

Object<br />

String<br />

Object<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!