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 is simple and straightforward and consists of three steps:
The first step is to modify the compiler options by adding the /com+ option and recompile the target application. The /com+ option enables support for Managed Extensions and forces a link to the proper library.
To modify the project settings
Once the project has been rebuilt, support for Managed Extensions is now available.
Once the target application has been built with Managed Extensions support, all NGWS runtime features, from managed objects to NGWS frameworks base classes (such as String), can be easily added. For a full list of these features, see Managed Extensions for C++.
For the purposes of this article, it is sufficient to demonstrate this step using the NGWS frameworks base 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)"; __gc int a1[] = new __gc int[20];
Note Because the a1
array is a managed array the managed operator new is used to declare and initialize the memory to 0.
After the variables have been declared, an instance of the NGWS frameworks Console class is created and the current values (all equal to 0) in the array is printed using the Write method of the Console class and the Length method of the Array class:
Console::Write(MyLiteral); Console::Write('\n'); for (i=0; i < a1->Length; ++i) // Use the Length member { Console::Write(a1[i]); Console::Write(L' '); }
Finally, the array is reinitialized and the values are again printed:
for (i=0; i < 20; ++i) a1[i] = i; for (i=0; i < a1->Length; ++i) // Use the Length member { Console::Write(a1[i]); Console::Write(L' '); }
Examining the example code reveals some of the benefits of using Managed Extensions in a C++ application:
The final step is achieving an error-free build of the application and testing of the new functionality.