Adding Support for Managed Extensions for C++ to an Existing Application
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:
- Modifying the Existing Project Settings
- Employing New Managed Extensions Functionality in Existing Applications
- Building and Testing for Regression
Note For a detailed guide to porting existing applications to Managed Extensions applications, see Managed Extensions for C++ Migration Guide.
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
- Open the target project in Visual Studio.
- In Solution Explorer, right-click the project node and click Properties.
The Property Pages dialog box appears.
- In the left pane, click the C/C++ folder.
- Click the General folder under C/C++.
- Set the Compile As Managed property to Assembly Support (/clr).
- Click OK and rebuild the project.
Once the project has been rebuilt, support for Managed Extensions is available.
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.
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;
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:
Console::WriteLine(MyLiteral); for (int i=0; i < a1->Length; ++i) // Use the Length property Console::Write(a1[i]); Console::WriteLine();
Finally, the following code reinitializes the array and prints the values again:
for (i=0; i < a1->Length; ++i) a1[i] = i; Console::WriteLine(MyLiteral2); for (i=0; i < a1->Length; ++i) Console::Write(a1[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.
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.