C Sharp

Interoperating with Unmanaged Code

A new language or development environment would be short-lived if it were to ignore legacy systems and code and provide only a means of writing new systems. Regardless of how great a new technology might be, its makers must take into account that for a time the new must exist with the old.

To that end, the .NET and C# design teams have made it easy for programmers to interoperate with existing code through the use of unmanaged code. Unmanaged code refers to code that is not managed, or controlled, by the .NET runtime. In this chapter, I'll cover the three main examples of unmanaged code in .NET, as follows: -

  • Platform Invocation Services These services enable .NET code to access functions, structures, and even callbacks in existing, unmanaged dynamic-link libraries (DLLs).
  • Unsafe code Writing unsafe code allows the C# programmer to use constructs (such as pointers) in C# applications at the expense of that code being managed by the .NET runtime.
  • COM interoperability This term refers to the ability of .NET code to use COM components and of COM applications to use .NET components.