IN THIS CHAPTER
Interoperability refers to the ability of managed and unmanaged code to invoke each other and exchange data in an organized and reproducible fashion. This chapter introduces you to the basic concepts behind code and data interoperability in C# and provides you with code samples for communicating with unmanaged code via COM or using Platform Invoke for accessing unmanaged DLLs such as the Win32 API.
Interoperability enables you to leverage your existing investment in unmanaged code going forward with the .NET Framework. Unmanaged code consists of all code that is not managed and executed within the Common Language Runtime (CLR). This includes COM objects, C++ DLLs, ActiveX controls, COM+ components, and any other code that is not compiled.
As you will find out, there are two different kinds of interoperability in C#, interoperability with unmanaged DLLs and interoperability with binary COM objects. Each is accomplished using a different technology, but some aspects of interoperability remain the same regardless of whether you’re working with unmanaged DLLs or COM objects.
One of the things that remain the same is the need for reliable transfer of data. A common problem among developers before managed code was not being able to write code in one language that could communicate with code written in another language. For example, a C string was stored in memory as an array of bytes terminated by the ASCII null (