This documentation is archived and is not being maintained.

Managed Extensions for C++ Development Scenarios

Visual Studio .NET 2003

When using the .NET Framework, code that targets the common language runtime is known as managed code, while code that does not target the common language runtime is known as unmanaged code. Managed Extensions for C++ allows you to mix unmanaged and managed code within the same application. New applications written with Managed Extensions can take advantage of unmanaged code features and new managed code features. Existing components can easily be wrapped as .NET Framework components using Managed Extensions, preserving investment in existing code while integrating with the .NET Framework.

Managed Extensions is the best choice for the following development scenarios:

Rapidly migrating unmanaged C++ applications to the .NET Framework

If you have an existing unmanaged C++ application, Managed Extensions provides a smooth transition to the .NET Framework. Because you can mix unmanaged and managed code in the same application, even in the same file, you can move code over time, component by component, to the .NET Framework. You can also continue to write components in unmanaged C++, taking advantage of the full power and flexibility of the language, and only use Managed Extensions to write thin, high-performance wrappers that make your C++ code callable from .NET Framework components. For more information, see Managed Extensions for C++ Migration Guide.

Accessing a C++ component from a .NET Framework–compatible language

Managed Extensions supports calling a C++ class from any .NET Framework–compatible language. This is made possible by writing a simple wrapper class using Managed Extensions that exposes your C++ class and methods as a managed class. The wrapper is a fully managed class and can be called from any .NET Framework–compatible language. The wrapper class acts as a mapping layer between the managed class and the unmanaged C++ class; it passes method calls directly into the unmanaged class. Managed Extensions supports calls to any unmanaged DLL or library and unmanaged classes.

Accessing .NET Framework classes from unmanaged code

With Managed Extensions, you can directly create and call a .NET Framework class from your C++ code. In addition, you can write C++ code that treats the .NET Framework component like any other Managed Extensions class.

You can also use the unmanaged COM support in the .NET Framework to call .NET Framework classes. Depending on your project, you can use either unmanaged COM support or Managed Extensions to access .NET Framework components. In some cases, leveraging the existing COM support will be the best option. In other cases, it may be possible to increase performance and developer productivity by using Managed Extensions.

Using managed and unmanaged code in one executable file

The Visual C++ compiler translates data, pointers, exceptions, and instruction flow between managed and unmanaged contexts automatically and transparently. This process allows managed code to interoperate seamlessly with unmanaged C++ code.

You are given control over what data and code should be managed. This feature is supported by the ability to choose whether each class and function is managed or unmanaged. This flexibility is needed, because some types of code or data perform better in an unmanaged environment. However, managed code typically offers enhanced developer productivity because of features such as garbage collection and managed class libraries.

See Also

Background | Managed Extensions for C++ Programming