This documentation is archived and is not being maintained.

Adding Support for Managed Extensions for C++ to an Existing Application

Visual Studio .NET 2003

One of the strongest reasons for using Managed Extensions for C++ is the ease in converting existing C++ applications to Managed Extensions applications. The procedure consists of three steps:

Note   For a detailed guide to porting existing applications to Managed Extensions applications, see Managed Extensions for C++ Migration Guide.

Modifying the Existing Project Settings

The first step is to modify the compiler options by adding the /clr option and to recompile the target application. The /clr option enables support for Managed Extensions and forces a link to the proper library.

To modify the project settings

  1. Open the target project in Visual Studio.
  2. In Solution Explorer, right-click the project node and click Properties.

    The Property Pages dialog box appears.

  3. In the left pane, click the C/C++ folder.
  4. Click the General folder under C/C++.
  5. Set the Compile As Managed property to Assembly Support (/clr).
  6. Click OK and rebuild the project.

Once the project has been rebuilt, support for Managed Extensions is available.

Employing New Managed Extensions Functionality in Existing Applications

Once the target application has been built with Managed Extensions support, all .NET Framework features, from managed types to .NET Framework classes (such as String), can be easily added.

For the purposes of this article, this step is demonstrated using the .NET Framework classes String and Console.

The following code declares two instances of the String class and a managed array of int type:

String* MyLiteral= L"Current array values";
String* MyLiteral2= L"Current array values (after initialization)";
int a1 __gc[] = new int __gc[10];

The use of the keyword __gc indicates that a1 is a managed array. As such, the contents will automatically be initialized to 0, and the memory required will be reclaimed automatically. Furthermore, managed arrays inherit from the System::Array .NET class, which provides a number of useful methods and properties.

After the variables have been declared, the following code creates an instance of the System::Console class and prints the current array contents (each element is initialized to 0) using the WriteLine method of the System::Console class and the Length property of the System::Array class:

for (int i=0; i < a1->Length; ++i)   // Use the Length property

Finally, the following code reinitializes the array and prints the values again:

for (i=0; i < a1->Length; ++i)
   a1[i] = i;
for (i=0; i < a1->Length; ++i)

This example demonstrates some of the benefits of using Managed Extensions in a C++ application:

  • Managed array elements are automatically initialized to 0.
  • Managed arrays are automatically destroyed and all resources are freed by the managed heap.
  • .NET Framework base classes are easily accessible and are managed classes themselves.

Note that managed and non-managed (native) constructs can be mixed in a single application. If the array in the previous example had not been declared using the __gc keyword, for example, a native C++ array would have been created.

Building and Testing for Regression

The final step is achieving an error-free build of the application and testing the new functionality.

A full treatment of this step is beyond the scope of this topic. For more information on migration of existing applications to Managed Extensions for C++, see Managed Extensions for C++ Migration Guide.

See Also

Introduction to the .NET Framework Class Library | Managed Extensions for C++ Programming