Introduction

.NET provides a number of features, such as garbage collection and managed memory, that make programming easier and safer than before. However, there are millions of lines of existing code that are here to stay for a considerable time. One of the goals of the .NET Framework designers was to provide smooth interoperability between managed and unmanaged code.

Let's refresh our understanding of managed and unmanaged code. Managed code is the code that is written according to the specifications of the common language runtime (e.g., providing metadata). Managed code can take advantage of the services provided by the common language runtime such as garbage collection and enhanced security. Unmanaged code on the other hand is any code that does not target the common language runtime and hence cannot take advantage of the services offered by runtime. Existing COM components, native DLLs, and so on, fall under the unmanaged code category.

The common language runtime supports:

  • Managed code making calls into native DLLs.

  • Managed code making calls into COM components.

  • Wrapping .NET code as COM components so that unmanaged code can deal with them.

To support marshaling across native DLLs as well as COM components, the .NET Framework defines a collection of classes under the namespace System.Runtime.InteropServices. Some examples of these classes are Marshal, DllImportAttribute, and MarshalAsAttribute. Later in the chapter, we will see how these classes, and many others, let us define and control the marshaling behavior. Unless otherwise explicitly mentioned, any class related to interoperability that we discuss in this chapter can be assumed to belong to the System.Runtime.InteropServices namespace.

At this point it is worth mentioning that the primary purpose of .NET's support for interoperability is to deal with existing Windows-based code. Keep in mind that .NET assemblies making calls to the unmanaged code may not be portable to other platforms.

Finally, it is worth mentioning that Microsoft has added extensions (called Managed Extensions) to Visual C++ language to support mixing managed and unmanaged code freely. Obviously, the unmanaged code will not be able to take advantage of the services provided by the common language runtime. However, Managed Extensions do provide the ability for you to port your existing code incrementally, without incurring the cost of porting the whole application at once. You can maintain your existing code base as unmanaged but still write newer code as managed code to take advantage of the new features of the .NET Framework. As a matter of fact, you can simply compile your existing C++ code with the -clr compiler switch to produce an output that is compatible with the common language runtime (what Microsoft calls “It Just Works.”). Covering Managed C++ is beyond the scope of this book. Two good references on Managed Extensions to C++ are [Sel-01] and [Ras-01].

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.194.39