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.

698 ❘ ChaPTer 26 interOp<br />

can see in Figure 26-3, a dual interface is derived from IDispatch but provides the additional methods of<br />

the interface directly in the vtable. Scripting clients can use the IDispatch interface to invoke the Add <strong>and</strong><br />

Sub methods, whereas clients aware of the vtable can call the Add <strong>and</strong> Sub methods directly.<br />

QueryInterface<br />

AddRef<br />

Release<br />

GetTypeInfoCount<br />

"Add"<br />

"Sub"<br />

47<br />

48<br />

GetIDsOfNames<br />

Invoke<br />

Add<br />

47 pAdd<br />

48 pSub<br />

figure 26-3<br />

Sub<br />

Casting <strong>and</strong> Queryinterface<br />

If a .<strong>NET</strong> class implements multiple interfaces, casts can be done to get one interface or another. With<br />

COM, the interface IUnknown offers a similar mechanism with the method QueryInterface(). As<br />

discussed in the previous section, the interface IUnknown is the base interface of every interface, so<br />

QueryInterface() is available anyway.<br />

method binding<br />

How a client maps to a method is defined with the terms early <strong>and</strong> late binding. Late binding means that the<br />

method to invoke is looked for during runtime. .<strong>NET</strong> uses the System.Reflection namespace to make this<br />

possible (see Chapter 14).<br />

COM uses the IDispatch interface discussed earlier for late binding. Late binding is possible with dispatch<br />

<strong>and</strong> dual interfaces.<br />

With COM, early binding has two different options. One way of early binding, also known as vtable<br />

binding, is to use the vtable directly — this is possible with custom <strong>and</strong> dual interfaces. The second option<br />

for early binding is also known as ID binding. Here the dispatch ID is stored inside the client code, so<br />

during runtime only a call to Invoke() is necessary. GetIdsOfNames() is called during design time. With<br />

such clients, it is important to remember that the dispatch ID must not be changed.<br />

data Types<br />

For dual <strong>and</strong> dispatch interfaces, the data types that can be used with COM are restricted to a list of<br />

automation-compatible data types. The Invoke() method of the IDispatch interface accepts an array<br />

of VARIANT data types. The VARIANT is a union of many different data types, such as BYTE, SHORT, LONG,<br />

FLOAT, DOUBLE, BSTR, IUnknown*, IDispatch*, <strong>and</strong> so on. VARIANTs have been easy to use from Visual<br />

Basic, but it was complex to use them from C++. .<strong>NET</strong> has the Object class instead of VARIANTs.<br />

With custom interfaces, all data types available with C++ can be used with COM. However, this also<br />

restricts the clients that can use this component to certain programming languages.<br />

registration<br />

.<strong>NET</strong> distinguishes between private <strong>and</strong> shared assemblies, as discussed in Chapter 18. With COM, all<br />

components are globally available through a registry configuration.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!