Interoperabilty with Managed Code
This topic discusses the Windows Software Development Kit (SDK) managed programming model and how it can be used for migration and interoperation with your unmanaged applications.
Code that executes under the control of the common language runtime (CLR) is called managed code. Conversely, code that runs outside the runtime is called unmanaged code. The API functions in the Windows SDK are examples of unmanaged code.
The Windows SDK provides a rich environment for creating applications by using managed or unmanaged code. Although it is possible to develop complete solutions in either environment, there are situations in which you will want to combine the features of both managed and unmanaged programming models in your applications.
If you have existing unmanaged components, you can access these components from managed code.
If you have existing managed components, you can access these components from unmanaged code.
You can access components that are available only from the unmanaged programming model in your managed applications. For example, you can add Search and Organize tasks to your Windows Presentation Foundation (WPF)-based applications.
You can access components available only from the Windows SDK in your unmanaged applications. For example, you can host a WPF page in an unmanaged page.
For more information about how the .NET Framework interacts with unmanaged code, see the Interoperability topic in the .NET Framework documentation.
In many cases, the API for a feature is distributed only in a managed or unmanaged programming model. For example, many of the shell extensions are found only in Win32, which is the unmanaged programming model for the Windows operating system. Many of the communication features are found only in the .NET Framework, which is the managed programming model for Windows Vista.
Accessing Windows SDK Components from Managed Code
The .NET Framework promotes interaction with the Windows SDK. Data types, method signatures, and error-handling mechanisms vary between managed and unmanaged object models. To simplify interoperation between .NET Framework components and unmanaged code and to ease the migration path, the CLR conceals the differences in these object models from both clients and servers.
Calling Unmanaged DLL Functions Using Platform Invoke
Platform invoke is a service that enables managed code to call unmanaged functions that are implemented in dynamic link libraries (DLLs) provided in the Windows SDK. Platform invoke locates and invokes an exported function and marshals (moves) its arguments (integers, strings, arrays, structures, and so on) across the interoperation (managed/unmanaged) boundary as needed.
To call unmanaged DLL functions using platform invoke, follow these basic steps:
Identify the functions in the DLLs. At a minimum, you must know the name of the function and name of the DLL that contains it.
Create a prototype for the function in managed code.
Call the function that is defined by the prototype.
Interacting with Unmanaged Code Through Interfaces
Component Object Model (COM) interoperation enables managed code to interact with COM objects through interfaces. The basic steps are as follows:
Import a type library as an assembly. Object model type definitions usually reside in a type library. In contrast, managed code compilers produce type metadata in an assembly. The two sources of type information are different. Both Visual Studio (which can automatically convert COM types) and the Windows SDK (which includes the tlbimp.exe command-line tool) provide tools to generate metadata from a type library. The resulting assembly is called an interop assembly. A reference to the library is added during compilation.
Create COM types in managed code. You can inspect COM types, activate instances, and invoke methods on the COM object the same way you do for any managed type.
Compile the project.
Deploy the application. Interop applications are best deployed as strong-named, signed assemblies in the global assembly cache.
Accessing Managed Components from Unmanaged Code
You can access managed components in unmanaged applications by using C++/CLI. C++/CLI is the successor to Managed Extensions for C++. It uses the new language extensions for managed code and the /crl compiler switch for Microsoft C++. The /clr switch creates metadata for the application that can be consumed by other managed applications, and enables the application to consume types and data in the metadata of other managed components. The /clr switch can be applied to an entire project or only to the modules that reference .NET Framework objects.
For example, you can migrate an existing application that consists entirely of unmanaged functions to the .NET Framework platform by recompiling just one module with the /clr compiler switch. This module is then able to use .NET Framework features, but remains compatible with the remainder of the application. This method enables you to convert an application to the .NET Framework platform in a gradual, piece-by-piece way. You can even decide between managed and unmanaged code compilation on a function-by-function basis within the same file.
While interoperability can be simple to implement, the following concepts will affect the performance, execution, and security of your applications:
Marshaling: The interop marshaler marshals (moves) data between the CLR heap and the unmanaged heap. Marshaling occurs whenever the caller and callee cannot operate on the same instance of data. The interop marshaler makes it possible for both the caller and callee to appear to be operating on the same data although they each have their own copy of the data. Different techniques are used to marshal different data types, such as strings, structures, classes, and arrays.
Performance: Regardless of the interoperability technique used, special transition sequences, called thunks, are required each time a managed function calls an unmanaged function and vice versa. The Microsoft Visual C++ compiler inserts these thunks automatically. Cumulatively, these transitions can be expensive in terms of performance. The number of transitions and the interoperability technique used (platform invoke or COM) affect the application’s performance.
Code access security: Applications that use unmanaged code are subject to code access security limitations on native code. Compiler options are available to remove these dependencies, if you determine that this is a secure choice.
Threading models: COM also has a marshaler that marshals data between COM apartments or different COM processes. When calling between managed and unmanaged code within the same COM apartment, the interop marshaler is the only marshaler involved. Calling between managed code and unmanaged code in different COM apartments or different processes involve both the interop marshaler and the COM marshaler.
Use the following resources to help port your existing applications to the .NET Framework. These topics can also help you leverage your existing programming knowledge as you begin to develop .NET Framework applications.
Migrating from Win32 to the .NET Framework
Microsoft Win32 to Microsoft .NET Framework API Map (MSDN Web site)
Migrating from Visual Basic 6.0 to Managed Code
Migrating from managed extensions for C++ to C++/CLI
Migrating from ATL/STL to the .NET Framework
Pure C++: CLR Generics Versus C++ Templates (MSDN Magazine)
STL.NET Primer (MSDN Web site)
Migrating from CRT to the .NET Framework
Run-time Routines and .NET Framework Equivalents (MSDN Web site)
Migrating from MFC to the .NET Framework
The Official Microsoft WPF and Windows Forms Site (Microsoft .NET Framework, Windows Forms Web site)
Migrating from ADO to ADO.NET
ADO.NET for the ADO Programmer (MSDN Web site)
Migrating from DCOM to the .NET Framework
Using Web Services Instead of DCOM (MSDN Web site)
Migrating from ASP to ASP.NET
The following topics will help you use .NET Framework features and functionality to enhance your existing applications.
.NET Framework interoperability with Win32
.NET Framework interoperability with ActiveX
.NET Framework interoperability with COM and COM+
COM Interop in Visual Basic (MSDN Web site)
Interoperability with Windows Presentation Foundation