Chapter 4. Playing Nice with Others: Native Code and COM Interoperability

I l @ ve RuBoard

All Microsoft Visual Basic .NET applications run within the common language runtime (CLR). This should not be news to anyone . The CLR provides what's called a managed runtime environment, which offers a set of services, including garbage collection, to applications that run within that environment. In essence, the CLR is a kind of sandbox ”it isolates Visual Basic .NET applications from native (unmanaged) applications that are running on the same computer system. It's been said that no man is an island. That's also true for a runtime environment such as the CLR ”it cannot exist on its own. Applications that run in this environment must be able to communicate with the outside world. (They'd be pretty useless otherwise .)

Likewise, enterprise application development often has more to do with playing nice with a company's existing roster of applications, services, and libraries than with creating lots of new, cool code. Backward compatibility and system integration are both the bane and the lifeblood of the enterprise developer. Given this situation, the ability to communicate with legacy systems and components is critically importance. The irony is that these "legacy" components might very well be products under active development. After all, COM sure isn't going away anytime soon.

On the Microsoft Windows platform, two major buckets of native code can be accessed outside of the runtime: native methods and COM objects (as shown in Figure 4-1). Native methods are functions that are contained in native dynamic-link libraries (the entire Win32 API falls into this category). To solve the problem of communicating with native methods, the CLR provides the platform invoke (PInvoke) service. This service lets you define and invoke native methods in a very natural way and gives you the flexibility to work around the functional limitations of the Microsoft .NET Framework. (Yes, there are limitations.)

Figure 4-1. The CLR and the unmanaged world.

graphics/f04pn01.jpg

With COM objects and type libraries, on the other hand, the challenge is to provide a rich type equivalent that you can use in your managed applications and also deal with all of the marshaling and threading complexities in a relatively transparent fashion. After all, COM objects are classes, with methods, properties, interfaces and a whole bunch of other stuff. It's important to be able to treat these objects, for the most part, as managed classes. It's also important to maintain the ease of use of COM objects that classic Microsoft Visual Basic developers are accustomed to.

COM interoperability (COM interop), unlike PInvoke, supports bidirectional communication. In general, COM interop can be considered a bidirectional service that provides a bridge between the .NET Framework and COM. It allows .NET clients to call COM components and COM clients to call .NET Framework components. COM interop is designed to maintain consistency between COM and .NET. It hides the inconsistencies between the two runtime models and makes the differences transparent to both clients and servers (managed or unmanaged).

Note

At the lowest level, PInvoke and COM interop both use the same marshaling service, but the COM interop layer provides a lot of additional functionality.


In this chapter, we'll cover both PInvoke and COM interop. The discussion of PInvoke will cover the basics as well as how to handle more complex data type marshaling situations. The discussion of COM interop will deal with issues related to accessing COM objects from within Visual Basic .NET as well as how to expose managed component through a COM-accessible interface.

Note

To play along with some of the COM interop material, you should probably have Visual Basic 6.0 installed. Creating samples in Visual Basic 6.0 is handy, and Visual Basic 6.0 is also a useful test tool for Visual Basic .NET components that you want to expose to COM clients.


I l @ ve RuBoard


Designing Enterprise Applications with Microsoft Visual Basic .NET
Designing Enterprise Applications with Microsoft Visual Basic .NET (Pro-Developer)
ISBN: 073561721X
EAN: 2147483647
Year: 2002
Pages: 103

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