COM and Interop

We haven't yet examined how a .NET application can use COM components. This level of COM interop is usually seamless and is well documented in almost every book about .NET fundamentals. The basic idea is to create a wrapper between the .NET application and the COM component. The .NET application communicates directly with this wrapper (often called a runtime callable wrapper [RCW]), which in turn communicates with the unmanaged COM component. Figure 9-1 depicts this arrangement.

Figure 9-1. The runtime callable wrapper

graphics/f09dp01.jpg

The RCW is an ordinary .NET assembly that must be deployed with the .NET application. Some vendors provide an RCW assembly with their COM components. If they don't, you can create an RCW for an existing COM component using one of two techniques. The fastest approach is to add a reference to the COM component in Microsoft Visual Studio .NET. Visual Studio .NET will then generate the RCW assembly and place it in the project directory. Your second option is to generate the RCW manually using the tlbimp.exe command-line utility included with the .NET SDK. Chapter 16 presents a case study that uses the COM interop features of .NET.

For most developers, this automatic support provides everything they need. Of course, the actual .NET code in the RCW performs the real heavy lifting and it makes use of a slew of .NET types (mainly from the System.Run­time.InteropServices namespace) that allow explicit communication between managed and unmanaged code and mapping between .NET and COM types. It is possible to delve deeper into these details and create your own custom RCW, but this is a task best suited for a seasoned COM expert. If you plan to dive into these lower-level details, I suggest a dedicated book such as .NET and COM: The Complete Interoperability Guide by Adam Nathan (Sams, 2002).

COM interoperability isn't just a .NET nicety it's a core part of the .NET Framework. In fact, Microsoft originally planned to rewrite COM+ entirely in managed .NET code, and future versions of .NET might still pursue this goal. Because of the scope and ambition of the .NET Framework, however, some plans had to be scaled back. COM+ was eventually implemented with heavy use of COM interop.

Of course, creating a .NET serviced component isn't the same as consuming a COM component from a .NET application, so you don't need to use an RCW. Instead, .NET simplifies development of components that use COM+ services (called serviced components) through the special ServicedComponent class (as discussed in the next section).



Microsoft. NET Distributed Applications(c) Integrating XML Web Services and. NET Remoting
MicrosoftВ® .NET Distributed Applications: Integrating XML Web Services and .NET Remoting (Pro-Developer)
ISBN: 0735619336
EAN: 2147483647
Year: 2005
Pages: 174

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net